本文介绍了我应该使用OwinContext的环境来保存每个请求应用程序特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来存储每个请求的日志对象。随着HttpContext的我想补充这项词典。我不想把HttpContext的这个,如果我能帮助它。
下面code是什么,我提出一个统一LifeTimeManager将存储在OwinContext的环境属性的对象,我有机会与我的Owin中间件。

I need a way to store a logging object per request. With HttpContext I would add this to the items Dictionary. I don't want to bring HttpContext into this if I can help it.The below code is what I propose for a Unity LifeTimeManager that will store objects in OwinContext's Environment property, which I have access to with my Owin middleware.

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;
    }
}

然后我就可以使用它像这样从我的中间件:

Then I can use it like this from my middleware:

container.RegisterType<IRequestLog, RequestLog>(new OwinContextLifetimeManager(environment));

它发生,我认为我可以选择我想除了那些已经被Owin保留任何键。
有没有我不应该使用OwinContext.Environment用于此目的的任何原因? MSDN文档是在此最佳实践含糊。

It occurs to me that I can choose whatever key I want except those that already are reserved by Owin.Is there any reason I should not be using the OwinContext.Environment for this purpose? The MSDN documentation is vague on the best practices of this.

达雷尔 - 米勒的回应在这里:How我应该用OWIN为自托管的ASP.NET Web API 使我相信请求对象的属性集合是要走的路时,每个请求的数据存储。如何从中间件访问该对象?

Darrel Miller's response here: How should I store per request data when using OWIN to Self-Host ASP.NET Web API leads me to believe the properties collection on the request object is the way to go. How can I access this object from middleware?

推荐答案

OWIN环境字典可以被用来存储每个请求的数据。请求对象的属性集可用于这样做。主要的区别是OWIN环境词典是OWIN概念,适用于在一个OWIN主机运行的任何中间件。请求对象的属性集合是一个ASP.NET Web API的概念,只适用于特定的框架。 BTW,的ASP.NET Web API本身运行在OWIN管道中间件。因此,要回答你的问题,你不能从你的中间件,因为它仅适用于网页API中间件(或具体框架)访问Web API的请求属性集合。如果你想要写你的横切关注点的东西,OWIN中间件必须使用OWIN环境的字典。如果像过滤器或消息处理程序的Web API扩展点是好的,那么你可以使用属性集合。显然,任何你写利用Web API扩展点仅适用于网页API,而OWIN中间件适用于任何种类的OWIN管道运行的应用程序,并且,包括Web API。

OWIN environment dictionary can be used to store per-request data. Properties collection of the request object can be used to do the same. The main difference is OWIN environment dictionary is an OWIN concept and is applicable to any middleware running in a OWIN host. Properties collection of the request object is an ASP.NET Web API concept and is applicable only to that specific framework. BTW, ASP.NET Web API itself runs as a middleware in OWIN pipeline. So, to answer your question, you cannot access the request properties collection of Web API from your middleware because it is applicable only to Web API middleware (or that specific framework). If you want to write your cross-cutting concern stuff as OWIN middleware you have to use OWIN environment dictionary. If Web API extension points like a filter or a message handler is okay, then you can use the properties collection. Obviously, anything you write leveraging Web API extension points is applicable only to Web API whereas OWIN middleware is applicable to any kind of app running in OWIN pipeline and that includes Web API.

这篇关于我应该使用OwinContext的环境来保存每个请求应用程序特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:52