2011-12-02 181 views
2

我有一個簡單的WCF服務。由於各種原因,所有配置都是以編程方式完成的,而不是xml。服務是自己託管的。當我在本地機器上運行主機和客戶端時,它運行良好。當我將主機移動到局域網中的另一臺機器時,它也很好用。但是,當我嘗試通過走出局域網(即使用我的路由器的WAN地址而不是本地局域網地址)到達任何一臺機器上的主機時,它不起作用,並且出現下面的錯誤。 I am將路由器上的端口8100轉發到服務主機。我還清除了主機防火牆上的端口,並嘗試徹底關閉防火牆。兩者都沒有喜樂。嘗試連接到遠程WCF服務時發生TCP錯誤(10060)

我現在使用的TCP沒有安全性,以保持簡單。任何人都可以提出什麼可能會導致這種行爲?謝謝。

(注:我掩蓋在下面的代碼中的WAN和LAN地址。)

我的客戶端類:

public class TrackingClient : IService 
{ 
    protected readonly Binding binding; 
    protected readonly EndpointAddress endpointAddress; 
    IService proxy; 

    public TrackingClient() 
    { 
     this.binding = new TCPBinding(); 
     this.endpointAddress = new EndpointAddress("net.tcp://WAN_IP:8100/Tracking"); 

     proxy = ChannelFactory<IService>.CreateChannel(
      binding, 
      endpointAddress); 
    } 

    public bool report(Payload payload) 
    { 
     return proxy.report(payload); 
    } 
} 

我的主機類:

 ServiceHost host = new ServiceHost(typeof(Service)); 
     host.AddServiceEndpoint(
      typeof(IService), 
      new TrackingComm.TCPBinding(), 
      "net.tcp://LAN_IP:8100/Tracking"); 

     host.Open(); 
     Console.WriteLine("=== TRACKING HOST ==="); 
     Console.ReadLine(); 
     host.Close(); 

我的綁定:

public class TCPBinding : NetTcpBinding 
{ 
    public TCPBinding() 
    { 
     Security.Mode = SecurityMode.None; 
     Security.Transport.ClientCredentialType = TcpClientCredentialType.None; 
     Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.None; 
     Security.Message.ClientCredentialType = MessageCredentialType.None; 
    } 
} 

錯誤R I得到當客戶端試圖連接:

Could not connect to net.tcp://WAN_IP:8100/. The connection attempt lasted for a time span of 00:00:21.0772055. TCP error code 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond WAN_IP:8100. 

[編輯]

與此掙扎幾天後,我終於設法追查問題我的路由器迴環問題。雖然我仍然沒有弄清楚爲什麼,但是發生的是,儘管路由器被設置爲轉發端口,但是當我使用WAN地址時,來自客戶端的呼叫並未將其傳送到主機。無論是通過路由器「走出去」還是「進入」,仍然是個謎,但是當我在局域網外的另一臺計算機上運行客戶端時,它運行得很好

回答

0

要添加到的代理設置配置文件是如下:

<system.net> 
<defaultProxy> 
<proxy 
usesystemdefault = "false" 
proxyaddress="http://address:port" 
bypassonlocal="false" 
/> 
</defaultProxy> 
</system.net> 

[此外]
WCF service connection issue - maybe security?
[/添加]

+0

你可以使用TCP通道的代理嗎?如果我明白[這](http://stackoverflow.com/questions/520692/does-wcfs-nettcpbinding-supports-socks-proxy)的權利,你不能。 – Gadzooks34

+0

當然 - 你認爲http使用什麼類型的頻道:) – KevinDTimm

+0

夠公平的。然後我不得不承認我被卡住了。你知道我怎麼可以包含你提供的信息,沒有配置文件?我可以在BasicHttpBinding類中找到proxyAddress屬性,但在NetTcpBinding類中找不到任何相關的東西。或者我可以以某種方式設置系統級別的默認屬性? (這是從上面的配置代碼片段看起來的樣子)。 – Gadzooks34

0

這工作對我的服務:

<system.net> 
    <defaultProxy> 
     <proxy autoDetect="True"/> 
    </defaultProxy> 
</system.net> 
相關問題