0
我正在使用SoapExtension來記錄web服務請求,在web服務中發生錯誤時的響應。請求和響應XMLDocument的數據都不正確。有人可以查看代碼,看看我可能做錯了什麼嗎?如果我無法集中SoapExtension中的更改,那麼我將圍繞每個Web服務端點添加日誌記錄代碼。記錄兩個webservice請求,在SoapExtension中的響應
我正在使用SoapExtension來記錄web服務請求,在web服務中發生錯誤時的響應。請求和響應XMLDocument的數據都不正確。有人可以查看代碼,看看我可能做錯了什麼嗎?如果我無法集中SoapExtension中的更改,那麼我將圍繞每個Web服務端點添加日誌記錄代碼。記錄兩個webservice請求,在SoapExtension中的響應
通過刪除XMLRequest,XMLResponse屬性的「Static」屬性來解決此問題。此外,我將在「SoapMessageStage.AfterSerialize」過程中獲取「XMLResponse」,在「SoapMessageStage.BeforeSerialize」過程中獲取「XMLRequest」。我以前已經切換了。
using System;
using System.IO;
using System.Web.Services.Protocols;
using System.Xml;
using Elmah;
public class ElmahSoapExtensionV2 : SoapExtension
{
private Stream oldStream;
private Stream newStream;
public XmlDocument XmlRequest { get; private set; }
public XmlDocument XmlResponse { get; private set; }
public override Stream ChainStream(Stream stream)
{
oldStream = stream;
newStream = new MemoryStream();
return newStream;
}
public override void ProcessMessage(SoapMessage soapMessage)
{
switch (soapMessage.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
XmlResponse = GetSoapEnvelope(newStream);
if (soapMessage.Exception != null)
{
var exception = soapMessage.Exception.GetBaseException();
exception.Data.Add("SoapRequest", XmlRequest.OuterXml);
exception.Data.Add("SoapResponse", XmlResponse.OuterXml);
ErrorSignal.FromCurrentContext().Raise(exception);
}
CopyStream(newStream, oldStream);
break;
case SoapMessageStage.BeforeDeserialize:
CopyStream(oldStream, newStream);
XmlRequest = GetSoapEnvelope(newStream);
break;
case SoapMessageStage.AfterDeserialize:
break;
}
}
private static XmlDocument GetSoapEnvelope(Stream stream)
{
var xmlDocument = new XmlDocument();
stream.Position = 0;
var streamReader = new StreamReader(stream);
xmlDocument.LoadXml(streamReader.ReadToEnd());
stream.Position = 0;
return xmlDocument;
}
private static void CopyStream(Stream streamFrom, Stream streamTo)
{
var textReader = new StreamReader(streamFrom);
var textWriter = new StreamWriter(streamTo);
textWriter.WriteLine(textReader.ReadToEnd());
textWriter.Flush();
}
#region MyRegion
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return null;
}
public override object GetInitializer(Type WebServiceType)
{
return null;
}
public override void Initialize(object initializer)
{
}
#endregion
}