无法在.Net Core中将Autofac添加到ServiceCollection

时间:2018-02-25 13:54:58

标签: c# .net dependency-injection .net-core autofac

我想将Autofac添加到.Net Core应用程序中。 我已尝试执行Autofac:Getting started tutorial的所有步骤,我已安装Autofac.Extensions.DependencyInjection,已更新Program课程,但无法添加autofac,例如。

获取错误

  

错误CS1061' IServiceCollection'不包含的定义   ' AddAutofac'没有扩展方法' AddAutofac'接受第一个   类型的参数' IServiceCollection'可以找到(你错过了吗?   using指令或程序集引用?)

重新安装nuget包,清洁&重建解决方案,关闭VS - 没有帮助的事情。

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();
  }
}

Autofac版本 - 4.6.2 Autofac.Extensions.DependencyInjection版本 - 4.2.0

1 个答案:

答案 0 :(得分:7)

尝试添加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();
        }
    }
}