1
我具有类似于这个代码在Startup.cs在ConfigureServices()中仍然可以访问强类型设置吗?
services.Configure<AppSettings>(
Configuration.GetSection("AppSettings"));
services.AddScoped<IMyService, MyService>();
services.AddScoped((_) => MyFactory.Create(
Configuration["AppSettings:Setting1"],
Configuration["AppSettings:Setting2"],
Configuration["AppSettings:Setting3"]));
我想的AppSettings的实例传递到MyFactory.Create()。这样的实例是否可用?有没有办法得到它的一个实例?
我想消除我当前代码中的冗余,并利用我的AppSettings类的一些好处(例如,它有一些默认设置和一些方便的只读属性,这些属性是其他属性的函数)。
它可能是这个样子:
services.Configure<AppSettings>(
Configuration.GetSection("AppSettings"));
var appSettings = ???;
services.AddScoped<IMyService, MyService>();
services.AddScoped((_) => MyFactory.Create(appSettings));
在什么地方的 “???” 去?
这看起来像我想要的!我将在接下来的几天内实施,并在确认时将其标记为答案。谢谢。 – Chris
@Chris是否为您解决问题? –
当然可以!标记为答案。抱歉耽搁了。 – Chris