Logging requests/responses in a WCF REST service

I'm looking for a way to log both requests and responses in a WCF REST service. The WCF REST starter kit comes with a RequestInterceptor class which can be used to intercept requests, but there does not seem to be an equivalent for responses. Ideally, I'd like to be able to intercept a response just before it's sent over the wire, eg when the underlying service method returns. Any suggestions?


请注意,如果您想拦截原始消息,而不是参数,则可以注入IDispatchMessageInspector的实现,而不是Dani建议的IParameterInspector扩展点。


There is a technic in WCF: you create InstrumentedOperationAttribute that derives from Attribute, IOperationBehavior.

Inside you implement:

public void ApplyDispatchBehavior(

   OperationDescription operationDescription,

   DispatchOperation dispatchOperation

   )
{

  dispatchOperation.ParameterInspectors.Add(

     new ServerPI()

     );

}

and the ServerPI() class is what does the magic: you do everything you need in beforecall and aftercall methods:

  class ServerPI : IParameterInspector
  {

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {
      Guid result = (Guid)correlationState;
      // ...
    }

    public object BeforeCall(string operationName, object[] inputs)
    {
      string parameter1 = inputs[0] as string;
      return Guid.NewGuid();
    }

  }
链接地址: http://www.djcxy.com/p/26894.html

上一篇: 在WCF RequestInterceptor ProcessRequest方法内部访问HttpContext

下一篇: 记录WCF REST服务中的请求/响应