简体   繁体   English

如何将Autofac与WepApi 2和Owin集成?

[英]How to integrate Autofac with WepApi 2 and Owin?

I am using this package to integrate Autofac with my WebApi Owin application: 我正在使用此软件包将Autofac与我的WebApi Owin应用程序集成:

https://www.nuget.org/packages/Autofac.WebApi2.Owin https://www.nuget.org/packages/Autofac.WebApi2.Owin

And following this post: 并关注这篇文章:

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/

My code in Startup.cs looks like this: 我在Startup.cs中的代码如下所示:

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

However whichever way I spin it, rearrange the code or whatever, Autofac is just not able to resolve anything. 无论我如何旋转它,重新排列代码或其他任何东西,Autofac都无法解决任何问题。 Before Owin I had my Global.asax method working just fine: 在Owin之前,我的Global.asax方法运行得很好:

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

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

        GlobalConfiguration.Configure(WebApiConfig.Register);
    }

What am I missing? 我错过了什么?

Thanks 谢谢

Ok, 好,

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. Autofac Owin集成实际上创建了一个Owin liftimescope,它可以通过整个Owin管道获得,因此可用于中间件并将此lifetimescope扩展到HttpRequestMessage。 This is the lifetimescope marked with the AutofacWebRequest tag. 这是标有AutofacWebRequest标记的lifetimescope。

So all the previous WebApi integration code still needs to be performed on application startup. 因此,所有以前的WebApi集成代码仍然需要在应用程序启动时执行。 I have included: 我包括:

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

but missed: 但错过了:

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

in the EngineContext.Initialize method, which does all the registrations via the builder. 在EngineContext.Initialize方法中,该方法通过构建器执行所有注册。

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: 在这里,您可以找到有关如何将Autofac与WebApi集成的更多信息,这显然也需要在Owin的情况下完成:

https://code.google.com/p/autofac/wiki/WebApiIntegration https://code.google.com/p/autofac/wiki/WebApiIntegration

I hope this is useful! 我希望这很有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM