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

cod*_*ast 17 c# asp.net unity-container owin owin-middleware

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

public class OwinContextLifetimeManager : LifetimeManager
{
    private string key = (new Guid()).ToString();
    private IDictionary<string, object> environment;

    public OwinContextLifetimeManager(IDictionary<string, object> 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

container.RegisterType<IRequestLog, RequestLog>(new OwinContextLifetimeManager(environment));
Run Code Online (Sandbox Code Playgroud)

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

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

Bad*_*dri 16

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

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

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

如果您想将您的横切关注内容编写为OWIN中间件,则必须使用OWIN环境字典.如果Web API扩展点像过滤器或消息处理程序一样可以,那么您可以使用属性集合.

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

  • 很多文字,没什么意义.更多论点:并发?HttpContext.Current.Items与Owin环境的区别?Owin FILO和注册中间件的重要性..希望听到更多关于具体事情而不是"Web API使用"=>我们也应该使用.. (2认同)