다음을 통해 공유


SQL SOAP 추적 예제 클래스 라이브러리에 대한 C# 코드 목록

[!참고] Visual Studio 프로젝트에 다음 예제 라이브러리 코드를 포함하는 방법은 클라이언트 응용 프로그램에 SOAP 추적 지원 추가를 참조하십시오.

using System;
using System.IO;
using System.Web.Services.Protocols;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

class info 
{
    public void Init() 
    {
        m_request = null;
        m_requestException = null;
        m_response = null;
        m_responseException = null;
    }

    public string request 
    {
        get { return m_request; }
        set { m_request = value; }
    }

    public Exception requestException
    {
        get { return m_requestException; }
        set { m_requestException = value; }
    }

    public string response
    {
        get { return m_response; }
        set { m_response = value; }
    }

    public Exception responseException
    {
        get { return m_responseException; }
        set { m_responseException = value; }
    }

    private string m_request = null;
    private Exception m_requestException = null;
    private string m_response = null;
    private Exception m_responseException = null;
}

class snoop : SoapExtension 
{
    public override object GetInitializer(LogicalMethodInfo lmi, SoapExtensionAttribute attr) 
    {
        return getInfo();
    }
    public override object GetInitializer(Type type)
    {
        throw new NotImplementedException("snoop.GetInitializer is not implemented.");
    }
    public override void Initialize(object initializer)
    {
        m_info = (info) initializer;
        m_info.Init();
    }
    public override void ProcessMessage(SoapMessage message) 
    {
        SoapClientMessage client = (SoapClientMessage) message;
        switch (client.Stage) 
        {
            case SoapMessageStage.BeforeSerialize:
                break;
            case SoapMessageStage.AfterSerialize:
                try 
                {
                    m_newStream.Position = 0;
                    StreamReader reader = new StreamReader(m_newStream);
                    m_info.request = FormatXml(reader.ReadToEnd());
                    m_newStream.Position = 0;
                    m_newStream.SetLength(0);
                    StreamWriter writer = new StreamWriter(m_newStream);
                    writer.Write(m_info.request);
                    writer.Flush();
                    Clipboard.SetDataObject(m_info.request+"\r\n",true);
                }
                catch (Exception e) 
                {
                    m_info.requestException = e;
                }
                m_newStream.Position = 0;
                Copy(m_newStream,m_oldStream);
                break;
            case SoapMessageStage.BeforeDeserialize:
                m_newStream.SetLength(0);
                try 
                {
                    Copy(m_oldStream,m_newStream);
                    m_newStream.Position = 0;
                    StreamReader reader = new StreamReader(m_newStream);
                    string tempData = reader.ReadToEnd();
                    m_info.response = FormatXml(tempData);
                    m_newStream.Position = 0;
                    m_newStream.SetLength(0);
                
                    StreamWriter writer = new StreamWriter(m_newStream);
                    writer.Write(tempData);
                    writer.Flush();
                    Clipboard.SetDataObject(m_info.request+"\r\n\r\n"+m_info.response+"\r\n",true);
                }
                catch (Exception e) 
                {
                    m_info.responseException = e;
                }
                m_newStream.Position = 0;
                break;
            case SoapMessageStage.AfterDeserialize:
                break;
            default:
                throw new Exception("invalid stage");
        }
    }
    public override Stream ChainStream(Stream stream)
    {
        m_oldStream = stream;
        m_newStream = new MemoryStream();
        return m_newStream;
    }
    private void Copy(Stream from, Stream to) 
    {
        TextReader reader = new StreamReader(from);
        TextWriter writer = new StreamWriter(to);
        string data = reader.ReadToEnd();
        writer.Write(data);
        writer.Flush();
    }
    private string FormatXml(string xml) 
    {
        XmlDocument inDoc = new XmlDocument();
        inDoc.PreserveWhitespace = false;
        inDoc.LoadXml(xml);
        StringWriter writer = new StringWriter();
        XmlTextWriter xmlWriter = new XmlTextWriter(writer);
        xmlWriter.Formatting = Formatting.Indented;
        inDoc.WriteContentTo(xmlWriter);
        xmlWriter.Flush();
        return writer.ToString();
    }
    private info m_info = null;
    private Stream m_oldStream = null;
    private Stream m_newStream = null;
    //private Stream m_tempStream = null;

        public static info getInfo() 
    {
        object res = Thread.GetData(m_slot);
        if (res == null) 
        {
            res = new info();
            Thread.SetData(m_slot,res);
        }
        return (info) res;
    }
    private static LocalDataStoreSlot m_slot = Thread.AllocateDataSlot();
}
[AttributeUsage(AttributeTargets.Method)]
class snoopattribute : SoapExtensionAttribute 
{
    public override Type ExtensionType
    {
        get { return typeof(snoop); }
    }
    public override int Priority 
    {
        get { return m_priority; }
        set { m_priority = value; }
    }
    private int m_priority = 0;
}

참고 항목

참조

Visual Studio 2005 클라이언트 응용 프로그램 작성
클라이언트 응용 프로그램에 SOAP 추적 지원 추가

도움말 및 정보

SQL Server 2005 지원 받기