【发布时间】:2019-05-18 19:05:43
【问题描述】:
我的 AfterRecieveRequest 方法生成一个 GUID,并通过correlationState 变量将其传递给 BeforeSendReply 方法。
在这两个调用之间,我的 WCF 服务中发生了很多事情,我想从 Web 服务方法中访问这个 GUID。有没有办法让我在整个 WCF 服务中访问这个对象?
GUID 用于记录目的,因为我正在调用不同应用程序的 API 并希望记录结果,并将它们记录在 IDispatchMessageInspector 实现中生成的 GUID 下。
例如:
IDispatchMessageInspector 实现:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
var correlationState = GUID.NewGuid();
StaticLogger.AddLog(originalMessage, correlationState.ToString(), "WCF-Incoming");
return correlationState;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
StaticLogger.AddLog(originalMessage, correlationState.ToString(), "WCF-Outgoing");
}
WCF 服务:
public ResponseObject WCFMethod(string param1, string param2)
{
ResponseObject response = new ResponseObject();
Client client = new Client();
ClientObject result = client.OutsideOperation(param1,param2);
// here, i would like to log the result object and use the GUID from the correlationstate
StaticLogger.AddLog(result, correlationState.ToString(), WCF-Internal )
response.resultAttribute = result;
return response;
}
我将如何继续完成这项工作?我一直在考虑使用 ThreadStatic 属性,以便线程将 GUID 保存在内存中的某个位置,但恐怕我对这个主题的理解不足以现在实现它。
【问题讨论】:
-
不言而喻,我已尽力隐藏不相关的代码,但如果我出于某种原因遗漏了您需要帮助我的内容,请告诉我。
-
您将使用“当前”对象并将correlationState(或您希望在逻辑请求期间可用的任何内容)存储在其中。有关示例,请参见 this answer。
标签: c# .net wcf idispatchmessageinspector