Last post Jun 28, 2018 12:23 PM by AnyUserNameThatLetsMeIn
Jun 28, 2018 09:06 AM|AnyUserNameThatLetsMeIn|LINK
I'm not looking for anything complex here but, every time I read another page about this, it starts wading into stuff that is way too much for what I need. My web service creating in ASP.NET Core 2.1 needs to have a config file. In simpler times, I would
have a config.xml or something. It will contain info such as:
<?xml version="1.0" encoding="utf-8" ?>
<add key="UserId" value="user1" />
<add key="UserPassword" value="Password123" />
<add key="InterfaceVersion" value="1.0" />
<add key="InterfaceName" value="WebApi" />
<add key="TargetServer" value="localhost" />
<add key="UserHTTPS" value="N" />
<add key="LogFilePath" value="./ApiLogs/" />
When the web api is called, it needs to read the values from this config file. Don't worry about the password being in plain text. I have 2 controllers in my api that pick up HTTPPost data. During that call, the config needs to be read to pick up the info
in it. That cannot be complicated can it? Can someone point me in the right direction or give me an example?
Jun 28, 2018 11:07 AM|mgebhard|LINK
There is no getting around using the standard ASP Core configuration convention. That means reading and understanding the docs.
Configuration is stored in the appsetting.json file. The configuration object is injected using DI.
Create a simple project to play with configuration. Once you are able to read from configuration files, then add environments to the mix. Create a appsetting.development.json, appsetting.staging.json, and appsetting.production.json. The environment is
driven by the ASPNETCORE_ENVIRONMENT variable which you can find in the project properties under debug. Change development to staging to see what happens.
Next, move all of the sensitive configuration to "User Secrets" on your development machine and to environment variables on the web servers.
Keep the other stuff in appsetting.XXX.json. For example, my staging server has an environment variable ASPNETCORE_ENVIRONMENT = Staging. I do this once and any successive deployments invoke Staging settings.
Jun 28, 2018 12:23 PM|AnyUserNameThatLetsMeIn|LINK
That works :)