Geeks With Blogs
Sean Carpenter Thoughts on Development

I've been playing with the National Digitial Forecast Database (NDFD) provided by the National Weather Service.  You can retrieve a forecast via a SOAP web service by providing your latitiude and longitude and a few other details.  The web service worked fine when I called it from Python, but I was getting an error when calling it from .NET (using the generated proxy classes in VS 2003 and VS 2005).  It turns out that the service needs HTTP version 1.0, not 1.1 (the default in .NET).  Unfortunately, the generated proxy class doesn't expose the ProtocolVersion property of the underlying HttpWebRequest object.  So, I added support for this property.

It's a little easier with VS 2005 because of the support for partial classes:

public partial class ndfd {
    private Version _protocolVersion = System.Net.HttpVersion.Version11;
    public Version ProtocolVersion {
        get { return _protocolVersion; }
        set { _protocolVersion = value; }
    }

    protected override System.Net.WebRequest GetWebRequest(Uri uri) {
        System.Net.WebRequest req = base.GetWebRequest(uri);
        ((System.Net.HttpWebRequest)req).ProtocolVersion = _protocolVersion;
        return req;
    }
}

I just added a new file to my solution and added a partial class definition (ndfd is the proxy class generated for the web service) that adds a ProtocolVersion property. Then in the GetWebRequest method, I set the protocol version of the WebRequest.

For VS 2003, I just derived a new class from the generated proxy class and added my property and override there:

public class ndfd2 : ndfd {
    private Version _protocolVersion = System.Net.HttpVersion.Version11;
    public Version ProtocolVersion {
        get { return _protocolVersion; }
        set { _protocolVersion = value; }
    }

    protected override System.Net.WebRequest GetWebRequest(Uri uri) {
        System.Net.WebRequest req = base.GetWebRequest(uri);
        ((System.Net.HttpWebRequest)req).ProtocolVersion = _protocolVersion;
        return req;
    }
}

Then I can just use the new class in my code. Both of these approaches avoid doing anything to the actual generated class, so the Web Reference can be updated without losing any of the customizations.

Posted on Saturday, May 27, 2006 6:28 AM .NET Programming | Back to top


Comments on this post: Changing the Protocol Version for a Web Service Call

Comments are closed.
Comments have been closed on this topic.
Copyright © Sean Carpenter | Powered by: GeeksWithBlogs.net