How to integrate Autofac with WepApi 2 and Owin?

后端 未结 1 1907
臣服心动
臣服心动 2021-02-05 04:28

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

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

And following this post:

<
相关标签:
1条回答
  • 2021-02-05 05:10

    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. This is the lifetimescope marked with the AutofacWebRequest tag.

    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;
    

    but missed:

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

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

    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:

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

    I hope this is useful!

    0 讨论(0)
提交回复
热议问题