I'm trying to use user secrets because I want to save my key AzureWebJobsStorage
in secrets.json
for local development.
- So the problem is:
I created my user secrets, installed the necessary packages, but my webjob is still trying to use the keys in my appsettings.json
.
What I accomplished so far:
I can read my secrets.json but I don't know where to go from here.
What have I tried
I have search through google but can't manage to find an answer. I saw similar questions here in StackOverflow but they refer to NetCore 2.0 or to something else not useful to me.
My files:
Program.cs
public class Program
{
static async Task Main()
{
var builder = new HostBuilder();
builder
.UseEnvironment("Development")
.ConfigureAppConfiguration((context, b) =>
{
if(context.HostingEnvironment.IsDevelopment())
b.AddUserSecrets<Program>();
})
.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
})
.ConfigureLogging((context, b) =>
{
b.AddConsole();
})
.ConfigureServices((context, b) =>
{
Infrastructure.IoC.DependencyResolver.RegisterServices(b, context.Configuration);
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
}
Functions.cs
public class Functions
{
private readonly ITransactionService _transactionService;
public Functions(ITransactionService transactionService)
{
_transactionService = transactionService;
}
public void ProcessQueueMessage([QueueTrigger("my_queue")] string message, ILogger logger)
{
try
{
Validate.IsTrue(long.TryParse(message, out long transactionId), "Invalid transaction from queue");
_transactionService.CategorizeAllOtherTransactions(transactionId).Wait();
}
catch (System.Exception)
{
logger.LogInformation(message);
}
}
}
.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>7f046fd1-a48c-4aa6-95db-009313bcb42b</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.6" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Yaba.Infrastructure.IoC\Yaba.Infrastructure.IoC.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>