idisPatchMessageInspector相关性state,并在两者之间访问它



我的Aftrecieverequest方法生成了一个GUID,并通过相关state变量将其传递给BeforesendReply方法。

在这两个电话之间,我的WCF服务发生了很多事情,我想从WebService方法中访问此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;
}

我将如何实现这一目标?我一直在考虑使用线程属性属性,因此线程将GUID保留在内存中的某个位置,但恐怕我对此主题的理解不足以立即实现。

如果您只想访问beforesendreply和Afterreceiverequest之间的GUID,您可以使用MessageProperties,执行服务时可以访问MessageProperties。

下面是我的测试。

  public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        var correlationState = Guid.NewGuid();
        request.Properties["myGuid"] = correlationState; // store guid in Properties
        return correlationState;
    }
    public void BeforeSendReply(ref Message reply, object correlationState)
    {
    }

然后您可以在服务中获得GUID。

 public class ServiceTest : Test
{
    public double add(double a, double b)
    {
         // get the property through IncomingMessageProperties property
        Guid gu = (Guid)OperationContext.Current.IncomingMessageProperties["myGuid"];
        Console.WriteLine(gu);
        return a + b;
    }
}

不确定这是您想要的,但是您可以将GUID添加到频道消息标题中。有人已经写了一些有用的信息:https://stackoverflow.com/a/1408177/2016162

相关内容

  • 没有找到相关文章

最新更新