
.NET DEVELOPER
More on SOAP extensions
Bob Tabor 10.21.2002
Rating: -4.67- (out of 5)




|
In a previous tip, I explained how SOAP Extensions allow you to plug in and capture/manipulate the SOAP message in ASP.NET as it travels through the ASP.NET Web service chain of events -- from IIS to the ASP.NET ISAPI extension, through the HTTP modules down to the HTTP Handler. I also explained how there are four events (BeforeDeserialize, AfterDeserialize, BeforeSerialize and AfterSerialize) that allow you to inspect or modify the SOAP request message before/after it is deserialized into an object, and before/after the response object is turned back into a SOAP message. Now, I give you the code for the SoapLogger utility. This source code uses some pretty advanced features of .NET, such as the creation of custom attributes, manipulation of memory and file streams, and inheriting and overriding .NET Framework classes and methods (respectively) to implement your own functionality. To use this example, do the following:
- Create a new C# class library project called "SoapLogger".
- Download the source code and paste it into the Class1.cs file that was provided by Visual Studio.NET when you created the new project. Be sure to remove any existing code from that file before pasting this code into it.
- Compile the project. It will create a new assembly in the bin directory of your project called SoapLogger.dll
- Open the Web services project you want to add logging to. Open the actual ASMX file (or rather, its code behind file .cs or .vb).
- Add a reference to the SoapLogger.dll by right-clicking the project name in the solution explorer and selecting "Add reference..." and the Add Reference dialog will appear.
- Find your SoapLogger assembly in the list (or use the browse feature) and create a reference to the component.
- Add an attribute to the Web Method you want to log. For example, I used the following code in the method signature to log the results of the UpdateAffiliateDetail Web Method:
(Notice the underscores in VB.NET!)
Alternatively, you can send in a location if you don't like the default location and name using a property of the SoapLogger attribute:
- Compile and test your Web service. Once you do, you will see a log file that contains the SOAP messages for each time your Web Method is called.
Here's the C# code:
Notice that the SoapLogger class derives from the SoapExtension class. This is how you "plug into" the architecture of the HTTP Handler for Web services. Then, you must override several methods, such as Initialize, GetInitializer (allows you to retrieve the properties of the attribute, or set default values), ChainStream (allows you to copy off the memory stream for processing) and most importantly the ProcessMessage method, that allows you to determine which stage the SOAP request/response is currently in. If you take a look in the example above in the ProcessMessage method, I only test for two ...
To continue reading for free, register below or login
To read more you must become a member of SearchSOA.com
');
// -->

SoapMessageStages -- BeforeDeserialize and AfterSerialize. Why is that? Because this is when the message is actually a SOAP message, and is not in its object form.
You can extend this sample and build all sorts of inspection and logging into your Web service applications. This should give you plenty of meat to cut your teeth on. Until next time...
About the Author
[IMAGE]Robert Tabor is a Microsoft Certified Professional in Visual Basic with
over six years of experience developing n-tier Microsoft-centric applications for some of the world's
most prestigious companies and consulting organizations, such as Ernst & Young, KPMG, Cambridge
Technology Partners, Sprint, American Heart Association, and the Mary Kay Corporation. Bob is the
author of Microsoft .NET XML Web services by Sams Publishing, and
contributes to
SoapWebservices.com and LearnVisualStudio.NET. He is currently working on
initiatives within Mary Kay, the second largest eCommerce site in retail volume on the net, of how to
utilize .NET within their e-business group.
 |

|
|
 |
|
 |