test

Page 186

ChainStream accepts a single parameter of type Stream and returns a parameter of type Stream. The first time ChainStream is called, the inbound stream is passed by the ASP.NET runtime and ChainStream is responsible for returning the outbound stream. The second time ChainStream is called, the outbound stream is passed by the ASP.NET runtime and ChainStream is responsible for returning the inbound stream. To keep my code as straightforward as possible, I use the postSerializeHandlers member variable to signal whether ChainStream is being called for the first time or the second time. I use an if/else statement to ensure that inboundStream and outboundStream are always set appropriately. The first time ChainStream is called, it needs to return a readable stream that contains the SOAP message that will be deserialized by the runtime. Because SOAP extensions often modify the contents of the stream, they often return a new instance of the MemoryStream class. If ChainStream creates a new stream, it becomes the outbound stream. The second time ChainStream is called, it needs to return a read/writable stream to the ASP.NET runtime. The ASP.NET runtime will use this stream to communicate the current contents of the message to the SOAP extension. Before the BeforeSerialization stage of ProcessMessage is called, the ASP.NET runtime will populate the stream with the contents of the SOAP message that was returned by the previously called SOAP extension. When ProcessMessage is finally called, the inbound stream can then be read by the SOAP extension, the message can be modified, and finally the new message can be written to the outbound stream. Therefore, the second time ChainStream is called, it needs to create a new stream for the inbound stream. There is one more caveat. The ChainStream method cannot modify the stream it receives from the ASP.NET runtime. The received stream can be modified only by the ProcessMessage method. If ChainStream does access any properties or methods of the stream received from ASP.NET, a runtime exception will occur. public override void ProcessMessage(SoapMessage message) { switch (message.Stage) { case SoapMessageStage.BeforeDeserialize: CopyStream(inboundStream, outboundStream); break;

case SoapMessageStage.AfterDeserialize: postSerializeHandlers = true; break; case SoapMessageStage.BeforeSerialize: break; case SoapMessageStage.AfterSerialize: WriteTraceLogEntry("Response"); CopyStream(inboundStream, outboundStream);

186


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.