如何Autofac与WepApi 2和Owin集成? [英] How to integrate Autofac with WepApi 2 and Owin?

查看:261
本文介绍了如何Autofac与WepApi 2和Owin集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这包Autofac和我的WebAPI Owin应用程序集成:

I am using this package to integrate Autofac with my WebApi Owin application:

<一个href=\"https://www.nuget.org/packages/Autofac.WebApi2.Owin\">https://www.nuget.org/packages/Autofac.WebApi2.Owin

和下面的这篇文章:

<一个href=\"http://alexmg.com/owin-support-for-the-web-api-2-and-mvc-5-integrations-in-autofac/\">http://alexmg.com/owin-support-for-the-web-api-2-and-mvc-5-integrations-in-autofac/

在Startup.cs我的code是这样的:

My code in Startup.cs looks like this:

        var config = new HttpConfiguration();

        IContainer container = EngineContext.InitializeEngine();

        var dependencyResolver = new AutofacWebApiDependencyResolver(container);
        config.DependencyResolver = dependencyResolver;

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);

        WebApiConfig.Register(config);
        app.UseWebApi(config);

不过无论怎样我转一下,重新安排code或什么的,Autofac是不能够解决任何问题。 Owin之前,我有我的Global.asax方法工作得很好:

However whichever way I spin it, rearrange the code or whatever, Autofac is just not able to resolve anything. Before Owin I had my Global.asax method working just fine:

    protected void Application_Start()
    {
        IContainer container = EngineContext.InitializeEngine();

        var dependencyResolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = dependencyResolver;

        GlobalConfiguration.Configure(WebApiConfig.Register);
    }

我是什么失踪?

感谢

推荐答案

好吧,

我想通了。
该Autofac Owin集成实际上创建了一个Owin liftimescope,这是可以贯穿整个Owin管道,从而提供给中间件和扩展了这一lifetimescope到Htt的prequestMessage。这是标有AutofacWebRequest标签lifetimescope。

I figured it out. The Autofac Owin integration actually creates an Owin liftimescope, which is available through the whole Owin pipeline, thus available to middleware and extends this lifetimescope to the HttpRequestMessage. This is the lifetimescope marked with the AutofacWebRequest tag.

因此​​,所有的previous的WebAPI一体化code仍然需要在应用程序启动时执行。我已经包括:

So all the previous WebApi integration code still needs to be performed on application startup. I have included:

    var dependencyResolver = new AutofacWebApiDependencyResolver(container);
    config.DependencyResolver = dependencyResolver;

但错过了:

var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).InstancePerRequest();

在EngineContext.Initialize方法,它通过制造商完成所有的注册。

in the EngineContext.Initialize method, which does all the registrations via the builder.

在这里您可以找到如何Autofac用的WebAPI,这显然需要在Owin的情况下,也做集成的详细信息:

Here you can find more information on how to integrate Autofac with the WebApi, which obviously needs to be done also in the case of Owin:

<一个href=\"https://$c$c.google.com/p/autofac/wiki/WebApiIntegration\">https://$c$c.google.com/p/autofac/wiki/WebApiIntegration

我希望这是有用的!

这篇关于如何Autofac与WepApi 2和Owin集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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