ASP.NET核心中的dependency injection

在Autofac中,您可以使用RegisterAssemblyTypes注册您的依赖项,这样您就可以完成类似这样的事情了吗?有没有办法在.net Core DI版本中做类似的事情

 builder.RegisterAssemblyTypes(Assembly.Load("SomeProject.Data")) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces() .InstancePerLifetimeScope(); 

这就是我想要注册的内容

LeadService.cs

 public class LeadService : ILeadService { private readonly ILeadTransDetailRepository _leadTransDetailRepository; public LeadService(ILeadTransDetailRepository leadTransDetailRepository) { _leadTransDetailRepository = leadTransDetailRepository; } } 

LeadTransDetailRepository.cs

 public class LeadTransDetailRepository : RepositoryBase, ILeadTransDetailRepository { public LeadTransDetailRepository(IDatabaseFactory databaseFactory) : base(databaseFactory) { } } public interface ILeadTransDetailRepository : IRepository { } 

这就是我如何尝试Regisyer,但我无法弄清楚如何注册存储库Startup.cs

 public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); services.AddTransient(); //not sure how to register the repositories services.Add(new ServiceDescriptor(typeof(ILeadTransDetailRepository), typeof(IRepository), ServiceLifetime.Transient)); services.Add(new ServiceDescriptor(typeof(IDatabaseFactory), typeof(DatabaseFactory), ServiceLifetime.Transient)); services.AddTransient(_ => new DataContext( this.Configuration["Data:DefaultConnection:ConnectionString"])); } 

使用ASP.NET Core Dependency Injection / IoC容器没有开箱即用的方法,但它是“按设计”。

ASP.NET IoC Container / DI旨在成为添加DIfunction的简单方法,并作为构建到ASP.NET Core应用程序中的其他IoC Container框架的基础。

据说它支持简单的场景(注册,尝试使用第一个构造函数,其中大多数参数都满足依赖关系和作用域依赖关系),但它缺少自动注册或装饰器支持等高级场景。

对于此function,您必须使用第三方库和/或第三方IoC容器(AutoFac,StructureMap等)。 它们仍然可以插入IServiceCollection您之前的注册仍然有效,但是您可以获得其他function。

我想你可以通过手动扫描注册所有服务。 然后将它们注册到Service集合。 这是我的示例代码(.Net core 2.0)

 public static void ResolveAllTypes(this IServiceCollection services, string solutionPrefix, params string[] projectSuffixes) { //solutionPrefix is my Solution name, to separate with another assemblies of Microsoft,... //projectSuffixes is my project what i want to scan and register //Note: To use this code u must reference your project to "projectSuffixes" projects. var allAssemblies = new List(); var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); foreach (var dll in Directory.GetFiles(path, "*.dll")) allAssemblies.Add(Assembly.LoadFile(dll)); var types = new List(); foreach (var assembly in allAssemblies) { if (assembly.FullName.StartsWith(solutionPrefix)) { foreach (var assemblyDefinedType in assembly.DefinedTypes) { if (projectSuffixes.Any(x => assemblyDefinedType.Name.EndsWith(x))) { types.Add(assemblyDefinedType.AsType()); } } } } var implementTypes = types.Where(x => x.IsClass).ToList(); foreach (var implementType in implementTypes) { //I default "AService" always implement "IAService", You can custom it var interfaceType = implementType.GetInterface("I" + implementType.Name); if (interfaceType != null) { services.Add(new ServiceDescriptor(interfaceType, implementType, ServiceLifetime.Scoped)); } } } 

我也想尝试内置,厌倦了缺少自动注册并为此构建了一个开源项目,请看一下:

https://github.com/SharpTools/SharpDiAutoRegister

只需添加nuget包SharpDiAutoRegister并在ConfigureServices方法中添加您的约定:

 services.ForInterfacesMatching("^I[a-zA-z]+Repository$") .OfAssemblies(Assembly.GetExecutingAssembly()) .AddSingletons(); services.ForInterfacesMatching("^IRepository") .OfAssemblies(Assembly.GetExecutingAssembly()) .AddTransients(); //and so on... 

ASP.NET Core是从头开始设计的,用于支持和利用dependency injection。

ASP.NET Core应用程序可以通过将它们注入Startup类中的方法来利用内置框架服务,并且还可以配置应用程序服务以进行注入。

ASP.NET Core提供的默认服务容器提供了一个最小的function集

不打算替换其他容器。

资料来源: https : //docs.asp.net/en/latest/fundamentals/dependency-injection.html


默认的ASP.NET容器是

简单,不提供其他容器可用的强大配置和性能选项。

幸运的是,您可以将默认容器换成一个已经作为NuGet包提供的社区创建的全function容器。

Autofachttp://autofac.org/ )已经可用于ASP.NET Core,您可以通过引用它们将它添加到项目中

Autofac和

Autofac.Extensions.DependencyInjection包。

资料来源: https : //blogs.msdn.microsoft.com/webdev/2016/03/28/dependency-injection-in-asp-net-core/