小编Pet*_*ter的帖子

在 .NET 6 应用程序中的 Program.cs(启动)中获取 Key Vault ServiceClient

我创建了一个实体框架 DbContext,并在程序中对其进行初始化(.NET 6 中不再需要 Startup.cs)。连接字符串取决于 Azure Key Vault 内的 SQL 密码。因此 Key Vault 和 DbContext 的注册都是在 Program.cs 中完成的。但是如何从 KeyVault 检索秘密并将其放入连接字符串中?我不想在这里手动创建 SecretClient 实例,但以某种方式注入它......

我现在有:

builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddSecretClient(new Uri(builder.Configuration.GetSection("KeyVault:VaultUri").Value))
        .WithCredential(new DefaultAzureCredential());
});
builder.Services.AddDbContext<MyContext>(options =>
{
    // How to get an instance of SecretClient here via DI? I need to call the serviceClient.GetSecret("sql-password") here to put it inside the connectionstring
    // var sqlPassword = builder.Configuration.GetSection("sql-password").Value <-- also returns null
    var sqlPassword = "get-this-password-from-key-vault";
    options.UseSqlServer(string.Format(builder.Configuration.GetConnectionString("MyConnectionString"), sqlPassword));
});
Run Code Online (Sandbox Code Playgroud)

我在几篇博客上读到,当您将 SecretClient 添加到服务时,它应该读取所​​有机密并将它们添加到 builder.Configuration 中,这意味着您应该能够像这样读取它:

var …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection asp.net-web-api azure-keyvault .net-6.0

5
推荐指数
1
解决办法
1万
查看次数

更新后ServiceBusTrigger无法正常工作

我已经安装了NuGet软件包Micorosft.Azure.WebJobs.ServiceBus,版本1.0.1(2015年3月19日).我的WebJob完全触发了servicebus队列上的新消息:

public static Task ProcessQueueMessage([ServiceBusTrigger("outbound")] BrokeredMessage message, TextWriter log)
Run Code Online (Sandbox Code Playgroud)

我用版本1.1.0(2015年11月19日)更新了NuGet包.现在,不再识别此触发器方法:

找不到工作职能.尝试公开您的工作类和方法.如果您正在使用绑定扩展(例如ServiceBus,Timers等),请确保您已在启动代码中调用扩展的注册方法(例如config.UseServiceBus(),config.UseTimers()等).

有没有其他人遇到过这个问题?

c# servicebus azure azure-webjobssdk webjob

3
推荐指数
1
解决办法
1835
查看次数