2011-12-15 203 views
0

實際上,我有兩個不同的問題,這取決於我嘗試運行看起來相關但以不同方式顯示自己的服務的方式。請注意,我運行的代碼是Microsofts guide中使用的代碼的完全重複,但不同的名稱空間和稍微不同的類名除外。WCF服務託管在Windows服務將無法正常運行

當我嘗試使用此方法運行服務時,Windows服務成功啓動,但是當它做到了WCF服務主機框會彈出,並且它提供了一條錯誤消息,表示IP端點localhost上已有一個偵聽器。這裏是我的配置文件,當我跑這樣:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <service behaviorConfiguration="CfmaWcfEphemerisLibrary.ServiceBehavior" 
       name="CfmaWcfEphemerisLibrary.Service1"> 
     <endpoint address="" binding="netTcpBinding" bindingConfiguration="" 
      contract="CfmaWcfEphemerisLibrary.IService1"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" 
      contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="net.tcp://localhost:8529/Service1" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="CfmaWcfEphemerisLibrary.ServiceBehavior"> 
      <serviceMetadata httpGetEnabled="false" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

一會兒戰鬥後,我發現了一個建議,說讓TCP連接可能有助於在端口共享。我嘗試過,但是當我嘗試啓動服務時失敗,並在「應用程序」下的Windows事件日誌中出現錯誤消息:

服務無法啓動。 System.ServiceModel.AddressAlreadyInUseException:IP端點0.0.0.0:8529上已有一個偵聽器。確保您沒有試圖在您的應用程序中多次使用此端點,並且沒有其他應用程序正在偵聽此端點。 ---> System.Net.Sockets.SocketException:通常只允許使用每個套接字地址(協議/網絡地址/端口) 在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress套接字地址) System.Net.Sockets.Socket.Bind(EndPoint localEP) at System.ServiceModel.Channels.SocketConnectionListener.Listen() ---內部異常堆棧跟蹤結束--- at System.ServiceModel.Channels.SocketConnectionListener.Listen () 在System.ServiceModel.Channels.BufferedConnectionListener.Listen() 在System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen() 在System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener) 在System.Service Model.Channels ....

我不明白爲什麼我在啓用共享的端口上收到使用端口異常。當我嘗試運行啓用了端口共享的服務時,這是我的App.config文件。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <service behaviorConfiguration="CfmaWcfEphemerisLibrary.ServiceBehavior" 
       name="CfmaWcfEphemerisLibrary.Service1"> 
     <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpBinding" 
      contract="CfmaWcfEphemerisLibrary.IService1"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" 
      contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="net.tcp://localhost:8529/Service1" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="CfmaWcfEphemerisLibrary.ServiceBehavior"> 
      <serviceMetadata httpGetEnabled="false" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <bindings> 
     <netTcpBinding> 
     <binding portSharingEnabled="true" name="tcpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"> 
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
      <security> 
      <transport> 
       <extendedProtectionPolicy policyEnforcement="Never" /> 
      </transport> 
      </security> 
     </binding> 
     </netTcpBinding> 
    </bindings> 
    </system.serviceModel> 
</configuration> 
+0

進入命令提示符。運行netstat -a。確保沒有任何東西已經在使用端口8529.另外,如果您在代碼中發佈瞭如何在Windwos服務中創建和凝視WCF服務主機,可能會有所幫助。 – user957902 2011-12-15 15:17:42

回答

0

可能使用同一端口的元數據交換的問題: http://msdn.microsoft.com/en-us/library/aa702636.aspx 參見「服務端點,並使用NetTcpBinding的一個MEX終結點之間共享一個端口」。

兩個詞,這能解決問題: <端點地址= 「的net.tcp://本地主機:8530/MEX」 ... >