Jim
Jim

Reputation: 16012

Getting Autofac to work with MVC6/ASP.NET5

I cannot get autofac to work, I have looked at this potentially duplicate question, but it doesn't help.

I am using the full .NET stack, DNX 4.5.1

I have included the following dependencies.

  "dependencies": {
    // matched latest autofac version with latest dependencyinjection version.
    "Autofac": "4.0.0-beta8-157",
    "Autofac.Framework.DependencyInjection": "4.0.0-beta8-157",    
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final" ...

And the following initialisation code.

// void?
public void ConfigureServices(IServiceCollection services)
{
     services.AddMvc();

     var container = new ContainerBuilder();
     ...
     // compilation error here!
     container.Populate(services);
}

I am receiving this error:

Error   CS1503  Argument 2: cannot convert
from'Microsoft.Extensions.DependencyInjection.IServiceCollection' to
'System.Collections.Generic.IEnumerable<Microsoft.Framework.DependencyInjection.ServiceDescriptor>' 
MuWapp.DNX 4.5.1    C:\MuWapp\Startup.cs    54  Active

Upvotes: 0

Views: 843

Answers (2)

Alex Meyer-Gleaves
Alex Meyer-Gleaves

Reputation: 3831

For RC1 you will need to use the Autofac.Extensions.DependencyInjection package.

https://www.nuget.org/packages/Autofac.Extensions.DependencyInjection/

We renamed our package to align with Microsoft's rename to Microsoft.Extensions.DependencyInjection. It's been a moving target supporting the early DNX releases.

Upvotes: 3

Marcin Zablocki
Marcin Zablocki

Reputation: 10683

As I've mentioned in the comment, you should use compatible versions of all packages in your project.json. I see on their page: https://github.com/autofac/Autofac/releases that they have released version for RC1, however there is no Autofac.Framework.DependencyInjection for RC1, so if you require this package, you will be unable to run it.

I think you should use built-in dependency injection during your development untill there is RTM version and all of the third-party packages will become stable.

Build-in DI has functionality for injecting classes into controllers, properties as well as attributes, so unless you use some advanced scenarios in which autofac is necessary, you should stick to asp.net 5 DI.

Upvotes: 0

Related Questions