|
Fun with SOAP Extensions广告 Fun with SOAP Extensions
March 22, 2001 Download or browse the VBSoapEx.exe in the MSDN Online Code Center. Note As promised, in this month's column we're going
to look at one of the more advanced, but cooler features of ASP.NET Web
Services—SOAP Extensions. For this month's column Keith Ballinger, a Program
Manager for .NET Web Services, has offered to share some of his knowledge of
this subject. Thanks and see you next month. -Rob SOAP Extensions allow developers to create very interesting applications on top of the core SOAP architecture found within .NET. For instance, you can implement an encryption algorithm on top of the Web Service call. Alternatively, you could implement a compression routine, or even create a SOAP Extension that will accept SOAP Attachments. How does this work? It’s easy. First, I'd recommend that you review Rob Howard’s earlier article Web Services with ASP.NET and then come back. You may also want to read SOAP in the Microsoft .NET Framework and Visual Studio .NET. Basically, you need to do two things: Create a class that derives from
System.Web.Services.Protocols.SoapExtension For this column, we will create an extension that records incoming and outgoing SOAP messages to our Web Service. This trace extension is useful for debugging when you really care about getting the SOAP message to look exactly the way you want it to look. The core piece of implementation you need to worry about is the ProcessMessage method. This method is called several times, and every time it sends a SoapMessage object, it includes information on the stage of the SOAP message. To illustrate, let’s examine the flow of a SOAP message within a Web Service. All of this applies on the client as well as the server, but we will concentrate on the server for this example. A SOAP message comes in and the server figures out which method
to route to. public void WriteInput( SoapMessage message ){ MemoryStream m = new MemoryStream(); m.Seek(0,
SeekOrigin.Begin); Notice we also write it out to a MemoryStream so that the SoapMessage object still has a valid stream when we are done. If we don’t do this, the server will try to deserialize the network stream that was originally on the SoapMessage, and find nothing there. A similar process takes place when we log the SOAP message that the server will return to the client. However, there is one exception. In the BeforeSerialize stage, we need to swap out the network stream on the SoapMessage, and replace it with one of our own MemoryStreams. This slight of hand will allow us to record whatever is sent as SOAP, without immediately losing it in the network. public void SetStream( SoapMessage message
){ public void WriteOutput( SoapMessage message ){ newStream.Seek(0,
SeekOrigin.Begin); Notice that we record whatever was written to the memory stream, and then write it out to the network stream once we are done. If we don’t write it out to the stream we stole, the client will never get a response. Here is the full trace extension for you to use and play with: <%@ WebService Language="c#" Class="TestIt" %> using System; public class TestIt{ [WebMethod] public class Address { [AttributeUsage(AttributeTargets.Method)] public override Type ExtensionType
{ public class TraceExtension : SoapExtension { Stream oldStream; public override int GetPriority()
{ public override object
GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{ public override void Initialize(object
initializer) { public override void
ProcessMessage(SoapMessage message)
{ case
SoapMessageStage.BeforeSerialize: case
SoapMessageStage.AfterSerialize: case
SoapMessageStage.BeforeDeserialize: case
SoapMessageStage.AfterDeserialize:
default: public void SetStream( SoapMessage message
){ public void WriteOutput( SoapMessage message ){ newStream.Seek(0,
SeekOrigin.Begin); public void WriteInput( SoapMessage message ){ MemoryStream m = new
MemoryStream(); m.Seek(0,
SeekOrigin.Begin); m.Seek(0,
SeekOrigin.Begin); void Copy(Stream from, Stream to) { TextReader reader =
new StreamReader(from); So there you have it—a trace extension. SOAP Extensions are a very useful feature, and I hope to see a lot imaginative uses for them over the next few years. There are some slight, but fundamental changes to the way extensions work in the upcoming beta 2 of the .NET Framework, but don’t worry, we'll update this code (which is for beta 1) with beta 2 code when it comes out. Sample binaries are available on the MSDN CDs 如果您希望与本文章的作者或其所在机构,进一步交流,请联系:畅享网 姜小姐 jill.jiang@amteam.org | 021-51096826-112 | 在线联系 |
节能与优化IT 企业CIO过冬良策当前金融危机的影响还在继续漫延,很多企业都在苦寻过冬的良策,在这种情况下,节能与优化技术与产品无疑成为CIO们关注的首要对象,本次选题就是针对节能与优化IT来为CIO们提供过冬的良…… |
|
|