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

C# 在MSMQ端点上使用消息检查器记录WCF请求和响应,c#,wcf,msmq,msmq-wcf,idispatchmessageinspector,C#,Wcf,Msmq,Msmq Wcf,Idispatchmessageinspector,我正在研究一个WCF服务层,它利用日志记录请求和回复soap消息 这是检查员,去除了不相关的代码 public class ServiceInspectorBehavior : Attribute, IDispatchMessageInspector, IServiceBehavior { public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext insta

我正在研究一个WCF服务层,它利用日志记录请求和回复soap消息

这是检查员,去除了不相关的代码

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);
    }
}
在新的业务需求出现之前,一切都很顺利,这将是最适合的,因此我正在实现MSMQ端点。这里的问题是消息传递是单向的

[OperationContract(Name = "EnqueueRequest", IsOneWay = true)]
即使我构建了一个响应对象,“reply”在inspector的BeforeSendReply方法中也是空的,因为没有一个会被发送回客户端

目前看来唯一可行的事情是将日志记录从消息检查器转移到我的消息处理器逻辑中,在那里我处理请求并构建响应。如果可能的话,我仍然希望保留当前设置

问题是,尽管看起来不太可能,“是否可以使用MSMQ在检查器的BeforeSendReply方法参数中包含回复消息?”

提前谢谢。 问候


编辑2015.08.27

我放弃了使用消息检查器进行日志记录。现在登录我的服务层。这就是我所做的。我仍然愿意接受建议

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, ...);
    }
}
是否可以在检查员的电子邮件中发送回复消息 BeforeSendReply方法参数,是否使用MSMQ

不,不是