简体   繁体   中英

Can't add Autofac to ServiceCollection in .Net Core

I want to add Autofac to .Net Core application. I've tried to do all steps from Autofac:Getting started tutorial , I've installed Autofac.Extensions.DependencyInjection , have updated Program class, but can't add autofac as in example.

Get error

Error CS1061 'IServiceCollection' does not contain a definition for 'AddAutofac' and no extension method 'AddAutofac' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Reinstall nuget package, clean & rebuild solution, close VS - things which doesn't help.


public class Program
{
  public static void Main(string[] args)
  {
        var host = new WebHostBuilder()
            .UseKestrel()
            .ConfigureServices(services => services.AddAutofac()) //here is problem
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
  }
}

Version of Autofac - 4.6.2 Version of Autofac.Extensions.DependencyInjection - 4.2.0

Try adding using Autofac.Extensions.DependencyInjection;

using System.IO;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;

namespace App
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .ConfigureServices(services => services.AddAutofac()) 
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

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