C# 统一使用IOptions

C# 统一使用IOptions,c#,.net,dependency-injection,unity-container,C#,.net,Dependency Injection,Unity Container,我有一个从ASP.Net核心(目标为4.5)和4.5 web应用引用的类库,我希望有共享应用设置。我在4.5端使用Unity进行DI,在核心端使用核心引导。在核心端,我注册了一种我的应用程序设置,如下所示 services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); private readonly AppSettings _appSettings; public SubmissionReposi

我有一个从ASP.Net核心(目标为4.5)和4.5 web应用引用的类库,我希望有共享应用设置。我在4.5端使用Unity进行DI,在核心端使用核心引导。在核心端,我注册了一种我的应用程序设置,如下所示

services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
private readonly AppSettings _appSettings;
public SubmissionRepository(PricingContext dbContext, IMapper mapper, IOptions<AppSettings> appSettings)
{
     _appSettings = appSettings;
}
services.Configure(Configuration.GetSection(“AppSettings”);
然后我像这样引用AppSettings

services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
private readonly AppSettings _appSettings;
public SubmissionRepository(PricingContext dbContext, IMapper mapper, IOptions<AppSettings> appSettings)
{
     _appSettings = appSettings;
}
private readonly AppSettings\u AppSettings;
公共提交存储库(PricingContext dbContext、IMapper映射器、IOptions应用程序设置)
{
_appSettings=appSettings;
}
我想在4.5端注册我的服务,我可以用Unity在WebApiConfig.cs中注册

container.RegisterType<AppSettings>(new InjectionFactory(o => BuildAppSettings()));
container.RegisterType(新注入工厂(o=>BuildAppSettings());
其中BuildAppSettings仅使用ConfigurationManager填充该类型的实例(不确定这是否正确)

然而,我在运行时得到一个异常

无法强制转换类型为“.Core.Integration.Models.AppSettings”的对象 打字 'Microsoft.Extensions.Options.IOptions'1[Core.Integration.Models.AppSettings]'


我猜我需要以某种方式将IOptions实例放入我的容器中,但不确定如何做到这一点。有更好的方法吗?

如果我再搜索10秒钟,我就会找到这个

Options.Create(new AppSettings{
    // config reads
});

下面是我使用的一个工作单例硬编码示例:
Container.RegisterInstance(Options.Create(newmemorydistributedCacheOptions())