Configuration in ASP.NET Core
When you create stateless service fabric asp.net core api, the template doesn’t add configuration builder by default. I’ve spent about an hour pulling my hair why it doesn’t load appsettings.json on start up. So, Add it first.
// from CreateServiceInstanceListeners()
return new WebHostBuilder()
.UseKestrel()
.ConfigureAppConfiguration(
(context, config) => config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true))
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
If you want typed config setting, create a settings class
public class SfUris
{
public string QueryService { get; set; }
public string CommandService { get; set; }
}
This class will be newed up by Services collection in startup.
services.Configure<SfUris>(Configuration.GetSection("SfUris"));
services.AddSingleton(r => r.GetRequiredService<IOptions<SfUris>>().Value);
By returning the type with IOptions
Then you can use the typed config setting.
services.AddTransient(x => x.GetService<IProxyFactory>()
.Create<ICommandService>(x.GetService<SfUris>().CommandService));
Comments