Introduction You may receive the error "The underlying connection was closed: Unable to connect to the remote server." while trying to consume a Web Service from your ASP.NET Web Application. However, the same Web Service can be consumed from a Windows Application without any issues.
Why this error occurs? This error particularly occurs if you are behind a firewall or proxy. When you use a winforms app, it can autodetect the proxy using IE settings for current user, and it can connect out through the proxy. However, when in asp.net, it cannot because the asp.net user identitiy does not have the correct proxy settings.
Resolution To resolve this issue, you need to explicitly specify the proxy settings for your application. You can do it at various levels viz., at the Machine.Config file which will apply for all the applications running on the system, at the Web.Config file such that it applies for a single application, and at the Page Level programattically in the code behind / code inline.
Web.Config setting
<configuration> <system.net> <defaultProxy> <proxy usesystemdefault = "false" proxyaddress="http://address:port" bypassonlocal="false" /> </defaultProxy> </system.net> <system.web>
For Machine.Config use the same settings as above.
To do it programmatically,
using System.Net;
WebServiceClass MyWebServiceClass = new WebServiceClass(); WebProxy proxyObject = new WebProxy("http://address:port", true); MyWebServiceClass.Proxy = proxyObject; MyWebServiceClass.MyWebMethod();
where WebServiceClass is the name of the Web Service Class which you are consuming.
That should solve the issue with consuming the Web Service across Proxy Settings.
Summary This article discussed on the Webservice error that occurs while consuming a service and the resolution for the same.
|
| Author: Quang 25 Mar 2009 | Member Level: Bronze Points : 1 |
This is great I find that most of the time this error is due to firewall of some sort. You can get the around proxy issue by setting the keepalive property. See more info here: http://connectionstringexamples.com/article.php?story=underlying-connection-closed
|