在控制台应用程序中从 App.config 获取 ServiceBusTrigger 名称

Get ServiceBusTrigger name from App.config in Console App

我有接收 ServiceBusTrigger 消息的功能:

public static void ProcessQueueMessage([ServiceBusTrigger("mysb")] string message)
{
    // do something with message
}

当我向 "mysb" Azure Que 发送消息时 - 此功能开始工作。

我的问题是:我可以从App.config中获取阙名吗?

这个 SO question 似乎回答了你的问题。

您将使用此代码:Microsoft.WindowsAzure.CloudConfigurationManager.GetSettings("QueueName")

在您的 app.config 文件中使用此类代码:

<configuration>
  <appSettings>
   <add key="QueueName" value="mysb"/>
  </appSettings>
</configuration>

作为对 sebbrochet 回答的补充,您可能需要实现自定义 INameResolver 以从 app.config 文件中获取值,正如这个 GitHub 问题所暗示的那样。
https://github.com/Azure/azure-webjobs-sdk/issues/581
当您希望自定义解析器启动时,只需在 ServiceBus 参数中使用 %paramname%。
希望对您有所帮助。

只是@Baywet 的答案的一个实现,我发现了@dprothero here

在您的 Program.cs 文件的 Main 方法中,在注册 Microsoft.Azure.WebJobs.JobHostConfiguration 时向 NameResolver 属性 提供 INameResolver 的实现,例如

static void Main()
{
    var config = new JobHostConfiguration
    {
        NameResolver = new QueueNameResolver()
    };

    //other configurations
}

QueueNameResolver class 就像

public class QueueNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        return ConfigurationManager.AppSettings[name].ToString();
    }
}

现在在 App.config 文件的 <appSettings> 部分添加一个密钥,例如

<appSettings>
    <add key="QueueName" value="myqueue" />
</appSettings>

并像

一样使用它
 public async Task ProcessQueueMessage([ServiceBusTrigger("%QueueName%")] BrokeredMessage receivedMessage)