从Console App中的App.config获取ServiceBusTrigger名称

时间:2016-05-11 07:16:26

标签: c# azure servicebus

我有接收ServiceBusTrigger消息的函数:

resize

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

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

3 个答案:

答案 0 :(得分:2)

As a complement to sebbrochet's answer, you'll probably have to implement a custom INameResolver to get the value from the app.config file as this GitHub issue suggests.
https://github.com/Azure/azure-webjobs-sdk/issues/581
Just use %paramname% in the ServiceBus param when you want your custom resolver to kick in.
Hope that helps.

答案 1 :(得分:1)

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

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

在app.config文件中使用这种代码:

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

答案 2 :(得分:0)

仅仅是@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类就像

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

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

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

并像

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