Skip to content

Commit

Permalink
Loading options from appsettings
Browse files Browse the repository at this point in the history
  • Loading branch information
Yety committed Jan 31, 2024
1 parent 85eda8c commit 32f74aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
18 changes: 7 additions & 11 deletions RenacciBackend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
using RenacModbus;

var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration;
var appSettingsSection = configuration.GetRequiredSection("RenacModbusOptions");
var appSettings = appSettingsSection.Get<RenacModbusOptions>();
if(appSettings == null)
throw new Exception("Failed to load RenacModbusOptions from configuration");

var services = builder.Services;
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddTransient(_ => new RenacModbusOptions
{
DeviceId = 1,
Host = "192.168.178.79",
Port = 8899
});
services.AddTransient((_) => appSettings);

services.AddTransient<RandomByteGenerator>();
services.AddTransient<TcpClientHandler>();
services.AddTransient<IStatusRepository, RenacStatusRepository>();
Expand All @@ -35,8 +36,3 @@
app.MapControllers();

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
7 changes: 6 additions & 1 deletion RenacciBackend/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"RenacModbusOptions" : {
"Host": "10.10.10.10",
"Port": 502,
"DeviceId": 1
}
}

0 comments on commit 32f74aa

Please sign in to comment.