Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在从IPParameterInspector获取MessageId_C#_Wcf_Logging_Serialization - Fatal编程技术网

C# 正在从IPParameterInspector获取MessageId

C# 正在从IPParameterInspector获取MessageId,c#,wcf,logging,serialization,C#,Wcf,Logging,Serialization,作为WCF服务的一部分,我希望使用请求的操作和提供的输入参数记录传入/传出呼叫 我还想在同一个日志行中记录请求的MessageId。IParameterInspector看起来非常适合于此,但是Message request对象的MessageId部分在此处不可用 如果我实现IDispatchMessageInspector,我可以访问Message对象,但不能访问输入参数/操作。从技术上讲,我可以访问这些参数/操作,但访问它们并不是件小事。我很想使用IParameterInspector的Be

作为WCF服务的一部分,我希望使用请求的操作和提供的输入参数记录传入/传出呼叫

我还想在同一个日志行中记录请求的MessageId。IParameterInspector看起来非常适合于此,但是Message request对象的MessageId部分在此处不可用

如果我实现IDispatchMessageInspector,我可以访问Message对象,但不能访问输入参数/操作。从技术上讲,我可以访问这些参数/操作,但访问它们并不是件小事。我很想使用IParameterInspector的BeforeCall功能,因为它不需要对消息的模式进行任何假设

我可能遗漏了一些非常简单的东西,但是在同一范围内获取MessageId和输入参数似乎并不容易


将BeforeCall和BeforeSendRequest相结合,或者至少在这两种方法之间传递数据,这将是完美的选择。

您可以在IParameterInspector内部使用类似的方法:

OperationContext.Current.IncomingMessageHeaders.MessageId
请参考链接

另一个选项是在afterRecieveRequest中添加messageproperty

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

Guid activityId = Guid.NewGuid();
            System.ServiceModel.OperationContext.Current.IncomingMessageProperties.Add("LOGID", activityId);
//log your messages here

return activityId 

}

public void BeforeSendReply(ref Message reply, object correlationState)
       {
            Guid activityId = (Guid)correlationState;
//Log your Message here..
}
在为请求提供服务的实际函数中,您可以访问此属性

 public String SayHello(String request)
            {
                object activityId;
                System.ServiceModel.OperationContext.Current.IncomingMessageProperties.TryGetValue("LOGID", out activityId);

    //log your messages here with this ID....

Return "Hello, World";
    }

我已经试过了,但不幸的是OperationContext.Current在BeforeCall的范围内为空。这很奇怪。它应该被设置好。您的配置是什么?可能是因为我正在本地运行客户端?我正在调试客户端->WCF。正在客户端上使用null OperationContext.Current属性调用BeforeCall。没有特殊的配置-事实上,它在AfterCall中也是空的…哦。经过一点调试后,如果在多线程环境中使用,则OperationContext.Current似乎不可用。我的客户机生成了多个访问服务器的线程,因此我看到空。我希望避免向服务实现本身添加代码,例如设置threadstatics,理想情况下处理端点行为中的所有内容,但这似乎并不简单:。如果直接创建服务类型的实例(而不是WCF proxy/clientchannel),然后对其调用方法,则不存在OperationContext。当您的操作在服务中运行时,WCF提供OperationContext实例。