【发布时间】:2021-01-22 22:30:47
【问题描述】:
拥有.net core 3.1.8 版和asp.net core 3.1.8 版的.net core worker 服务我在从worker 服务加载用户密码时遇到问题,但是在asp.net core 项目中同样有效。
从 asp.net core 和 .net core worker 服务调用的通用方法
public static string GetFioApiKey(this IConfiguration configuration)
=> configuration["PaymentServiceSecrets:FioApiKey"];
worker服务的csproj
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>8d19c97d-a2fb-4a51-a694-9635b9c0c42c</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.1.8" />
</ItemGroup>
</Project>
worker服务的startup.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config => config.AddUserSecrets(Assembly.GetExecutingAssembly()))
.ConfigureServices((hostContext, services) =>
{
// simplified
services.AddSingleton<IApplicationSettings>(applicationSettings =>
{
var appSettings = new ApplicationSettings
{
FioApiKey = config.GetFioApiKey()
};
return appSettings;
});
services.AddHostedService<Worker>();
});
}
发现这个link 到 github 问题,尝试应用解决方法但仍然不起作用
【问题讨论】:
-
请注意,用户机密只能在开发期间使用 (docs.microsoft.com/en-us/aspnet/core/security/…)
标签: c# asp.net-core .net-core