Try to search your question here, if you can't find : Ask Any Question Now ?

Inject configuration into ASP Core correctly

HomeCategory: stackoverflowInject configuration into ASP Core correctly
Avatarbhawya asked 4 months ago

We host a ASP Core web api inside a service fabric instance. I want to forward all configs to the Core app. I use this method

And then I invoke it using ConfigureAppConfiguration

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    return new[]
        new ServiceInstanceListener(serviceContext =>
            new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                return new WebHostBuilder()
                                services => services.AddSingleton(serviceContext))
                            .ConfigureAppConfiguration(builder => builder.AddServiceFabricConfiguration(serviceContext))
                            .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseReverseProxyIntegration)

The inected configration in startup class do have the configs applied, like connection string and logging

enter image description here

But it seems its not fully jacked into the framework.

For example I need to explicit supply ApplicationInsights with the config services.AddApplicationInsightsTelemetry(Configuration); otherwise it does not pickup the instrument key. And the logging does not work (the log level is not applied).


I tried doing a custom config provider in a standard core project without service fabric and it works.

public class Program
    public static void Main(string[] args)

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            .ConfigureAppConfiguration(builder => builder.AddServiceFabricConfiguration())

internal class ServiceFabricConfigurationProvider : ConfigurationProvider

    public override void Load()
        Data["Logging:Debug:LogLevel:Default"] = "Information";

internal class ServiceFabricConfigurationSource : IConfigurationSource

    public IConfigurationProvider Build(IConfigurationBuilder builder)
        return new ServiceFabricConfigurationProvider();

public static class ServiceFabricConfigurationExtensions
    public static IConfigurationBuilder AddServiceFabricConfiguration(this IConfigurationBuilder builder)
        builder.Add(new ServiceFabricConfigurationSource());
        return builder;

So it seems something i service fabric land is not working as it should.

1 Answers
Best Answer
AvatarFernando answered 4 months ago
Your Answer

11 + 17 =

Popular Tags

WP Facebook Auto Publish Powered By :