Tuesday, April 17, 2007

Setting SoapExtensions Programmatically

Creating a class that inherits from SoapExtension can provide (among other things) an easy way of logging XML that is passed between a client and server during Web Service calls. To apply your SoapExtension to an entire Web Service, then you can make changes in the Web.config file.

Likewise, if you are running a client application then you can add a section to your app.Config file.

However, if you are connecting to a Web Service from a class library then how do you get your logging?

I've just had this problem. We have code running as a COM+ server, so we can't add the setting to the app.Config file. I suppose if we wanted to we could add it to the dllhost.exe.config file, or mess around with the Application Root Directory, but for a number of reasons these aren't really an option.

But we can set SoapExtensions programmatically in our code:

  1. Create the SoapExtension class you want to use
  2. Create a SoapExtensionAttribute class, overriding the ExtensionType property
  3. Apply the attribute to the method.
Check out this blog for full code samples

No comments: