Glen
Glen

Reputation: 95

WebRequest within Soap

I am hoping someone will be able to help me, I haven't been programming in C# for a while. I'm sure the answer will be simple but i just can't get my head around it.

I have the following, where i call System.Web.Services.Protocols.SoapHttpClientProtocol. I have included just one of the many calls that i have.

public partial class ExchangeService : System.Web.Services.Protocols.SoapHttpClientProtocol   {

//Lots of code using Soap 

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("getDetailsLite", RequestNamespace="http://www.MySite.com/ExchangeService/", ResponseNamespace=" http://www.MySite.com/ExchangeService/", ", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlElementAttribute("Result", IsNullable=true)]
    public GetDetailsLiteResp getDetailsLite(getDetailsLiteReq request) {
        object[] results = this.Invoke("getDetailsLite", new object[] {
                    request});
        return ((getDetailsLiteResp)(results[0]));
    }

    public void getDetailsLiteAsync(getDetailsLiteReq request) {
        this. getDetailsLiteAsync(request, null);
    }

    public void getDetailsLiteAsync (getDetailsLiteReq request, object userState) {
        if ((this.getDetailsLiteOperationCompleted == null)) {
            this.getDetailsLiteOperationCompleted = new System.Threading.SendOrPostCallback(this.OngetDetailsLiteOperationCompleted);
        }
        this.InvokeAsync("getDetailsLite", new object[] {
                    request}, this. getDetailsLiteOperationCompleted, userState);
    }
}

I want to override the WebRequest that the SoapHttpClientProtocol calls. The SoapHttpClientProtocol looks like this (which I believe is called from System.Web.Services.dll)

namespace System.Web.Services.Protocols  {
    public class SoapHttpClientProtocol : HttpWebClientProtocol    {
    public SoapHttpClientProtocol();
    public SoapProtocolVersion SoapVersion { get; set; }
    protected IAsyncResult BeginInvoke(string methodName, object[] parameters, AsyncCallback callback, object asyncState);
    public void Discover();
    protected object[] EndInvoke(IAsyncResult asyncResult);
    protected virtual XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize);

    //This line is the one i am talking about
    protected override WebRequest GetWebRequest(Uri uri);

    protected virtual XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize);
    protected object[] Invoke(string methodName, object[] parameters);
    protected void InvokeAsync(string methodName, object[] parameters, SendOrPostCallback callback);
    protected void InvokeAsync(string methodName, object[] parameters, SendOrPostCallback callback, object userState);
    }
}

I need protected the override WebRequest GetWebRequest(Uri uri ) to look like this:

protected override WebRequest GetWebRequest(Uri uri)  {
    HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);
    webRequest.KeepAlive = false;
    webRequest.ProtocolVersion=HttpVersion.Version10;
    return webRequest;
}

Does anyone know how i would do this? I am uanble to edit it directly inside of the SoapHttpClientProtocol which im 99% sure i shouldn't be doing anyway.

Thanks for any help you may be able to provide

Upvotes: 0

Views: 8494

Answers (2)

McGarnagle
McGarnagle

Reputation: 102793

Subclass it? GetWebRequest is marked as virtual in the root class WebClientProtocol, so you can override the method.

public class MyHttpProtocol : SoapHttpClientProtocol  
{
    public override WebRequest GetWebRequest(Uri uri)
    {
        // Base request:
        WebRequest request = base.GetWebRequest(uri);

        // You code goes here...

        // Return...
        return request;
    }
}

Then use MyHttpProtocol as needed within your service class.

Upvotes: 1

Jon Senchyna
Jon Senchyna

Reputation: 8047

Since ExchangeService is a partial class, you can create another file that adds to the declaration of ExchangeService and also declares a suitable override for GetWebRequest. This will allow you to write custom functionality for ExchangeService, while still having it be updatable if the WSDL changes.

For more info, see the MSDN article Partial Classes and Methods (C# Programming Guide).

Upvotes: 2

Related Questions