我们有一个解决方案,其中我们使用WCF客户端消息检查器从http报头中读取相关id (和其他值)。
然后,我们将该值存储在System.ServiceModel.OperationContext.Current.IncomingMessageProperties.中
OperationContext.Current是当前线程的执行上下文。
然而,根据Jon Skeet的回答,这个问题:Will a request in IIS run on a single thread? asp.net请求将在线程之间跳转。
这正是我们正在经历的,在请求的执行过程中,关联id发生了变化。
有没有什么地方我们可以存储来自http请求的值,这样它们就可以在堆栈的较低层被访问?
发布于 2012-08-29 16:55:09
我使用了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的此article以了解更多详细信息。
发布于 2016-09-11 07:32:08
这对我很管用
var headers =OperationContext.Current.IncomingMessageProperties["httpRequest"];
var apiToken = ((HttpRequestMessageProperty)headers).Headers["apiKey"];https://stackoverflow.com/questions/12159566
复制相似问题