Search code examples
c#azureazure-functionsazure-functions-runtimeazure-function-app

Is it possible to Pass Azure Function ServiceBusTrigger connection at runtime


My Code Look like this :

public class Startup : IWebJobsStartup
{

  public async void Configure(IWebJobsBuilder builder)
   {

   Get Connection string Via HTTP Service.

       ServiceBusConnectionString = jArray["connectionString"].ToString();

   }

}

And I want to Pass Connection Like this

[FunctionName("FunctionTopicMessageLogger")]
        public  void Run([ServiceBusTrigger("topic", "FunctionTopicMessageLogger",Connection = **ServiceBusConnectionString** )]Message mySbMsg)
        {

        }

This is my Local.setting.json file for first time

{
  "IsEncrypted": false,
  "Values": {

    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "Connectinstring": " "

  }
} 

Problem is that connection only take Const Parameter (So it's not allow to change) So i am trying to write in a local.setting.json and trying to read it from there, I am getting error first time when i am running , and from second time connection is there so it's working fine.

Is i am doing something wrong Please suggest.

enter image description here


Solution

  • There are two important parts in the solution for your problem.

    1. During creation of Azure Function with Service Bus trigger you have to specify the name of the connection string parameter that will be used to retrieve connection string value. In this case if you type "ServiceBusConnectionString" you have to add such configuration parameter in the local.settings.json file in the "ConnectionStrings" section:

      {
         "IsEncrypted": false,
         "Values": {
                      "AzureWebJobsStorage": "UseDevelopmentStorage=true",
                      "FUNCTIONS_WORKER_RUNTIME": "dotnet"
                    },
      
       "ConnectionStrings": {
                   "ServiceBusConnectionString": "xxx"
        }
      }
      
    2. After depoying your function to Azure, you should use "Configuration" section in the portal and you should avoid using "local.settings.json" file because it is only for local development:

    enter image description here

    I hope this will help.