azure Net 6.0从appsettings而不是local.settings.json设置ServiceBusTrigger QueueName和Connection

rkue9o1l  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(97)

我有一个ServiceBusTrigger
public async Task Run( [ServiceBusTrigger("%QueueName%", Connection = "ServiceBus")]
我可以在“值”部分的local.settings.json中设置QueueName和Connection

`"Values": {
    "QueueName": "SbQueueName",    
    "ServiceBus": "Endpoint=sb:...."
  },`

如何从单独的自定义appsettings配置中设置QueueName和Connection,以便能够使用自定义设置节命名

"CustomSectionName": {    
    "QueueName": "SbQueueName",
    "ServiceBus": "Endpoint=sb:...."
  }
xfb7svmp

xfb7svmp1#

要从appsettings.json中的自定义命名节中检索值,您需要实现一种机制来访问此节并通过运行时绑定将其传递给Azure Function。
在appsettings文件的根目录中设置它们并给予它们一个自定义名称可能会更简单,更直接,如下所示:代码:

public async Task Run( [ServiceBusTrigger("%CustomQueueName%", Connection = "CustomServiceBus")]

appsettings.json:

{
   "CustomQueueName": "SbQueueName",
   "CustomServiceBus": "Endpoint=sb:...."
}

至于使用自定义的appsettings配置文件,请在Startup.cs中使用以下行:

public override void Configure(IFunctionsHostBuilder builder)
    {
        var services = builder.Services;
        var context = builder.GetContext();

        var config = new ConfigurationBuilder()
            .AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), false)
            // Custom appsettings
            .AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.Custom.json"), false) // Or whatever name you'd like
            .AddEnvironmentVariables();
       ...
       }

相关问题