gpt4 book ai didi

c# - ASP.NET Core Web 服务不会将 appsettings.json 加载到配置中

转载 作者:可可西里 更新时间:2023-11-01 08:42:50 37 4
gpt4 key购买 nike

我有一个带有 Razor Pages 的 ASP.NET Core 2.1 Web 应用程序,它在 appsettings.json 文件中定义了 AAD 身份验证信息(由默认应用程序模板提供 - 请参阅下文了解我是如何到达那里的).但是,当尝试在 Startup.cs 中配置身份验证时,配置中没有来 self 的 appsettings.json 的任何配置值。如果我在调试器中检查 IConfiguration 对象,那么它似乎只有环境变量配置:

enter image description here

这是问题所在的 Startup.ConfigureServices 方法:

public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options =>
{
// This is from the default template. It should work, but the relevant settings aren't there so options isn't populated.
this.Configuration.Bind("AzureAd", options);

// This of course works fine
options.Instance = "MyInstance";
options.Domain = "MyDomain";
options.TenantId = "MyTenantId";
options.ClientId = "MyClientId";
options.CallbackPath = "MyCallbackPath";
});

services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

以及服务配置,以防它很重要(请注意,这是在服务结构无状态服务之上构建的):

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(opt =>
{
int port = serviceContext.CodePackageActivationContext.GetEndpoint("ServiceEndpoint").Port;
opt.Listen(IPAddress.IPv6Any, port, listenOptions =>
{
listenOptions.UseHttps(GetCertificateFromStore());
listenOptions.NoDelay = true;
});
})
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
}))
};
}

为了创建这个服务,我使用了 VS2017 中的向导。我选择了一个现有的服务结构项目 (.sfproj) 并选择了 Services > Add > New Service Fabric Service 并选择了 Stateless ASP.NET Core [for .NET Framework ],然后在下一页上我选择了 Web Application(带有 Razor Pages,而不是 MVC 的那个)并单击了我选择 Work 的 Change Authentication或学校帐户并输入我的 AAD 信息。我对此模板所做的唯一更改是在 Startup.ConfigureServicesAddAzureAD 调用中添加代码并设置 appsettings.json 文件总是被复制到输出目录。

为什么 appsettings.json 文件没有加载到配置中? 据我了解,默认情况下应该会发生这种情况,但似乎缺少某些内容...

最佳答案

WebHostBuilder 默认不加载appsettings.json,需要手动调用AddJsonFile。例如:

return new WebHostBuilder()
.UseKestrel(opt =>
{
//snip
})
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();

或者您可以使用 WebHost.CreateDefaultBuilder这将加载更多默认值。

关于c# - ASP.NET Core Web 服务不会将 appsettings.json 加载到配置中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53291037/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com