SATHEESH  P
SATHEESH P

Reputation: 429

Dotnet Core Read Port From Appsettings.json

I have some Dotnet core api services. The services have to read the configuration and ports from the appsettings.json. I need to set different ports for the services and read from the common json file. How to do it and how to read the ports from the common json file.Any one try to help me.

Thank you..

Upvotes: 0

Views: 1669

Answers (1)

Ali Dogan
Ali Dogan

Reputation: 116

Startup.cs

public IConfiguration _configuration { get; }

public Startup(IConfiguration configuration)
{
       _configuration = configuration;`
}

in Configure Services

services.Configure<ClassName>(Configuration.GetSection("Port")); 

where you want to use

private readonly IOptions<ClassName> _options;

public ConfigService(IOptions<ClassName> options)
{
    _options = options;
}

'ClassName' should be the model to match in appsettings.json.

Upvotes: 3

Related Questions