Как прочитать appsetting.json для решения Service Fabric? - PullRequest
0 голосов
/ 11 сентября 2018

У меня есть Service Fabric API в качестве службы, а уровни доступа к бизнесу и данным являются отдельной библиотекой классов.Я устанавливаю значения конфигурации в appsetting.json.Но я не могу прочитать значения на бизнес-уровне и уровнях доступа к данным.Кроме того, я не хочу использовать переменные среды.Как я могу прочитать appsetting.json на уровне доступа к данным и бизнес-уровне?

Ответы [ 2 ]

0 голосов
/ 12 сентября 2018

добавить эту строку в CreateServiceInstanceListeners

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

                    return new WebHostBuilder()
                                .UseKestrel()
                                .UseCommonConfiguration()
                                .ConfigureServices(
                                    services => services
                                        .AddSingleton<StatelessServiceContext>(serviceContext))
                                .UseContentRoot(Directory.GetCurrentDirectory())
                                .UseStartup<Startup>()
                                .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                .UseUrls(url)
                                .Build();
                }))
        };
    }

UseCommonConfiguration

        public static IWebHostBuilder UseCommonConfiguration(this IWebHostBuilder builder)
    {
        builder.ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;

            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

            if (env.IsDevelopment())
            {
                var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                if (appAssembly != null)
                {
                    config.AddUserSecrets(appAssembly, optional: true);
                }
            }

            config.AddEnvironmentVariables();
        });

        return builder;
    }
0 голосов
/ 11 сентября 2018

Передайте объект IConfiguration в качестве аргумента конструктора вашему контроллеру API.Передайте его вниз по течению другим классам.

...