2012-11-19 147 views
3

我正在嘗試創建一個tcp wcf服務,該服務可以在沒有IIS的服務器上託管,並且無需身份驗證即可訪問該服務。如何託管公共WCF服務?

這裏是我做的:

ServiceHost svh = new ServiceHost(typeof(MyService)); 

var tcpbinding = new NetTcpBinding(SecurityMode.None); 

var location = "net.tcp://localhost:11111"; 
svh.AddServiceEndpoint(typeof(IMyService), tcpbinding, location); 

svh.Open(); 
.... 

該服務適用於locashost罰款,但是當我把它在服務器上(添加防火牆例外),客戶端與消息崩潰:

The socket connection was aborted. This could be caused by an error processing 
your message or a receive timeout being exceeded by the remote host, or an 
underlying network resource issue. Local socket timeout was '00:00:59.7344663'. 

這裏的客戶端配置文件:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <system.serviceModel> 
     <bindings> 
      <netTcpBinding> 
       <binding name="NetTcpBinding_IMyService"> 
        <security mode="None" /> 
       </binding> 
      </netTcpBinding> 
     </bindings> 
     <client> 
      <endpoint address="net.tcp://myserver:11111/" 
       binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IMyService" 
       contract="MyServer.IMyService" name="NetTcpBinding_IMyService" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

我應該爲此換個工作?

+0

您是否閱讀過http://msdn.microsoft.com/zh-cn/library/bb332338.aspx? –

回答

1

我以前有過類似的問題,我通過在端點地址(ServiceHost .ctor)中不使用localhost來解決問題,而是使用實際的機器名替代。另外,您可以在服務主機本身中定義地址,如下所示。

ServiceHost svh = new ServiceHost(typeof(MyService), new Uri("net.tcp://myserver:11111")); 
var tcpbinding = new NetTcpBinding(SecurityMode.None); 
svh.AddServiceEndpoint(typeof(IMyService), tcpbinding, ""); 
svh.Open(); 
+0

是的,這工作!謝謝 –