简体   繁体   中英

WCF client has inconsistent behavior with multiple network interfaces

I have the WCF client shown below:

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. Here are the similarities and differences between A and B:

  1. Both A and B are Windows Server 2012 R2 have multiple network interfaces. And each of them have one interface connected to the Internet (so as to reach the service in question).
  2. When running on machine A, everything works as expected i get expected response ("cow says 'moo'") in the client.
  3. When running on machine B, i get different behaviours:
    1. Via the web-browser (firefox), i can get to the service by constructing a request in the address bar. I can see the request going on the external network interface. And everything works as in 2 above.
    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. 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 ).

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. 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.

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. So any kind of advice/tips on how to get to the bottom of this situation on machine B would be most welcomed.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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