Listato di codice C# per la libreria di classi di esempio di traccia SQL SOAP
Questa caratteristica verrà rimossa a partire da una delle prossime versioni di Microsoft SQL Server. Evitare di utilizzare questa funzionalità in un nuovo progetto di sviluppo e prevedere interventi di modifica nelle applicazioni in cui è attualmente implementata.
[!NOTA]
Per istruzioni su come includere il codice per la libreria di esempio seguente nel progetto Visual Studio, vedere Aggiunta di supporto per la traccia SOAP nelle applicazioni client.
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;
}