AspectCore使用 属性注入FromServiceContext

.net5.0

nuget

1
2
3
<PackageReference Include="AspectCore.Core" Version="2.1.0" />
<PackageReference Include="AspectCore.Extensions.DependencyInjection" Version="2.1.0" />
<PackageReference Include="AspectCore.Extensions.Hosting" Version="2.1.0" />

  

配置:

1
2
3
4
public static void Main(string[] args)
{
    CreateHostBuilder(args).UseServiceContext().Build().Run();
}

StartUp

1
services.AddControllersWithViews().AddControllersAsServices();

 注AddControllersAsServices方法,不添加则Controller中属性注入不生效!!! 

==============================注意:属性注入,字段不行==============================

 

[AspectCore.DependencyInjection.FromServiceContext]
private ILogger<PrintController> log { get; set; }

 

 

构造完成才会注入

https://www.cnblogs.com/huawublog/p/14302956.html

posted on 2022-07-28 18:00  隨風.NET  阅读(105)  评论(0编辑  收藏  举报

导航