I have a Class Library that is referenced from a ASP.Net Core (targeting 4.5) and a 4.5 web app and I want to have shared App Settings. I am using Unity for DI on the 4.5 side, and Core Bootstrapping on the Core side. On the Core side, I register a type of my App Settings like so
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
I then reference AppSettings like so
private readonly AppSettings _appSettings;
public SubmissionRepository(PricingContext dbContext, IMapper mapper, IOptions<AppSettings> appSettings)
{
_appSettings = appSettings;
}
I want to register my services on the 4.5 side, I was able to do this in WebApiConfig.cs with Unity
container.RegisterType<AppSettings>(new InjectionFactory(o => BuildAppSettings()));
where BuildAppSettings just populates an instance of that type using ConfigurationManager (not sure if this is the right way to do it)
However I get an Exception at Runtime
Unable to cast object of type '.Core.Integration.Models.AppSettings' to type 'Microsoft.Extensions.Options.IOptions`1[Core.Integration.Models.AppSettings]'.
I am guessing I need to somehow put IOptions instance into my container, but not sure how to do this. Is there a preferred/better way to do this?
If I would have Googled for 10 more seconds, I would have found this
Options.Create(new AppSettings{
// config reads
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.