Adding HTTP Headers using MessageInspector in WCF (VB.NET) Not Working

25 Views Asked by At

I am working on a service client that's supposed to call an Enterprise SOAP service at my agency. I need to add a handful of HTTP Headers, so I created a Message Inspector:

Public Function BeforeSendRequest(ByRef request As Channels.Message, channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
    Dim objHttpRequestMessage As HttpRequestMessageProperty
    Dim objHttpRequestMessageObject As Object = Nothing

    If request.Properties.TryGetValue(HttpRequestMessageProperty.Name, objHttpRequestMessageObject) Then
        objHttpRequestMessage = CType(objHttpRequestMessageObject, HttpRequestMessageProperty)

        AddHeaders(objHttpRequestMessage)
    Else
        objHttpRequestMessage = New HttpRequestMessageProperty()

        AddHeaders(objHttpRequestMessage)

        request.Properties.Add(HttpRequestMessageProperty.Name, objHttpRequestMessage)
    End If

    Debug.Write("Just added headers...")

    Return Nothing
End Function

The AddHeaders(x) method adds the headers using x.Headers.Add(<name>, <value>) (these headers are secrets, so I cannot share them).

I add the inspector through a behavior:

Imports System.ServiceModel.Channels
Imports System.ServiceModel.Description
Imports System.ServiceModel.Dispatcher

Public Class ACUEIS2EndpointBehavior
    Implements IEndpointBehavior

    Public Sub Validate(endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
        ' Not implemented
    End Sub

    Public Sub AddBindingParameters(endpoint As ServiceEndpoint, bindingParameters As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
        ' Not implemented
    End Sub

    Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, endpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
        ' Not implemented
    End Sub

    Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
        clientRuntime.ClientMessageInspectors.Add(New ACUEIS2MessageInspector())
    End Sub
End Class

I plug in the behavior when I get my client proxy:

Private Shared Function GetProxy() As EmployeeInformationServiceDelegateClient
    Dim objBinding As WSHttpBinding =
        New WSHttpBinding(SecurityMode.TransportWithMessageCredential)

    With objBinding
        .Security.Message.ClientCredentialType = MessageCredentialType.UserName
        .ReceiveTimeout = New TimeSpan(0, 0, 0, 0, 500)
        .SendTimeout = New TimeSpan(0, 0, 0, 0, 500)
        .TextEncoding = Encoding.UTF8
    End With

    Dim objProxy As EmployeeInformationServiceDelegateClient =
        New EmployeeInformationServiceDelegateClient(objBinding, New EndpointAddress(GetEndpoint()))

    With objProxy.ClientCredentials.UserName
        .UserName = EncryptionManager.Decrypt(GetClientPIN())
        .Password = EncryptionManager.Decrypt(GetClientPassword())
    End With

    objProxy.Endpoint.EndpointBehaviors.Add(New ACUEIS2EndpointBehavior())
    ' objProxy.ChannelFactory.Endpoint.EndpointBehaviors.Add(New ACUEIS2EndpointBehavior())

    Return objProxy
End Function

As you can see, I tried adding it to my client endpoint and the channelfactory endpoint behaviors. Either way, I see that the code runs and attempts to add the headers, but when I look at the log file - even though it's clear the "add headers" logic runs before the headers are sent, the custom headers are never in the sent headers.

I was expecting the HTTP headers to be in the request.

0

There are 0 best solutions below