BizTalk Utilities CV ,   Jobs ,   Code library
 
Go to the front page to continue learning about XML or select below:

Contents

ReBlogger Contents

Previous posts in WSE

 
 
Page 21199 of 21350

Solving "The underlying connection was closed" when using WSE

Blogger : Jan Tielens Webservices
All posts : All posts by Jan Tielens Webservices
Category : WSE
Blogged date : 2004 May 08

Some time ago I've posted a code snippet to solve following exception which in some cases can happen when you're calling a web service:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.
at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at ...

It seems that this exception also pops up when using Web Services Enhancements (WSE), but the solution I provided doesn't work in that case. You can't get directly to the underlying HttpWebRequest to set the KeepAlive property to false. But recently some people posted two solutions in the comments, so all kudos goes to these people, this time I'm just the messenger! Both solutions use Reflection to get a hold of the HttpWebRequest, nice work!

Solution by Skeup&Zeb
using System;
using System.Net;
using System.Reflection;

// Web service reference entered in wizard was "MyWebService.MyWebServiceWse"
using MyProject.MyWebService;

namespace MyProject
{
    public class MySubClassedWebService : MyWebServiceWse { 
        private static PropertyInfo requestPropertyInfo = null;

        public MySubClassedWebService(){}

        protected override System.Net.WebRequest GetWebRequest(Uri uri) { 
            WebRequest request = base.GetWebRequest(uri);

            if (requestPropertyInfo==null) 
            // Retrieve property info and store it in a static member for optimizing future use 
            requestPropertyInfo = request.GetType().GetProperty("Request");

            // Retrieve underlying web request 
            HttpWebRequest webRequest = 
            (HttpWebRequest)requestPropertyInfo.GetValue(request,null);

            // Setting KeepAlive 
            webRequest.KeepAlive = false; 
            return request; 
        } 
    }
}

Solution by Schwank:
protected override System.Net.WebRequest GetWebRequest(Uri uri)

    PropertyInfo requestPropertyInfo = null;

    WebRequest request = base.GetWebRequest(uri);

    if (requestPropertyInfo==null)

    requestPropertyInfo = request.GetType().GetProperty("Request");

    // Retrieve underlying web request 
    HttpWebRequest webRequest = (HttpWebRequest)requestPropertyInfo.GetValue(request, null);

    // Setting KeepAlive 
    webRequest.KeepAlive = false;

    return request;
}


Read comments or post a reply to : Solving "The underlying connection was closed" when using WSE
Page 21199 of 21350

Newest posts
 

    Email TopXML