简体   繁体   中英

How can I catch soap messages on WCF Web Service?

I have a wcf web services. I am logging errors on db. My error log table has fields like ErrorMessage, Date, LogLevel, etc. I need to log also soap messages. How can I catch response and request soap messages inside my service?

EDIT: I am using log4net library for logging.

If you want to “catch” and log the application messages at the “outermost” edge of your WCF service, then you should consider implementing an IDispatchMessageInspector which will enable custom inspection of inbound/outbound service messages.

The following MSDN link provides an overview of the IDispatchMessageInspector , which references your scenario:

Implement IDispatchMessageInspector to inspect or modify inbound or outbound application messages either prior to dispatching a request message to an operation or before returning a reply message to a caller. There are a large number of scenarios that require intercepting messages prior to invoking the operation for which it is destined. For example, you can log incoming application messages or perform some feature based on a message header.

http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector(v=vs.110).aspx

Alternatively, you could consider leveraging WCF Trace Logging with a custom database trace listener. In that way, to WCF will feed the trace data to the custom listener, which can insert into the database.

http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

In order to intercept all requests and responses in a reliable way you have to implement BehaviorExtensionElement I have answered on how to implement it in this thread

all what you can do is something like this

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
 {

       //here you can  create a buffered message as the original message can accessed only once 
           //MemoryStream stream = new MemoryStream();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = System.Text.Encoding.UTF8;
            StringWriter sw = new StringWriter();
            XmlWriter writer = XmlWriter.Create(sw, settings);





            MessageBuffer buffer = request.CreateBufferedCopy(int.MaxValue);
            //Create a copy of the message in order to continue the handling of te SOAP                 
            request = buffer.CreateMessage();
           request.WriteMessage(writer);
          //Recreate the message 
             writer.Flush();
            //Flush the contents of the writer so that the stream gets updated
             //you can log the str to the database 
            var str =  sw.ToString();

            request = buffer.CreateMessage();


  }

I'm pretty sure that you have to combine ASP.NET system diagnostic components and log4net, by creating your own listener that will use log4net manager.

In your web config, have a look at <system.diagnostics> section and <sharedListeners> . In my case we are logging the full request/response into a file.

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="d:\soap.svclog" />
    </sharedListeners>
  </system.diagnostics>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM