【问题标题】:How to read appsetting.json for Service Fabric solution?如何读取 Service Fabric 解决方案的 appsetting.json?
【发布时间】:2019-02-16 02:14:13
【问题描述】:

我有一个 Service Fabric API 作为服务,业务和数据访问层是一个单独的类库。我在 appsetting.json 中设置了配置值。但我无法读取业务层和数据访问层中的值。另外,我不想使用环境变量。如何读取数据访问层和业务层的appsetting.json?

【问题讨论】:

    标签: .net-core microservices azure-service-fabric appsettings


    【解决方案1】:

    将此行添加到 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();
                    }))
            };
        }
    

    使用通用配置

            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;
        }
    

    【讨论】:

      【解决方案2】:

      IConfiguration object 作为构造函数参数传递给您的 API 控制器。将其传递给下游的其他类。

      【讨论】:

      • 我作为参数传递。但我没有得到 appsetting.json 文件中的值。
      • 使用普通的 api 就可以了。但是使用服务结构它不起作用。
      猜你喜欢
      • 2018-07-16
      • 2022-01-01
      • 2023-03-07
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2018-02-14
      相关资源
      最近更新 更多