Autofac程序集扫描-.NET Core [英] Autofac assembly scanning - .NET Core

查看:102
本文介绍了Autofac程序集扫描-.NET Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在我的.NET Core 2.0项目中进行程序集扫描.

I've been trying to get assembly scanning working in my .NET core 2.0 project.

我得到的例外情况

Autofac.Core.Registration.ComponentNotRegisteredException
  HResult=0x80131500
  Message=The requested service 'Microsoft.AspNetCore.Hosting.Internal.WebHostOptions' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
  Source=Autofac
  StackTrace:
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.<RunAsync>d__4.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
   at Track4Corp.VehicleDatabase.Server.Program.Main(String[] args) in C:\Users\Admin\source\repos\VehicleDatabase-server\src\Track4Corp.VehicleDatabase.Server\Program.cs:line 10

Program.cs

Program.cs

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace Track4Corp.VehicleDatabase.Server
{
  public class Program
  {
    public static void Main(string[] args)
    {
      CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
  }
}

启动类

public class Startup
  {
    public Autofac.IContainer ApplicationContainer { get; private set; }

    public IConfigurationRoot Configuration { get; private set; }

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
//TODO: Move out of code before release
      services.AddDbContext<UserDBContext>(options => options.UseSqlServer("Server=DESKTOP-9L9IGFE;Database=DB;Trusted_Connection=True;"));

      //Autofac Assembly Registration
      ApplicationContainer = AutoFacAssemblyRegistration.RegisterAllAssemblies();

      return new AutofacServiceProvider(ApplicationContainer);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
      }

      app.Run(async (context) =>
      {
       await context.Response.WriteAsync("Hello World!");
      });

因此,我实际上的构建器已移至另一个项目,现在我在该项目上的硬编码路径上扫描,我知道所有dll文件都位于该路径上.我在调试模式下运行了它,然后看到它们都在那里.

So my actually builder i moved to another project, where I for now scan on a hardcorded path where I know all my dll files are located. I ran it in debug mode and see all of them being there.

  public class AutoFacAssemblyRegistration
  {
    public static IContainer RegisterAllAssemblies()
    {
      var builder = new ContainerBuilder();
      string executingDirectory = Directory.GetCurrentDirectory();
      // string[] files = Directory.GetFiles(executingDirectory, "*.dll");
      string[] files = Directory.GetFiles(@"PATH_HERE", "*.dll");

      var listOfAssemblies = new List<Assembly>();
      foreach (string item in files)
      {
        listOfAssemblies.Add(item: Assembly.LoadFile(item));
      }
      builder.RegisterAssemblyTypes(listOfAssemblies.ToArray()).AsImplementedInterfaces().InstancePerLifetimeScope();
      //builder.RegisterAssemblyModules(listOfAssemblies.ToArray());

      return builder.Build();
    }
  }

我还尝试了各种解决方案来注册程序集,例如

I also tried different sort of solutions to register the assemblies, such as

foreach (Assembly item in listOfAssemblies)
{
  System.Type[] assemblyType = item.GetTypes();
  foreach (System.Type typeItem in assemblyType)
  {
    if (typeItem.IsInterface)
    {
      Autofac.Builder.IRegistrationBuilder<object, Autofac.Builder.ConcreteReflectionActivatorData,
        Autofac.Builder.SingleRegistrationStyle> registration =
        builder.RegisterType(typeItem).AsImplementedInterfaces();

      if (typeItem.GetInterfaces().Any(i => i.IsAssignableFrom(typeof(ISingletonDependency))))
      {
        registration.SingleInstance();
      }
      else
      {
        registration.InstancePerRequest();
      }
    }
  }
}

我想知道我是否做错了什么,或者在.NET Core 2.1项目中这是不可能的?

I am wondering if I am doing something wrong, or perhaps this is not possible in .NET core 2.1 projects?

如果有人能向我暗示正确的方向,以及如何解决我的问题,那将是很好的事情.

Would be nice if anyone could hint me in the right direction, how to solve my problem.

推荐答案

存在类似的问题,并通过在执行containerBuilder.Build()之前添加containerBuilder.Populate(serviceCollection)来解决.

Had a similar issue and solved it by adding containerBuilder.Populate(serviceCollection) before doing containerBuilder.Build().

按照您在问题中提供的示例,它看起来像这样-

Following the sample you provided in your question, it would look something like this -

  public class AutoFacAssemblyRegistration
  {
    public static IContainer RegisterAllAssemblies(IServiceCollection services)
    {
      var builder = new ContainerBuilder();
      builder.Populate(services);
      string executingDirectory = Directory.GetCurrentDirectory();
      // string[] files = Directory.GetFiles(executingDirectory, "*.dll");
      string[] files = Directory.GetFiles(@"PATH_HERE", "*.dll");

      var listOfAssemblies = new List<Assembly>();
      foreach (string item in files)
      {
        listOfAssemblies.Add(item: Assembly.LoadFile(item));
      }
      builder.RegisterAssemblyTypes(listOfAssemblies.ToArray()).AsImplementedInterfaces().InstancePerLifetimeScope();
      //builder.RegisterAssemblyModules(listOfAssemblies.ToArray());

      return builder.Build();
    }
  }

这篇关于Autofac程序集扫描-.NET Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆