如何通过Owin上下文回购被注入到API控制器 [英] How to pass Owin context to a Repo being injected into Api controller

查看:1017
本文介绍了如何通过Owin上下文回购被注入到API控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC的WebAPI owin(软主持)项目,使用统一解决控制器依赖

I've got a MVC WebApi owin (soft hosted) project, that uses Unity for resolving controller dependencies

这看起来像这样

public class PacientaiController : ODataController
    {
        private readonly IEntityRepo<Ent.Pacientas> repo;

        public PacientaiController(IEntityRepo<Ent.Pacientas> repo)
        {
            this.repo = repo;
        }



我试图解决的问题 - 就是我如何通过OwinContex '成回购

the problem I'm trying to solve - is how do I pass 'OwinContex' into a Repo.

public class PacientasEntityRepo:IEntityRepo<Pacientas>,IDisposable
    {
        public PacientasEntityRepo(IOwinContext ctx)
        {
        .........

如果我试图像这样在 Startup.cs

Container.RegisterType<IOwinContext>(new InjectionFactory(o => HttpContext.Current.GetOwinContext()));



我得到一个空裁判,称 HttpContext.Current 为NULL

这里的主要思想,是对当前身份验证的用户传递到回购,由于回购主办逻辑查询数据库,根据用户。 (例如,如果用户是管理员,则返回该数据,如果用户是来宾 - 返回这个数据)

The main idea here, is to pass the currently authenticated user to the repo, because Repo host the Logic for querying the Database, depending on the user. (say if the user is Admin, then return this data, if the user is guest - return this data)

的点之中! - 这是一个自主机

The point being - that this is a self Host !

推荐答案

让我们抛开为什么你有这样的设计和专注的问题:注射 IOwinContext

Lets put aside why you have this design and concentrate to the problem: injecting the IOwinContext:

您还可以用 HttpRequestMessage 实例得到它 GetOwinContext 方法,但你还需要获得一个 HttpRequestMessage 弄好了。

you can also get it from a HttpRequestMessage instance with the GetOwinContext method, however you also need to get a HttpRequestMessage somehow.

团结呢不支持 HttpRequestMessage 注射开箱即用,但你可以使用自定义的 DelegatingHandler 存储当前 HttpRequestMessage 在容器如下所述:注入的WebAPI UrlHelper到使用Autofac

Unity does not support injection of the HttpRequestMessage out of the box but you can use a custom DelegatingHandler which stores the current HttpRequestMessage in the container as described here: Inject WebAPI UrlHelper into service using Autofac

链接的问题是关于Autofac,但你可以用统一将其传送工作服务:

The linked question is about Autofac but you can transfer it for work with Unity:

CurrentRequest CurrentRequestHandler 可从的Andrew戴维的答案,因为它是:

The CurrentRequest and the CurrentRequestHandler can be used from Andrew Davey's answer as it is:

public class CurrentRequest
{
    public HttpRequestMessage Value { get; set; }
}

public class CurrentRequestHandler : DelegatingHandler
{
    protected async override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        var scope = request.GetDependencyScope();
        var currentRequest = (CurrentRequest)scope.GetService(typeof(CurrentRequest));
        currentRequest.Value = request;
        return await base.SendAsync(request, cancellationToken);
    }
}



然后你只需要注册 DelegatingHandler

httpConfiguration.MessageHandlers.Insert(0, new CurrentRequestHandler());

和注册 CurrentRequest IOwinContext 在容器

container.RegisterType<CurrentRequest>(
            new HierarchicalLifetimeManager());

container.RegisterType<IOwinContext>(
    new HierarchicalLifetimeManager(),
    new InjectionFactory(c => c.Resolve<CurrentRequest>().Value.GetOwinContext()));

httpConfiguration.DependencyResolver = new UnityHierarchicalDependencyResolver(container);



除了自定义委托处理器有其他地方挂接到Web.API捕捉 HttpRequestMessage 例如,你可以创建自己的 IHttpControllerActivator ,并使用 ExecuteAsync 方法如下所述:依赖注入的ASP.NET Web API 2

Beside the custom delegation handler there are other places to hook into Web.API to capture the HttpRequestMessage for example you can create your own IHttpControllerActivator and use the ExecuteAsync method as described here: Dependency Injection in ASP.NET Web API 2

这篇关于如何通过Owin上下文回购被注入到API控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆