如何阅读 Service Fabric 解决方案的 appsetting.json?
How to read appsetting.json for Service Fabric solution?
我有一个 Service Fabric API 作为服务,业务和数据访问层是一个单独的 class 库。我在 appsetting.json 中设置配置值。但是我无法读取业务层和数据访问层中的值。另外,我不想使用环境变量。如何读取数据访问层和业务层的appsetting.json?
将 IConfiguration
object 作为构造函数参数传递给 API 控制器。将其向下游传递给其他 类.
将此行添加到 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;
}
我有一个 Service Fabric API 作为服务,业务和数据访问层是一个单独的 class 库。我在 appsetting.json 中设置配置值。但是我无法读取业务层和数据访问层中的值。另外,我不想使用环境变量。如何读取数据访问层和业务层的appsetting.json?
将 IConfiguration
object 作为构造函数参数传递给 API 控制器。将其向下游传递给其他 类.
将此行添加到 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;
}