我是否应该使用OwinContext的环境来保存每个请求的特定于应用程序的数据

我需要一种方法来存储每个请求的日志对象。 使用HttpContext,我会将其添加到项目Dictionary中。 如果我能帮助它,我不想把HttpContext带进去。 下面的代码是我建议的Unity LifeTimeManager,它将在OwinContext的Environment属性中存储对象,我可以使用我的Owin中间件访问它。

public class OwinContextLifetimeManager : LifetimeManager { private string key = (new Guid()).ToString(); private IDictionary environment; public OwinContextLifetimeManager(IDictionary environment) { this.environment = environment; } public override object GetValue() { if (environment != null && environment.ContainsKey(key)) return environment[key]; else return null; } public override void RemoveValue() { if (environment != null) environment.Remove(key); } public override void SetValue(object newValue) { if (environment != null) environment[key] = newValue; } } 

然后我可以从我的中间件中使用它:

 container.RegisterType(new OwinContextLifetimeManager(environment)); 

在我看来,我可以选择我想要的任何键,除了那些已经由Owin保留的键。 有什么理由我不应该为此目的使用OwinContext.Environment吗? MSDN文档对此的最佳实践含糊不清。

Darrel Miller在这里的回答: 当使用OWIN自我主机时,我应该如何存储每个请求数据ASP.NET Web API让我相信请求对象上的属性集合是要走的路。 如何从中间件访问此对象?

OWIN环境字典可用于存储每个请求数据。 请求对象的属性集合可用于执行相同操作。

主要区别在于OWIN环境字典是一个OWIN概念,适用于在OWIN主机中运行的任何中间件。 请求对象的属性集合是ASP.NET Web API概念,仅适用于该特定框架。

顺便说一句,ASP.NET Web API本身作为OWIN管道中的中间件运行。 因此,要回答您的问题,您无法从中间件访问Web API的请求属性集合,因为它仅适用于Web API中间件(或特定框架)。

如果你想编写你的横切关注的东西作为OWIN中间件你必须使用OWIN环境字典。 如果Web API扩展点像filter或消息处理程序一样可以,那么您可以使用属性集合。

显然,利用Web API扩展点编写的任何内容仅适用于Web API,而OWIN中间件适用于在OWIN管道中运行且包含Web API的任何类型的应用程序。