Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 966
Diver Dan Avatar asked May 30 '15 21:05

Diver Dan


People also ask

Should I use Autofac with ASP.NET core?

It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. Autofac is the most popular DI/IoC container for ASP.NET and it works with . NET Core flawlessly.

How do I use Autofac in .NET core?

In that case, we have to create a module class. First, I create the Configuration folder in the root directory and then create the module class called RegisterModule, which inherits the Module in Autofac as below code. Then we have to register our services in the RegisterModule as below code.


Video Answer


1 Answers

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!

like image 70
erPe Avatar answered Oct 03 '22 20:10

erPe