Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.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# 在web服务请求中共享信息_C#_Asp.net_Multithreading_Wcf_Logging - Fatal编程技术网

C# 在web服务请求中共享信息

C# 在web服务请求中共享信息,c#,asp.net,multithreading,wcf,logging,C#,Asp.net,Multithreading,Wcf,Logging,我们有一个解决方案,使用WCF客户端消息检查器从http头读取相关id(和其他值) 然后将该值存储在System.ServiceModel.OperationContext.Current.IncomingMessageProperties中 OperationContext.Current是当前线程的执行上下文 然而,根据Jon Skeet对这个问题的回答: asp.net请求将在线程之间跳转 这正是我们所经历的,即在请求执行过程中相关id会发生变化 是否有地方可以存储http请求中的值,以便

我们有一个解决方案,使用WCF客户端消息检查器从http头读取相关id(和其他值)

然后将该值存储在System.ServiceModel.OperationContext.Current.IncomingMessageProperties中

OperationContext.Current是当前线程的执行上下文

然而,根据Jon Skeet对这个问题的回答: asp.net请求将在线程之间跳转

这正是我们所经历的,即在请求执行过程中相关id会发生变化


是否有地方可以存储http请求中的值,以便可以在堆栈的较低位置访问这些值?

例如,我使用了
System.Web.HttpContext.Current.Items
集合来存储我们在MessageInspector中获取的值

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        var httpResponse =
            reply.Properties[HttpResponseMessageProperty.Name] 
                   as HttpResponseMessageProperty;

        if (httpResponse != null)
        {
            string cookie = httpResponse.Headers[HttpResponseHeader.SetCookie];

            if (!string.IsNullOrEmpty(cookie))
            {
                System.Web.HttpContext.Current.Items.Add("MyCookies", cookie);
            }
        }
    }
在堆栈的其他位置,可以使用相同的键从Items集合中拾取值


请查看Scott Hanselman提供的更多详细信息。

我使用了
System.Web.HttpContext.Current.Items
集合来存储我们在MessageInspector中获取的值,例如

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        var httpResponse =
            reply.Properties[HttpResponseMessageProperty.Name] 
                   as HttpResponseMessageProperty;

        if (httpResponse != null)
        {
            string cookie = httpResponse.Headers[HttpResponseHeader.SetCookie];

            if (!string.IsNullOrEmpty(cookie))
            {
                System.Web.HttpContext.Current.Items.Add("MyCookies", cookie);
            }
        }
    }
在堆栈的其他位置,可以使用相同的键从Items集合中拾取值

请查看Scott Hanselman提供的更多详细信息。

这对我很有用

 var headers =OperationContext.Current.IncomingMessageProperties["httpRequest"];
        var apiToken = ((HttpRequestMessageProperty)headers).Headers["apiKey"];
这对我有用

 var headers =OperationContext.Current.IncomingMessageProperties["httpRequest"];
        var apiToken = ((HttpRequestMessageProperty)headers).Headers["apiKey"];

消息检查器可能运行在不同的线程上。消息检查器可能运行在不同的线程上。