简体   繁体   English

在MSMQ端点上使用消息检查器记录WCF请求和响应

[英]Logging WCF Requests and Responses using Message Inspectors on an MSMQ Endpoint

I'm working on a WCF service layer, which utilizes message inspectors for logging request and reply soap messages. 我正在WCF服务层上工作,该服务层利用消息检查器记录请求和回复肥皂消息。

Here's the inspector, stripped of irrelevant code; 这是检查员,除去了不相关的代码;

public class ServiceInspectorBehavior : Attribute, IDispatchMessageInspector, IServiceBehavior
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        // pass request to BeforeSendReply method
        return request.ToString();
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        string requestMessage = (string)correlationState;
        string responseMessage = reply.ToString();
        LogManager.Log(request, response);
    }
}

It was all fine and dandy, until a new business requirement came along, which will work best with MSMQ , so I'm in the process of implementing an MSMQ endpoint. 一切顺利,直到提出新的业务需求为止,这将最适合与MSMQ配合使用,因此我正在实现MSMQ端点。 The problem here is that the messaging is one-way. 这里的问题是消息传递是单向的。

[OperationContract(Name = "EnqueueRequest", IsOneWay = true)]

Even if I build a response object, the 'reply' will be null in the inspector's BeforeSendReply method, as none would be sent back to the client. 即使我构建了一个响应对象,在检查器的BeforeSendReply方法中,“ reply”也将为null,因为不会将任何内容发送回客户端。

The only thing that seems plausible at the moment, is to move logging from message inspectors, into my message processor logic, where I handle the request, and build the response. 目前看来,似乎唯一可行的事情是将日志记录从消息检查器移到我的消息处理器逻辑中,由我在其中处理请求并构建响应。 I still prefer to keep my current setup if possible though. 如果可能的话,我仍然更喜欢保持当前设置。

The question is, although seemingly unlikely, "Is it possible to have a reply message in the inspector's BeforeSendReply method parameters, using MSMQ?". 问题是,尽管看似不太可能,但“使用MSMQ是否可能在检查器的BeforeSendReply方法参数中出现回复消息?”。

Thanks in advance. 提前致谢。 Regards. 问候。


edit 2015.08.27 编辑2015.08.27

I've ditched logging using Message Inspector. 我放弃使用Message Inspector进行日志记录。 Now logging in my service layer. 现在登录我的服务层。 Here's what I've done. 这就是我所做的。 I'm still open to suggestions. 我仍然愿意接受建议。

public ResponseBase ProcessRequest(RequestBase requestObject)
{
    string requestString = string.Empty;
    string responseString = string.Empty;

    try
    {
        requestString = DataContractSerializerHelper.SerializeObject(requestObject);

        // ...
        // Do magic stuff, build a response object, etc
        // ...

        responseString = DataContractSerializerHelper.SerializeObject(responseObject);
        return responseObject;
    }
    catch(Exception e)
    {
        // Handle/log exception, return a default response, etc
    }
    finally
    {
        Logger.Log(requestString, responseString, ...);
    }
}

Is it possible to have a reply message in the inspector's BeforeSendReply method parameters, using MSMQ? 使用MSMQ是否可以在检查器的BeforeSendReply方法参数中包含回复消息?

No it is not. 不它不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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