HttpContext.Current如何作用于请求?

时间:2017-06-25 13:04:48

标签: c# .net asp.net-web-api

我想创建一个与ApiContext.Current类似的HttpContext.Current,但我不想使用HttpContext.Current.Items

如果ApiContext.Current是静态属性。

public class ApiContextSnapshot {
    public Guid AppId { get; set; }
}

public static class ApiContext {
    public static ApiContextSnapshot Current { get; set; }
}

这个问题是并发请求将覆盖/使用相同的静态类。 HttpContext.Current如何执行此操作以便并发请求具有自己的静态类

Per mjwillis

所以他们正在使用CallContext.HostContext。那么HttpContext.Current会返回CallContext.HostContext吗?

如果我想做类似的事情,我应该使用CallContext.SetDataCallContext.HostData吗?

1 个答案:

答案 0 :(得分:1)

HttpContext.Current并非如此。您已经定义了标准的自动实现属性,因此只有一个'值'。

HttpContext.Current似乎最终会调用CallContext.HostContext,其定义为here。我的猜测是ASP.NET / IIS / something在Web请求开始时设置了该属性。

http://blog.stephencleary.com/2013/04/implicit-async-context-asynclocal.html也可能值得一读,作为考虑的选项。虽然,老实说,我认为你应该使用HttpContext.Current.Items

相关问题