# OnceMi.AspNetCore.AutoInjection **Repository Path**: MiKeNetProject/oncemi_aspnetcore_autoinjection ## Basic Information - **Project Name**: OnceMi.AspNetCore.AutoInjection - **Description**: ASP.NET Core自动依赖注入。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: https://github.com/oncemi/OnceMi.AspNetCore.AutoInjection - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-03-02 - **Last Updated**: 2022-03-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # OnceMi.AspNetCore.AutoInjection **注意:本项目不再更新,此组件移动至:https://github.com/oncemi/OnceMi.Framework/tree/main/src/OnceMi.AspNetCore.AutoInjection**
1. 安装 通过Nuget安装:[![](https://img.shields.io/nuget/v/OnceMi.AspNetCore.AutoInjection.svg)](https://www.nuget.org/packages/OnceMi.AspNetCore.AutoInjection) 2. 配置注入 在```Startup```中的```ConfigureServices```中配置服务: ```c# public void ConfigureServices(IServiceCollection services) { //配置自动注入 services.AddAutoInjection(); services.AddControllersWithViews(); } ``` 3. 使用 在需要注入的类中,使用```AutoInjection```标记,注册服务会通过反射将有此特性的类注册到DI容器中: ```c# [AutoInjection(typeof(ITestService), InjectionType.Singleton)] public class TestServiceC : ITestService { private readonly ILogger _logger; public TestServiceC(ILogger logger) { _logger = logger; } public void Test() { _logger.LogInformation($"This is {this.GetType().Name} inject by AutoInjection."); } } ``` 4. 说明 ```AutoInjectionAttribute```有四种构造方式: | 属性 | 说明 | 生命周期 | | ------------ | ------------ |------------ | | [AutoInjection] | 默认方式,不包含接口,直接注入当前类 | Scoped | | [AutoInjection(typeof(ITestService))] | 接口注入,注入指定的接口,当前类需要是指定接口的实现类 | Scoped | | [AutoInjection(InjectionType.Singleton)] | 不包含接口,直接注入当前类 | 指定的生命周期(Singleton) | | [AutoInjection(typeof(ITestService), InjectionType.Singleton)] | 接口注入,注入指定的接口,当前类需要是指定接口的实现类 | 指定的生命周期(Singleton) |