In one of my concrete class. I have the method.
public class Call : ICall
{
......
public Task<HttpResponseMessage> GetHttpResponseMessageFromDeviceAndDataService()
{
var client = new HttpClient();
var uri = new Uri("http://localhost:30151");
var response = GetAsyncHttpResponseMessage(client, uri);
return response;
}
Now I put the url into appsettings.json.
{
"AppSettings": {
"uri": "http://localhost:30151"
}
}
And I created a Startup.cs
public class Startup
{
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
}
and now I get stuck.
EDIT
By the way, I don't have a controller, it is a console application.
The preferred way to read configuration from appSettings.json is using dependency injection and the built or (or 3rd party) IoC container. All you need is to pass the configuration section to the
Configure
method.This way you don't have to manually set the values or initialize the
AppSettings
class.And use it in your service:
The IoC Container can also be used in a console application, you just got to bootstrap it yourself. The
ServiceCollection
class has no dependencies and can be instantiated normally and when you are done configuring, convert it to anIServiceProvider
and resolve your main class with it and it would resolve all other dependencies.Create a static class
then access the settings anywhere you want like
appsettings.json example
You can access data from the IConfigurationRoot as following:
Like suggested in the comment I would put the information in a seperate class for that info and pass it into the DI container.
the class
DI
Controller