Having a .net core worker service with .net core version 3.1.8 and asp.net core version 3.1.8 I am having an issue to load user secret from worker service however the same works in asp.net core project.
Common method called from both asp.net core and .net core worker service
public static string GetFioApiKey(this IConfiguration configuration)
=> configuration["PaymentServiceSecrets:FioApiKey"];
csproj of worker service
<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>
startup.cs of worker service
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>();
});
}
found this link to github issue, tried to apply the workaround but still doesn't work