Using WebClient in a WCF Service

I use WebClient to load some resource as follows:

Stream stream; try { WebClient webClient = new webClient(); stream = webClient.OpenRead(MyResourceUri); } catch (Exception) { return null; } return stream; 

When I do this in a WPF application, it works fine and it gets the correct thread.

When I do this in a WCF service call, it does not work. A WebException message is sent with the message "Unable to connect to remote server" . (It works for files hosted on my machine or inside the company’s network, however this fails for any resource on the Internet). The service is hosted on IIS7.

Research so far shows that the difference is related to the web proxy. Webclient.proxy in a WPF application refers to the proxy settings set in IE, while it is not in WCF.

Why is that? And more importantly, how can I get WebClient in WCF to use similar proxy settings?

EDIT: I installed the proxy server in WebClient and worked in the WCF service

 webClient.Proxy = new WebProxy(ProxyAddressFromIE); 

Here I hardcoded the proxy admin. What method / API is there to get it? And yet, why is it different from WCF service and WPF application?

+6
source share
3 answers

To answer one of your questions, the reason for the difference between your WPF application and the WCF service hosted in IIS is this.

WPF applications run in a real Windows session (more precisely, your user session). This means that a user profile has been loaded for this session, and this session contains, among other things, proxy settings configured in IE.

WCF services hosted in IIS do not start in a Windows session. They start as a service and therefore do not have a Windows session (they actually start in session 0, but this is just an implementation detail). This means that there is no proxy configuration.

To reliably solve this problem, you may have your own configuration for the proxy server, possibly in web.config. Another option is to configure a proxy server through netsh.exe.

+2
source

I needed to do the same, and I found the answer here: Get the URI from the default web proxy . Basically, you need to dynamically read the proxy server using WebRequest.GetSystemWebProxy() and by defining the proxy server using the test proxy.

Hope this helps!

+2
source

Consider handling the call using sources other than the highly abstract WebClient. From a higher to a lower level, this means that the WebRequest and WebResponse objects go all the way to socket programming. The reason is that the WebClient method is closely related to the choice in Internet Explorer, since most of the high-level Internet stack is in windows. If you want to get around this, you need to dig deeper.

I would like to point my finger exactly where to dig, but I have not suffered this particular problem and have no experience. I know where to look for the answer, but no indication of "X stands for place." However, due to the high level, very abstract nature of WebClient, I am not sure that you can easily bypass implicit stack creation and / or communication with IE, without any headaches, than beating and using an object that gives more explicit HTTP control -message.

Happy hunt.

0
source

Source: https://habr.com/ru/post/889959/


All Articles