简体   繁体   English

WCF客户端与多个网络接口的行为不一致

[英]WCF client has inconsistent behavior with multiple network interfaces

I have the WCF client shown below: 我有如下所示的WCF客户端:

public static string Execute(string a)
{
    WebHttpBinding b = new WebHttpBinding();
    b.Security.Mode = WebHttpSecurityMode.Transport;
    WebChannelFactory<IAnimalService> f = new WebChannelFactory<IAnimalService>(b, new Uri(a));
    f.Endpoint.Behaviors.Add(new WebHttpBehavior());
    IWebService client = f.CreateChannel();
    return client.SayHello("moo");
}

I am testing the client (in a console host) against its counterpart service from 2 different computers (A, B) and and i get different outcome. 我正在针对来自2台不同计算机(A,B)的对应服务测试客户端(在控制台主机中),并且得到了不同的结果。 Here are the similarities and differences between A and B: 以下是A和B之间的异同:

  1. Both A and B are Windows Server 2012 R2 have multiple network interfaces. A和B均为Windows Server 2012 R2,具有多个网络接口。 And each of them have one interface connected to the Internet (so as to reach the service in question). 并且它们每个都有一个连接到Internet的接口(以便访问有问题的服务)。
  2. When running on machine A, everything works as expected i get expected response ("cow says 'moo'") in the client. 在计算机A上运行时,一切正常,我在客户端中得到了预期的响应(“牛说'moo'”)。
  3. When running on machine B, i get different behaviours: 在机器B上运行时,我会得到不同的行为:
    1. Via the web-browser (firefox), i can get to the service by constructing a request in the address bar. 通过网络浏览器(firefox),我可以通过在地址栏中构造一个请求来获得该服务。 I can see the request going on the external network interface. 我可以在外部网络接口上看到请求。 And everything works as in 2 above. 一切都如上述2所述。
    2. Via the app (ie code above), i get the exception below and more over, when looking at the request in wireshark, it does not go on the external network interface. 通过应用程序(即上面的代码),我在下面和上面得到了异常,当在wireshark中查看请求时,它不会在外部网络接口上运行。 The exception comes the fact that on that network interface there is a proxy and the proxy is rejecting the request ( X-Squid-Error -> ERR_ACCESS_DENIED 0 ). 例外情况是,在该网络接口上有一个代理,并且该代理拒绝了该请求( X-Squid-Error -> ERR_ACCESS_DENIED 0 )。

Exception: 例外:

Unhandled Exception: System.ServiceModel.Security.MessageSecurityException: The HTTP request was forbidden with client authentication scheme
 'Anonymous'. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace --- 

I also consulted the routing table on machine B and it shows that request should have gone on the external network interface. 我还查询了机器B上的路由表,它表明请求应该已经在外部网络接口上发出了。 I have assumed that the internal routing table would be consulted by the lower layer of the TCP-IP stack in order to forward the packet to the correct (network) medium. 我假设内部路由表将由TCP-IP堆栈的下层查询,以便将数据包转发到正确的(网络)介质。

So, I am wondering if someone would have an idea how i could get my client to pick the correct interface on machine B. I am not sure how to go about to troubleshoot the problem further. 因此,我想知道是否有人会知道如何让我的客户选择机器B上的正确接口。我不确定如何进一步解决该问题。 So any kind of advice/tips on how to get to the bottom of this situation on machine B would be most welcomed. 因此,关于如何在机器B上解决这种问题的任何建议/提示都将受到欢迎。

Try to use this snippet in you config file, so you will be able to use your default credentials to autenticate in the proxy server. 尝试在配置文件中使用此代码段,以便您将能够使用默认凭据在代理服务器中自动启动。

<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>

似乎默认情况下,绑定试图在计算机B上寻找默认的代理设置。添加以下绑定配置解决了该问题:

b.UseDefaultWebProxy = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM