在 WCF REST 服务中记录请求/响应

发布于 2024-08-09 17:44:43 字数 148 浏览 4 评论 0原文

我正在寻找一种在 WCF REST 服务中记录请求和响应的方法。 WCF REST 入门工具包附带了一个 RequestInterceptor 类,可用于拦截请求,但似乎没有对应的响应类。理想情况下,我希望能够在通过网络发送响应之前拦截响应,例如当底层服务方法返回时。有什么建议吗?

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, e.g. when the underlying service method returns. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风铃鹿 2024-08-16 17:44:43

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

Notice that if you want to intercept the raw message, and not the parameters, you can inject your implementation of IDispatchMessageInspector instead of the IParameterInspector extension point that Dani suggests.

深海不蓝 2024-08-16 17:44:43

WCF中有一个技术:
您创建从 Attribute IOperationBehavior 派生的 InstrumentedOperationAttribute。

在你内部实现:

public void ApplyDispatchBehavior(

   OperationDescription operationDescription,

   DispatchOperation dispatchOperation

   )
{

  dispatchOperation.ParameterInspectors.Add(

     new ServerPI()

     );

}

ServerPI() 类的神奇之处在于:
您可以在 beforecall 和 aftercall 方法中执行所需的所有操作:

  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();
    }

  }

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();
    }

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文