Read appsetting.json content from other project
See original GitHub issueI’m running v3.7.2 of the boilerplate. and the name of my project is Prime
I’m trying to read the appsetting.json
values located in the project Prime.Web.Host and read it values in the project Prime.Core but I’ve not been able to get it to work. I followed this as a guide https://stackoverflow.com/questions/48948905/how-to-access-appsettings-from-another-project?noredirect=1&lq=1
my appsetting.json
looks something like this.
"AppSettings": {
"IBS": {
"SharedKey": "dadaoo88dsfladfa8adsa",
"SharedVector": "iudafdfaddd",
"ApplicationId": 54344
},
"CoreBankingService": {
"AccountOpening": {
"DefaultBranchCode": "kkd8873jjdaf",
"DefaultAccountManager": "kakjdkjfa5583mdfa"
}
},
"ThirdPartyService": {
"BaseUrl": "http://localhost:3456/",
"Spay": {
"BaseUrl": "http://localhost:3456/SPay/",
"SwitchId": "66553557477dad",
"AppId": "jhhjd66a5"
}
}
}
I defined a POCO to mimic this as shown below;
namespace Prime
{
public class AppSettings
{
public IBS IBS { get; set; }
public CoreBankingService CoreBankingService { get; set; }
public ThirdPartyService ThirdPartyService { get; set; }
}
public class IBS
{
public string SharedKey { get; set; }
public string SharedVector { get; set; }
public int ApplicationId { get; set; }
}
public class CoreBankingService
{
public AccountOpening AccountOpening { get; set; }
}
public class ThirdPartyService
{
public string CreditSearchBaseUrl { get; set; }
public Spay Spay { get; set; }
}
public class Spay
{
public string BaseUrl { get; set; }
public string SwitchId { get; set; }
public string AppId { get; set; }
}
public class AccountOpening
{
public string DefaultBranchCode { get; set; }
public string DefaultAccountManager { get; set; }
}
}
In Prime.Web.Host project, my Startup.cs
file looks something like this;
public class Startup
{
private const string DefaultCorsPolicyName = "localhost";
private readonly IConfigurationRoot _appConfiguration;
public Startup(IHostingEnvironment env, IConfiguration configuration)
{
_appConfiguration = env.GetAppConfiguration();
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//var settingsSection = _appConfiguration.GetSection($"AppSettings");
//services.Configure<AppSettings>(settingsSection);
// services.AddSingleton(_appConfiguration.GetSection("AppSettings").Get<AppSettings>());
services
.Configure<AppSettings>(_appConfiguration)
.AddSingleton(sp => sp.GetRequiredService<IOptions<AppSettings>>().Value);
// MVC
services.AddMvc(
options => options.Filters.Add(new CorsAuthorizationFilterFactory(DefaultCorsPolicyName))
);
//...other code...
}
//...other code...
}
Now when I try to incject AppSettings
into any class, I always get null
as the values of the properties of AppSettings
public class CoreBankingService : ICoreBankingService
{
private readonly AppSettings _appSettings;
public CoreBankingService(AppSettings appSettings)
{
_appSettings = appSettings;
var foo = _appSettings.IBS //all properties of _appSettings is null
}
}
All the properties in _appSettings
is null
. What exactly am I doing wrong? I don’t want to use AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder())
as this does not resolve to the correct root folder when I try to deploy from VSTS to a private agent pool.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
Under TestModule PreInitialize
Of course, you can also read appsetting.json in the unit test, I did not try but it should be no problem.
https://weblog.west-wind.com/posts/2018/Feb/18/Accessing-Configuration-in-NET-Core-Test-Projects
Oh I saw my mistake. I was using
IOptionSnapShot<AppSettings>
instead ofIOptions<AppSettings>
Thank you @maliming