Scenario
I am consuming a web service published by a partner. I am implementing a .NET proxy client. I would like to serialize the XML related to a web method invocation. The aim is to store the request details for further processing/re-processing/logging.
I could shape the table in such a way that each parameter correspond to a field. But in this way the table would be strictly related to the web method signature. Instead I would like to create a general WebServiceClientRequest table that holds the XML body of the SOAP request.
Eventually I can always add a related table that maps each parameter.
Question
Is there a way to serialize the XML content of the SOAP request?
There is a way indeed. The idea is to use SOAP extensions, as described in the MSDN article: Walkthrough: Altering the SOAP Message Using SOAP Extensions .
Then I used a custom DAL manager to store the SOAP messages into the DB.
Here are the details.
public class SOAPTraceDB : SoapExtension
{
private Stream oldStream;
private Stream newStream;
private string ConnectionString;
private Int32 RequestId;
// Save the Stream representing the SOAP request or SOAP response into
// a local memory buffer.
public override Stream ChainStream(Stream stream)
{
oldStream = stream;
newStream = new MemoryStream();
return newStream;
}
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return null;
}
// The SOAP extension was configured to run using a configuration file
// instead of an attribute applied to a specific XML Web service
// method.
public override object GetInitializer(Type WebServiceType)
{
// Return a db table to log the trace information to, based on the type.
return MyConfigurationManager.GetAppSetting<string>("SOAPTraceLoggerConnectionString");
}
public override void Initialize(object initializer)
{
ConnectionString = (string)initializer;
}
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
WriteRequest(message);
break;
case SoapMessageStage.BeforeDeserialize:
WriteResponse(message);
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}
public void WriteRequest(SoapMessage message)
{
newStream.Position = 0;
Stream s = new MemoryStream();
Copy(newStream, s);
RequestId = MyDALStaticManager.WriteMessage(DateTime.Now, SOAPMessageType.request, StreamToString(s));
s.Close();
newStream.Position = 0;
Copy(newStream, oldStream);
}
public void WriteResponse(SoapMessage message)
{
Stream s = new MemoryStream();
Copy(oldStream, newStream);
newStream.Position = 0;
Copy(newStream, s);
MyDALStaticManager.WriteMessage(DateTime.Now, SOAPMessageType.response, StreamToString(s), RequestId);
newStream.Position = 0;
s.Close();
}
private string StreamToString(Stream s)
{
string res = string.Empty;
s.Position = 0;
StreamReader r = new StreamReader(s);
//SQL Server accept only utf-16 encoding. Is there a better way to set up the stream encoding? utf-16 is not featured among the System.Text.Encoding
res = r.ReadToEnd().Replace("encoding=\"utf-8\"", "encoding=\"utf-16\"");
r.Close();
return res;
}
private void Copy(Stream from, Stream to)
{
TextReader reader = new StreamReader(from);
TextWriter writer = new StreamWriter(to);
string str = reader.ReadToEnd();
str = XmlUtils.FormatXml(str);
writer.WriteLine(str);
writer.Flush();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.