BoostPLM
BoostPLM

Reputation: 21

Extract SOAP body as XML from WCF (vb.net)

I am to build a client in vb.net that is to contact a JAX-WS web-service.

I intend to use the WCF framework, since this simplified the security setup.

The request is also built in the standard way, however I want to extract the body response directly to an XML (stream) as I need to transform it by an xlst stylesheet. Furthermore, the response from the web-service will change over time and I do not want to do new builds every time a new property is added to the web-service, but only handle this in the stylesheet.

I have googled for an example, especially looked into these articles :

Custom Message Handling with WCF 1

Custom MEssage Handling with WCF 2

However, it is not clear to me how it works. From other articles I see that the message class seems to play an important role, but I do not see how I can get hold of that object through the WCF.

My competences in C# are limited so an example in vb.net would be appreciated.

Any help?

Upvotes: 1

Views: 824

Answers (1)

BoostPLM
BoostPLM

Reputation: 21

I ended up (as suggested) using the IMessageInspector. I did this by adding the following code to the auto-generated Reference.vb under the Service Reference:

Public Class WindchillResponseInspector 'System.ServiceModel.Description.IEndpointBehavior
    Implements IClientMessageInspector


    Public Sub AfterReceiveReply(ByRef reply As Message, correlationState As Object) Implements IClientMessageInspector.AfterReceiveReply


        Dim buffer As MessageBuffer = reply.CreateBufferedCopy(Integer.MaxValue)
        Dim replycopy As Message = buffer.CreateMessage()
        reply = buffer.CreateMessage

        ResponseStream = New IO.MemoryStream

        'Dim stream As New FileStream("myfile.xml", FileMode.Create)
        Dim xdw As Xml.XmlDictionaryWriter = Xml.XmlDictionaryWriter.CreateTextWriter(ResponseStream)
        '    reply.WriteMessage(xdw)
        replycopy.WriteBodyContents(xdw)
        xdw.Flush()
        'printStream(ResponseStream)
    End Sub

    Public Function BeforeSendRequest(ByRef request As Message, channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
        Dim buffer As MessageBuffer = request.CreateBufferedCopy(Integer.MaxValue)
        Dim requestcopy As Message = buffer.CreateMessage()
        request = buffer.CreateMessage

        Dim requestStream = New IO.MemoryStream

        'Dim stream As New FileStream("myfile.xml", FileMode.Create)
        Dim xdw As Xml.XmlDictionaryWriter = Xml.XmlDictionaryWriter.CreateTextWriter(requestStream)
        '    reply.WriteMessage(xdw)
        requestcopy.WriteMessage(xdw)
        xdw.Flush()
        printStream(requestStream)
        Return Nothing
    End Function

End Class

In order to have this code working, I had to add it to the behavior:

Public Class WindchillInspectorBehavior
    Implements IEndpointBehavior

    Public Sub AddBindingParameters(endpoint As ServiceEndpoint, bindingParameters As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters

    End Sub

    Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
        Dim inspector As New WindchillResponseInspector
        clientRuntime.ClientMessageInspectors.Add(inspector)

    End Sub

    Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, endpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior

    End Sub

    Public Sub Validate(endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate

    End Sub
End Class

Upvotes: 1

Related Questions