Skip to main content

Creating Azure Function from Visual Studio 2017 triggered from Azure Service Bus Topic

Recently, I was building an Azure Function from Visual Studio and encountered an issue with the auto generated Function.cs class file.
My requirement was to create an Azure Function that would get triggered as soon as we receive a message on an Azure Service Bus Topic. For this I tried to use the Project template available in Visual Studio 2017.

Unfortunately, when I tried to provide all the required values, and run the function, I got following error:


First you get a warning which says:

Warning: Cannot find value named 'Endpoint=sb:......' in local.settings.json that matches 'connection' property set on 'serviceBusTrigger' in '\Source\repos\FunctionApp1\MyFunction\bin\Debug\net461\Function1\function.json'. You can run 'func azure functionapp fetch-app-settings <functionAppName>' or specify a connection string in local.settings.json.

This warning was followed by an error:

[2/12/2018 9:48:05 AM] A ScriptHost error has occurred
[2/12/2018 9:48:05 AM] Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1.Run'. Microsoft.Azure.WebJobs.ServiceBus: Microsoft Azure WebJobs SDK ServiceBus connection string 'AzureWebJobsEndpoint=sb:....' is missing or empty.
[2/12/2018 9:48:05 AM] Error indexing method 'Function1.Run'
[2/12/2018 9:48:05 AM] Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1.Run'. Microsoft.Azure.WebJobs.ServiceBus: Microsoft Azure WebJobs SDK ServiceBus connection string 'AzureWebJobsEndpoint=sb:/.....' is missing or empty.

When I tried to look for the issue on the web, I could not find a proper solution for my issue. After exploring further, I finally got the fix for this issue. Hence, I thought of sharing the fix with others, who might face similar issues in their projects.

So whenever we create a new function from inside of Visual Studio 2017, we are required to specify the Azure Service Bus Connection string, the topic and subscription name in a dialog box that appears just like one below:


After we enter the required details and hit OK, we get a Function file that's auto-generated for us by the Functions SDK.
That looks something like this:


And the local.settings.json file looks like this:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDashboard": "",
}
}

The auto-generated code has an issue over here, the value of Connection parameter in the Function File, needs to have the name of the settings Key, which holds the actual connection string.

So in order to get rid of the error, we need to make two changes in the auto-generated code:
  • Introduce a setting entry in local.settings.json file, which holds the actual connection string of Azure Service Bus. So your local.settings.json file should look like this:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "Your Storage Account Connection String",
"AzureWebJobsDashboard": "Your Storage Account Connection String",
"MyConnectionKey":"Your Azure Service Bus Connection String"
}
}

  • Update the Function file to hold name of the Settings Key that you have declared in local.settings.json, rather than the actual connection string.

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.ServiceBus.Messaging;
namespace MyFunction
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([ServiceBusTrigger("testtopic", "testsubscription", AccessRights.Manage, Connection = "MyConnectionKey")]string mySbMsg, TraceWriter log)
{
log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
}
}



Now if you run your Project, you would not get the error again, and your Functions App would work as expected.


Hope this is helpful. Happy Coding :)

Comments

Post a Comment

Popular posts from this blog

Handling Concurrency in Azure Functions (HTTP Triggered)

Concurrency & Isolation Concurrency is one of the most common requirements when we don't want the resource to be accessed by multiple requests at the same time. Lets say we have a Database that holds products stock information, then it is very important that any read/write operation that happens on the Database must hold the property of Isolation . The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed sequentially, i.e., one after the other. Providing isolation is the main goal of concurrency control. Depending on the concurrency control method (i.e., if it uses strict – as opposed to relaxed – serializability), the effects of an incomplete transaction might not even be visible to another transaction. Options Available in Azure Functions Recently while working on Azure Functions, I discovered a really simple way of handling Concurrency via code. There are ways to con

Azure Functions - Retrieve data from Dynamics 365 CRM/CE Online using CRM OData Web API

Introduction: This blog describes how you can retrieve data from Dynamics 365 CRM/CE Online in an Azure Function, using CRM OData Web API. The CRM Web API has a couple of advantages over the traditional SOAP based approach to interact with CRM. These are rest based services, its easy to use and transport over http protocol , and there is no need to use additional web resources (XrmServiceToolkit ) of SDK (SDK.REST). Here is a detailed documentation on CRM OData Wbeb API. I will be using an Azure Function Application in Visual Studio 2017 to retrieve records from Dynamics 365 CRM/CE, by making use of OData API Endpoint provided by CRM. Prerequisites: Dynamics 365 CRM Online Trial https://trials.dynamics.com/ https://signup.microsoft.com/Signup?OfferId=bd569279-37f5-4f5c-99d0-425873bb9a4b&dl=DYN365_ENTERPRISE_PLAN1&Culture=en-us&Country=us&ali=1 Azure Subscription related to CRM Online instance https://portal.azure.com/ Visual Studio 2017 with Cl