Created
April 2, 2019 20:43
-
-
Save gBritz/4e19f0c3f5a11b4f8b0e5eac9b1480e7 to your computer and use it in GitHub Desktop.
Log xml soap webservice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Diagnostics; | |
| using System.IO; | |
| using System.Web.Services.Protocols; | |
| namespace SoapService.Client | |
| { | |
| public class LogXmlSoap : SoapExtension | |
| { | |
| private Stream oldStream; | |
| private Stream newStream; | |
| public override Stream ChainStream(Stream stream) | |
| { | |
| oldStream = stream; | |
| newStream = new MemoryStream(); | |
| return newStream; | |
| } | |
| public override Object GetInitializer(Type serviceType) | |
| { | |
| return null; | |
| } | |
| public override Object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute) | |
| { | |
| return null; | |
| } | |
| public override void Initialize(Object initializer) { } | |
| public override void ProcessMessage(SoapMessage message) | |
| { | |
| switch (message.Stage) | |
| { | |
| case SoapMessageStage.AfterDeserialize: | |
| break; | |
| case SoapMessageStage.AfterSerialize: | |
| var xmlRequest = GetSoapEnvelope(newStream); | |
| CopyStream(newStream, oldStream); | |
| Log(xmlRequest, message.Url); | |
| break; | |
| case SoapMessageStage.BeforeDeserialize: | |
| CopyStream(oldStream, newStream); | |
| var xmlResponse = GetSoapEnvelope(newStream); | |
| break; | |
| case SoapMessageStage.BeforeSerialize: | |
| message.Stream.Position = 0; | |
| break; | |
| } | |
| } | |
| private String GetSoapEnvelope(Stream stream) | |
| { | |
| stream.Position = 0; | |
| StreamReader reader = new StreamReader(stream); | |
| var result = reader.ReadToEnd(); | |
| stream.Position = 0; | |
| return result; | |
| } | |
| private void CopyStream(Stream from, Stream to) | |
| { | |
| TextReader reader = new StreamReader(from); | |
| TextWriter writer = new StreamWriter(to); | |
| writer.WriteLine(reader.ReadToEnd()); | |
| writer.Flush(); | |
| } | |
| public static void Log(String xmlSoap, String url) | |
| { | |
| Debug.WriteLine(string.Format("Url: {0}, xml: {1}", url, xmlSoap)); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <configuration> | |
| <system.web> | |
| <webServices> | |
| <soapExtensionTypes> | |
| <add type="SoapService.Client.LogXmlSoap, SoapService.Client" priority="1" group="0" /> | |
| </soapExtensionTypes> | |
| </webServices> | |
| </system.web> | |
| </configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment