I have created a new Web API which contains a Program.cs (no startup.cs) and uses top level statements (i.e. does not have public class Program).
How do I add dependency injection to allow IOptions to be passed to any constructors I create? I have this working using the old Startup.cs, but I seem to be I'm missing something with the new format.
I have added
Program.cs:
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
builder.Services.Configure<IOptions<DbConfig>>(config.GetSection("ConnectionStrings"));
The class constructor looks like this:
public SqlClient(IOptions<DbConfig> Config)
{
_configuration = Config;
}
Any calls to this function are showing an error "There is no argument given that corresponds to the parameter Config", i.e. it want's me to pass that parameter in from the calling code.
DbConfig has the 4 properties from the appsettings.json DatabaseConnection section. Appsettins.json:
"DatabaseConnection": {
"DataSource": "xx.xx.xx.xx",
"UserID": "ID",
"Password": "PW",
"InitialCatalog": "DB"
}