SOAP webservices transport layer

Stand-alone usage #

To utilize the enhanced abilities of HttpRequest in a .NET SOAP web service client without affecting the rest of your application, you have to override the GetRequest method in the generated web service stub class that inherits from .NET's SoapHttpClientProtocol.

The best way to do this is to add a custom partial class that implements the GetRequest method and also adds a custom constructor that creates an instance of Rebex HttpRequestCreator.

CSharp

// this assumes you already have a generated MyWebService class in your project
// and that it inherits from System.Web.Services.Protocols.SoapHttpClientProtocol
public partial class MyWebService
{
    private readonly HttpRequestCreator _creator;

    public MyWebService()
    {
        // instantiate HttpRequest creator in the constructor
        // to make HTTP and TLS/SSL session caches work properly
        _creator = new HttpRequestCreator();

        // specify any settings if needed
        _creator.Settings.SslAllowedVersions = TlsVersion.TLS12 | TlsVersion.TLS11 | TlsVersion.TLS10;
    }

    protected override WebRequest GetWebRequest(Uri uri)
    {
        switch (uri.Scheme)
        {
            case "http":
            case "https":
                // use Rebex HttpRequest to handle HTTP and HTTPS requests
                return _creator.Create(uri);
            default:
                // use default functionality to handle other requests
                return base.GetWebRequest(uri);
        }
    }
}

VisualBasic

' this assumes you already have a generated MyWebService class in your project
' and that it inherits from System.Web.Services.Protocols.SoapHttpClientProtocol
Partial Public Class MyWebService
    Private ReadOnly _creator As HttpRequestCreator

    Public Sub New()
        ' instantiate HttpRequest creator in the constructor
        ' to make HTTP and TLS/SSL session caches work properly
        _creator = New HttpRequestCreator()

        ' specify any settings if needed
        _creator.Settings.SslAllowedVersions = TlsVersion.TLS12 Or TlsVersion.TLS11 Or TlsVersion.TLS10
    End Sub

    Protected Overrides Function GetWebRequest(uri As Uri) As WebRequest
        Select Case uri.Scheme
            Case "http", "https"
                ' use Rebex HttpRequest to handle HTTP and HTTPS requests
                Return _creator.Create(uri)
            Case Else
                ' use default functionality to handle other requests
                Return MyBase.GetWebRequest(uri)
        End Select
    End Function
End Class

Note: For a list of common pitfalls, check out Migrating to HttpRequest article.

Plugging into .NET WebRequest factory #

Registering HttpRequestCreator with .NET's WebRequest is the easiest way to make .NET SOAP web services use Rebex HttpRequest instead of System.Net.HttpWebRequest to handle HTTP and HTTPS requests.

CSharp

// instantiate HttpRequest creator
var creator = new HttpRequestCreator();

// specify any settings if needed
creator.Settings.SslAllowedVersions = TlsVersion.TLS12 | TlsVersion.TLS11 | TlsVersion.TLS10;

// register this creator with .NET's WebRequest to handle HTTP
// and HTTPS requests (replacing System.Net.HttpWebRequest)
creator.Register();

// from now on, all SOAP web service stubs that inherit from
// System.Web.Services.Protocols.SoapHttpClientProtocol class
// will use Rebex HttpRequest instead of .NET HttpWebRequest
// by default

// consume a SOAP web service
var service = new MyWebService();
var news = service.GetSomeNews();
// ...

VisualBasic

' instantiate HttpRequest creator
Dim creator = New HttpRequestCreator()

' specify any settings if needed
creator.Settings.SslAllowedVersions = TlsVersion.TLS12 Or TlsVersion.TLS11 Or TlsVersion.TLS10

' register this creator with .NET's WebRequest to handle HTTP
' and HTTPS requests (replacing System.Net.HttpWebRequest)
creator.Register()

' from now on, all SOAP web service stubs that inherit from
' System.Web.Services.Protocols.SoapHttpClientProtocol class
' will use Rebex HttpRequest instead of .NET HttpWebRequest
' by default

' consume a SOAP web service
Dim service = New MyWebService()
Dim news = service.GetSomeNews()
' ...

Note: For a list of common pitfalls, check out Migrating to HttpRequest article.