【问题标题】:Get ServiceBusTrigger name from App.config in Console App从 Console App 中的 App.config 获取 ServiceBusTrigger 名称
【发布时间】:2016-05-11 07:16:26
【问题描述】:

我有接收 ServiceBusTrigger 消息的函数:

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

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

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

【问题讨论】:

    标签: c# azure servicebus


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        【解决方案3】:

        只是@Baywet 的答案的一个实现,我通过@dprothero 找到了here

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

        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();
            }
        }
        

        现在在App.config 文件的&lt;appSettings&gt; 部分添加一个密钥,例如

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

        并像使用它一样

         public async Task ProcessQueueMessage([ServiceBusTrigger("%QueueName%")] BrokeredMessage receivedMessage)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-06
          • 2012-06-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多