简体   繁体   中英

Getting autofac to work with mvc6 beta5

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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