【问题标题】:Syncing correlationstate between IClientMessageInspector and IParameterInspector在 IClientMessageInspector 和 IParameterInspector 之间同步相关状态
【发布时间】:2012-12-28 17:34:58
【问题描述】:

我有一个 wcf 客户端。根据要求,我需要记录请求中的一些元数据(以及请求中未包含的用户数据)。然后,如果请求成功,我可能必须记录响应元数据,并根据标志,完整的肥皂请求。

我正在尝试以正确的方式执行此操作(使用 IParameterInspector 检查元数据并使用 IClientMessageInspector 获取 Soap),但我无法关联两个接口请求。我不确定这里的线程安全。这是我所在位置的精简版...

     public class SoapRequestInfo
{
    public string UserId { get; set; }
    public Guid Key { get; set; }
    //would contain a lot more info
}

public class OperationProfilerParameterInspector : IParameterInspector, IClientMessageInspector
{
    //before serialization
    public object BeforeCall(string operationName, object[] inputs) //IParameterInspector
    {
        //Add the operation, record some specific inputs to db
        return new SoapRequestInfo
                            {
                                UserId = "1234",
                                Key = new Guid()
                            };
    }

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) //IParameterInspector
    {
       var info = correlationState as SoapRequestInfo;
        //Do some additional logging - easy enough
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel) //IClientMessageInspector
    {
        //want to correlate this with IParameterInspector
        return null;
    }


    public void AfterReceiveReply(ref Message reply, object correlationState) //IClientMessageInspector
    {
        //May want to log full soap message depending on after call criteria
    }
}      

我知道我不能使用私有变量来保存 Guid。我不能使用会话,因为可能有多个请求连续出现并且不能保证响应是正确的。那么如何才能唯一标识两个接口之间的correlationState呢?

【问题讨论】:

  • 你找到解决这个问题的方法了吗?
  • 不幸的是,没有。下面的帖子中有一个很好的观点,即在标题中设置一些标识符,但由于我只是服务的消费者,我无法取回标识符。为 TLS 赋予价值听起来很有希望。问题发生后 8 个月左右的答案太糟糕了。这是一个 Web API,所以我们关闭了会话并使用了本地 memcache 服务器,并且使用身份验证令牌不能作为唯一标识符,因为 api 用于可以触发(并且确实)触发的移动应用程序一次多个请求。 50 对于某些页面,但响应速度非常快

标签: wcf


【解决方案1】:

如果您的服务在 ASPNET 兼容模式下运行,您可能可以使用 HttpContext.Items 来保留您的对象,否则您可以使用 TLS(线程本地存储),将数据放入插槽并稍后获取/清除。

【讨论】:

    【解决方案2】:

    愿你可以尝试做一些不同的事情:

    来自这个帖子passing correlation token to WCF service?

    如果您使用带有 WS-Addressing 的消息版本,您应该自动拥有它,因为每条消息都将包含其自动生成的 ID (guid),并且每个响应也将包含请求的 ID。您可以通过 OperationContext 访问这些标头

    如果使用消息不符合您的要求,也许您可​​以尝试在发送请求时将自己的 id 放在 header 中,并可能使用相同的 id 更新响应。

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2022-10-23
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多