Intercept messages in a WCF Client

bob picture bob · Aug 1, 2009 · Viewed 24.8k times · Source

Has anyone got any experience with Web Service Extensions? I spent time trying to make a web service extension from the MS examples.

I have an .net 3.5 web service client, built by adding a reference to the WSDL, via the VS IDE "Project > Add Service Reference". This built my web service client, and all works OK.

I need to intercept the request and response body for my web service client. I have found lots of references to Web Service Extensions, but am having an attack of the tired, and just can't get my extensions to fire.

I've used the MS example from here "How to implement a SOAP extension" ( http://msdn.microsoft.com/en-us/library/7w06t139.aspx) , which builds a logger for the request / response streams.

The related MS article "Soap Message Modification" (http://msdn.microsoft.com/en-us/library/esw638yk(VS.85).aspx) shows how to enable the SOAP extension for the web client:

Implementing the SOAP Extension

There are two ways to run a SOAP extension on either a client or server application. First, you can configure the application to run the extension. To configure your SOAP extension to run for all Web methods on all Web services, especially a vroot, edit the <soapExtensionTypes> Element section within the Web.config file. The following code shows that the type attribute value must be on one line and include the fully qualified name of the extension, plus the version, culture, and public key token of the signed assembly.

<configuration>
<system.web>
<webServices>
<soapExtensionTypes>
<add type="Contoso.MySoapExtension, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"/>
</soapExtensionTypes>
</webServices>
</system.web>
</configuration>

I've compiled the traceextension into its own class library, and referenced it in the web.config of the web service project like so:

<add type="TraceExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef8757fac167b8d8" priority="1" group="High"/>

No joy. Nothing is logged, no breakpoints are hit.

I then removed the referenced class, and dropped the source code into the web service project.

I tried to add a reference to it like so (my namespace is ServcieTest001):

<add type="ServiceTest001.TraceExtension" group="High" priority="1" />

I used the following thread as a guide as to enabling me extension "getting-raw-soap-data-from-a-web-reference-client-running-in-asp-net" (http://stackoverflow.com/questions/300674/getting-raw-soap-data-from-a-web-reference-client-running-in-asp-net).

Still no joy. I then copied the code from the above thread, and still cannot get the extension to fire when I make a SOAP request.

Can anyone point me to a functioning downloadable web service extension demo project, so I can disassemble it and work out what I'm missing?

Answer

Zach Bonham picture Zach Bonham · Aug 2, 2009

John is right, you can intercept the messages on the client using a custom client behavior that implements IClientMessageInspector. See How To: Inspect or Modify Messages on the Client on MSDN.

The only thing 'tricky' about it is that if you plan on modifying the message body then you will need to create a copy of the original message first. See Using the Message Class for the gooey details.