|
|
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-102 | 在线联系 |
CIO一职:升迁热门?绝路一条? 一直以来,CIO被认为是企业信息化建设的灵魂人物。但在最近,业界对CIO的看法有了改变。一是部分企业渊如英国电信,就完全取消了CIO岗位;另外,Gartner在一份调查报告中也指出,全球约有2/3的C.. BlackBerry是由RIM公司开发的领先的无线解决方案,可以让移动专业人员随时随地与他人和信息保持连接。BlackBerry不只是智能手机,集成了电话、电子邮件、个人信息管理、互联网等服务,并具有极.. |
|
|