Inspect #SOAP XML Messages in #VB.NET
When you call a webservice in .NET (Either C# or VB.NET), you don’t really get to see exactly what XML is being sent down the wire. This might not matter, but if something is not working as it should, then you might need to look at the exact XML being sent, to tweak the reference.cs file accordingly.
There’s plenty of code on the web that shows how to do this in C#, but I needed to port this code to VB.NET; so here it is:
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Description
Imports System.ServiceModel.Dispatcher
”’ <summary>
”’ WCFLogger: Use like “ws.Endpoint.Behaviors.Add(new InspectorBehavior())”
”’ http://stackoverflow.com/questions/5493639/how-do-i-get-the-xml-soap-request-of-an-wcf-web-service-request
”’ </summary>
Public Class WcfLogger
Implements IClientMessageInspector
Public Sub AfterReceiveReply(ByRef reply As Message, _
correlationState As Object) Implements IClientMessageInspector.AfterReceiveReply
Debug.WriteLine(reply)
End SubPublic Function BeforeSendRequest(ByRef request As Message, _
channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
Debug.WriteLine(request)
Return Nothing
End Function
End Class”’ <summary>
”’
”’ </summary>
Public Class InspectorBehavior
Implements IEndpointBehavior
”’ <summary>
”’ Implement to confirm that the endpoint meets some intended criteria.
”’ </summary>
”’ <param name=”endpoint”>The endpoint to validate.</param>
Public Sub Validate(endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
End Sub”’ <summary>
”’ Implement to pass data at runtime to bindings to support custom behavior.
”’ </summary>
”’ <param name=”endpoint”>The endpoint to modify.</param>
”’ <param name=”bindingParameters”>The objects that binding elements require to support the behavior.</param>
Public Sub AddBindingParameters(endpoint As ServiceEndpoint, _
bindingParameters As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
End Sub”’ <summary>
”’ Implements a modification or extension of the service across an endpoint.
”’ </summary>
”’ <param name=”endpoint”>The endpoint that exposes the contract.</param>
”’ <param name=”endpointDispatcher”>The endpoint dispatcher to be modified or extended.</param>
Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, _
endpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
End Sub”’ <summary>
”’ Implements a modification or extension of the client across an endpoint.
”’ </summary>
”’ <param name=”endpoint”>The endpoint that is to be customized.</param>
”’ <param name=”clientRuntime”>The client runtime to be customized.</param>
Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, _
clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
clientRuntime.MessageInspectors.Add(New WCFLogger())
End Sub
End Class