Getting autofac to work with mvc6 beta5

拜拜、爱过 提交于 2019-12-30 04:34:09

问题


I am trying to get autofac working with an mvc6 application I am working on. I found this blog article however it seems to be a little dated. It looks like its using the beta3 bits

I am using this clr version

1.0.0-beta5-11911

My project has these 2 references

"Autofac": "4.0.0-alpha2",
"Autofac.Dnx": "4.0.0-alpha2",

Within the article is talks about how to modify the startup.cs

    // Create the Autofac container builder.
        var builder = new Autofac.ContainerBuilder();

        // Add any Autofac modules or registrations.
        builder.RegisterModule(new AutofacModule());

        // Populate the services.
        builder.Populate(services);

        // Build the container.
        var container = builder.Build();
        return container.Resolve<IServiceProvider>();

The above code complains about builder.Populate(services); giving me an error

The type 'IServiceDescriptor' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.Framework.DependencyInjection.IServiceDescriptor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

From me research it looks like in beta4 DependencyInjection.IserviceDescriptor was removed.

Has anyone else managed to get autofac working with the latest beta5 bits?


回答1:


For anyone who would be looking how to get AutoFac running below configuration allowed me to use it in beta6

Below is snippet of project.json

  "dependencies": {
"Autofac": "4.0.0-beta6-110",
"Autofac.Framework.DependencyInjection": "4.0.0-beta6-110",
"Microsoft.AspNet.Mvc": "6.0.0-beta6",
"Microsoft.AspNet.Server.IIS": "1.0.0-beta6",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta6",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta6"
},

And then part of startup.cs

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        //create Autofac container build
        var builder = new ContainerBuilder();

        //populate the container with services here..
        builder.RegisterType<DemoService>().As<IProjectDemo>();
        builder.Populate(services);

        //build container
        var container = builder.Build();

        //return service provider
        return container.ResolveOptional<IServiceProvider>();
    }

As mentioned by @peco make sure you have

using Autofac.Framework.DependencyInjection

And that got me going notime with AutoFac :) Hope this helps!



来源:https://stackoverflow.com/questions/30551700/getting-autofac-to-work-with-mvc6-beta5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!