.net core 6 使用注解自动注入实例,无需构造注入 autowrite4net

像java使用@autowrite一样使用

1、前提先注册到ioc容器当中

builder.Services.AddScoped

2、nuget引入AutoWrite4Net

3、启用

cs 复制代码
//启用自动注入
app.UseAutoWrite();

4、在类上使用注解

cs 复制代码
[StartAutoWrite]
public class NacosController : ControllerBase

5、实例上使用注解

cs 复制代码
/// <summary>
/// nacos实例
/// </summary>
[AutoWrite]
public INacosConfigService? nacosConfigService;

6、即可直接使用,且在使用AddScoped方式注入的实例也保持生命周期不发生变化

7、出现System.InvalidOperationException:"*****' from root provider."错误时关闭验证

cs 复制代码
builder.Host.UseDefaultServiceProvider(use =>
{
    use.ValidateScopes = false;
});

我在使用.netcore6开发时怎么搜索都没有类似的插件,于是自己开发了这个。

下载后可以直接在项目dev分支二次开发或者优化提交一起完善这款插件。

gitee: FMJ/AutoWrite4Net

相关推荐
csdn_aspnet5 天前
.NetCore Webapi 接口验证请求来源
.netcore·ip·token·签名·webapi·cors·sign
宝桥南山8 天前
Microsoft Fabric - 简单尝试一下Microsoft Fabric .NET SDK
microsoft·微软·.net·.netcore·powerbi·fabric
geovindu9 天前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
geovindu14 天前
CSharp: Observer Pattern
开发语言·后端·观察者模式·设计模式·c#·.netcore·行为模式
geovindu14 天前
CSharp: Strategy Pattern
开发语言·后端·c#·.net·.netcore·策略模式·行为模式
geovindu14 天前
CSharp: Task Scheduler
开发语言·后端·c#·.net·.netcore
geovindu20 天前
CSharp: Wordcloud
开发语言·后端·c#·.net·.netcore·词云
geovindu20 天前
CSharp: Command Pattern
开发语言·后端·c#·.net·.netcore·命令模式·行为模式
全栈小51 个月前
【C#】.net core,静态方法线程安全解析,面试时经常被问的线程安全就藏在里面
安全·c#·.netcore
清风与日月1 个月前
Yitter.IdGenerator:高性能分布式唯一ID生成器详解
分布式·c#·.net·.netcore