在SoapExtension中记录Web服务请求,响应

时间:2013-07-11 05:05:10

标签: asp.net .net web-services asmx soap-extension

我正在使用SoapExtension来记录Web服务请求,以及在Web服务中发生错误时的响应。请求和响应XMLDocument都有不正确的数据。有人可以查看代码,看看我做错了什么吗?如果我无法集中SoapExtension中的更改,那么我将在每个Web服务端点周围添加日志记录代码。

    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 static XmlDocument XmlRequest { get; private set; }
        public static 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:
                    XmlRequest = 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);
                    XmlResponse = 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
    }

1 个答案:

答案 0 :(得分:1)

通过删除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
}