Enrico
Enrico

Reputation: 6256

Azure Service Bus and Azure Function Queue Trigger

I have an issue to create an Azure Function with Queue Trigger in .NET core 2.0.

What I want

In a microservice architecture, when a new message is created in a queue for creating a user, a service has to receive this message and creates a user in a database based on the information in it.

Issue

In Visual Studio 2017 I create a new project under Azure Function.

Visual Studio Add New Project

From the New Template I select Queue Trigger.

Visual Studio New Template

This screen is different from a Microsoft post about this subject. Anyway, first issue is what Connection and Path are.

I created a Service Bus and I have my credential from Azure Portal. I copied Primary Connection String and Path is the name of my queue.

If I run the project, I receive a lot of errors. I found this link on Azure Documentation for a simple trigger. In this example they use ServiceBusTrigger instead of QueueTrigger. For resolving ServiceBusTrigger, I added Microsoft.Azure.WebJobs.ServiceBus from Nuget. It seems it doesn't work.

Azure Portal Service Bus

To have the right credential for my Azure Function, I created one in Azure Portal and downloaded the app content.

Azure Portal Function

It looks like

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_EXTENSION_VERSION": "beta",
    "ScmType": "None",
    "WEBSITE_AUTH_ENABLED": "False",
    "AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=my;AccountKey=something",
    "WEBSITE_NODE_DEFAULT_VERSION": "6.5.0",
    "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING": "DefaultEndpointsProtocol=https;AccountName=my;AccountKey=something",
    "WEBSITE_CONTENTSHARE": "createprofile-98873b60",
    "WEBSITE_SITE_NAME": "CreateProfile",
    "WEBSITE_SLOT_NAME": "Production",
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=createprofil8796;AccountKey=something"
  },
  "configurationSource": "config",
  "bindings": [
    {
      "type": "serviceBusTrigger",
      "connection": "sb://myservicebus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=something",
      "queueName": "testqueue",
      "accessRights": "manage",
      "name": "myQueueItem",
      "direction": "in"
    }
  ]
}

This is the error page I have

Error Page

Warning: Cannot find value named 'https://MyDevServiceBus.servicebus.windows.net' in local.settings.json that matches 'connection' property set on 'queueTrigger' in 'C:\Projects\CustomerProfile.AzureService\bin\Debug\netstandard2.0\Function1\function.json'. You can run 'func azure functionapp fetch-app-settings ' or specify a connection string in local.settings.json. [12/03/2018 12:10:41] Reading host configuration file 'C:\Projects\CustomerProfile.AzureService\bin\Debug\netstandard2.0\host.json' [12/03/2018 12:10:41] Host configuration file read: [12/03/2018 12:10:41] { [12/03/2018 12:10:41] } [12/03/2018 12:10:42] Generating 1 job function(s) [12/03/2018 12:10:42] Starting Host (HostId=desktop7fksikf-631144646, Version=2.0.11353.0, ProcessId=8992, Debug=False, Attempt=0, FunctionsExtensionVersion=beta) Listening on http://localhost:7071/ Hit CTRL-C to exit... [12/03/2018 12:10:42] A ScriptHost error has occurred [12/03/2018 12:10:42] Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1.Run'. Microsoft.Azure.WebJobs.Host: Microsoft Azure WebJobs SDK 'QueueConnection' connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways: [12/03/2018 12:10:42] 1. Set the connection string named 'AzureWebJobsQueueConnection' in the connectionStrings section of the .config file in the following format , or [12/03/2018 12:10:42] 2. Set the environment variable named 'AzureWebJobsQueueConnection', or [12/03/2018 12:10:42] 3. Set corresponding property of JobHostConfiguration. [12/03/2018 12:10:42] Error indexing method 'Function1.Run' [12/03/2018 12:10:42] Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1.Run'. Microsoft.Azure.WebJobs.Host: Microsoft Azure WebJobs SDK 'QueueConnection' connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways: [12/03/2018 12:10:42] 1. Set the connection string named 'AzureWebJobsQueueConnection' in the connectionStrings section of the .config file in the following format , or [12/03/2018 12:10:42] 2. Set the environment variable named 'AzureWebJobsQueueConnection', or [12/03/2018 12:10:42] 3. Set corresponding property of JobHostConfiguration. [12/03/2018 12:10:42] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.). [12/03/2018 12:10:42] Job host started [12/03/2018 12:10:42] The following 1 functions are in error: [12/03/2018 12:10:42] Run: Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1.Run'. Microsoft.Azure.WebJobs.Host: Microsoft Azure WebJobs SDK 'QueueConnection' connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways: [12/03/2018 12:10:42] 1. Set the connection string named 'AzureWebJobsQueueConnection' in the connectionStrings section of the .config file in the following format , or [12/03/2018 12:10:42] 2. Set the environment variable named 'AzureWebJobsQueueConnection', or [12/03/2018 12:10:42] 3. Set corresponding property of JobHostConfiguration. [12/03/2018 12:10:42] [12/03/2018 12:10:42] [12/03/2018 12:10:42] Host lock lease acquired by instance ID '000000000000000000000000E37F5049'.

What is the correct implementation of this kind of Azure Function? Is there any example on GitHub? Thanks in advance.

Upvotes: 3

Views: 9162

Answers (2)

Paul Batum
Paul Batum

Reputation: 8415

Since you mentioned in a comment that you were having problems finding the service bus queue trigger template, here is what it looks like - as Mikhail mentioned in his answer, fill in the Connection="" with the name of the app setting that has your connection string

enter image description here

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.ServiceBus.Messaging;

namespace FunctionApp11
{
    public static class Function2
    {
        [FunctionName("Function2")]
        public static void Run([ServiceBusTrigger("myqueue", AccessRights.Manage, Connection = "")]string myQueueItem, TraceWriter log)
        {
            log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
        }
    }
}

Upvotes: 0

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35144

You are confusing Azure Service Bus and Azure Storage Queues. Queue Trigger is for Storage Queues, so Connection is the connection string for Storage Account and Path is for queue path.

To use Service Bus, you should use Service Bus Trigger. It takes a bit more effort to setup that one on .NET Core / v2 version of functions, since the Visual Studio tooling is not available for that yet.

I would recommend using Functions v1 for Service Bus for the time being.

ServiceBusTrigger is the correct type to use. You should set its Connection property to the name of your setting which contains Service Bus connection string, not to connection string itself. Thus

  1. Put ServiceBusTrigger on your function definition.
  2. Add a setting called MyServiceBusConnection (or any other name you like). Set it to sb://myservicebus.servicebus.windows.net/;...
  3. Reference the setting name from the trigger definition, e.g. [ServiceBusTrigger("myqueue", AccessRights.Manage, Connection = "MyServiceBusConnection")]

Upvotes: 8

Related Questions