.net 动态从容器中获取对象实例 IOC 依赖注入
- 创建对象保存ApplicationServices startup中的app参数的ApplicationServices属性
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
Startup.applicationService = app.ApplicationServices;
}
- 在startup中的Configure方法中记录下 app.ApplicationServices
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
Startup.applicationService = app.ApplicationServices;
}
- 获取对象
var machineService= Startup.applicationService.GetService<TestService>();
留待后查,同时方便他人
联系我:renhanlinbsl@163.com
联系我:renhanlinbsl@163.com