2

I creating an Azure Function app with ServiceBusTrigger. I am running Visual Studio under an account which is also used to logging into Visual Studio.

I have added this user account under Service Bus Access Control & assigned Azure Service Bus Data Receiver and Azure Service Bus Data Sender roles.

[FunctionName("Function1")]
        public void Run([ServiceBusTrigger("topic-one", "sub-one", Connection = "ServiceBusConnString")]string mySbMsg)
        {
            var credentail = new DefaultAzureCredential();
            _logger.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
        }

This is how local.settings.json file look:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "ServiceBusConnString__serviceUri": "https://sb-test-three.servicebus.windows.net/",
    "ServiceBusConnString__fullyQualifiedNamespace": "https://sb-test-three.servicebus.windows.net/",
    "ServiceBusConnString": "https://sb-test-three.servicebus.windows.net/ManagedIdentity",
    "ConnectionString": "https://sb-test-three.servicebus.windows.net/"
  }
}

I am getting below error when running the app locally using Visual Studio.

enter image description here

How can I run Azure function from Visual Studio using managed identity? I am not sure how to use DefaultAzureCredential class

OpenStack
  • 5,048
  • 9
  • 34
  • 69
  • can you try changing the `ServiceBusConnString` to `Endpoint=sb://sb-test-three.servicebus.windows.net/;Authentication=ManagedIdentity` ? – Jayendran Oct 01 '22 at 05:34
  • @Jayendran I am not getting following error: `The listener for function FunctionWthMI was unable to start. Azure.Messaging.ServiceBus: The connection string used for an Service Bus client must specify the Service Bus namespace host and either a Shared Access Key (both the name and value) OR a Shared Access Signature to be valid. (Parameter 'connectionString').` . I added following `"ServiceBusConnString": "Endpoint=sb://sb-test-three.servicebus.windows.net/;Authentication=ManagedIdentity",` – OpenStack Oct 01 '22 at 13:57
  • did you make sure the version of `Microsoft.Azure.WebJobs.Extensions.ServiceBus version` 5.x ? https://learn.microsoft.com/en-us/dotnet/api/overview/azure/microsoft.azure.webjobs.extensions.servicebus-readme-pre – Jayendran Oct 01 '22 at 14:12
  • @Jayendran: I am using version 5.7.0 `` – OpenStack Oct 01 '22 at 14:13

1 Answers1

2

Looks like you only need ServiceBusConnString__fullyQualifiedNamespace for the managed identity and you can remove the below configs

 "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "ServiceBusConnString__serviceUri": "https://sb-test-three.servicebus.windows.net/",
    "ConnectionString": "https://sb-test-three.servicebus.windows.net/"

You only need the below

    "FUNCTIONS_WORKER_RUNTIME": "dotnet",    
    "ServiceBusConnString__fullyQualifiedNamespace": "sb-test-three.servicebus.windows.net",

And make sure you identity is having the below Roles

  • Azure Service Bus Data Receiver
  • Azure Service Bus Data Owner

Refer the official docs

Jayendran
  • 9,638
  • 8
  • 60
  • 103
  • No luck with the change. May be I am making some basic mistake. – OpenStack Oct 02 '22 at 18:55
  • whats error you are getting? For me its working fine – Jayendran Oct 03 '22 at 06:08
  • I am getting following error `(Stored credentials not found. Need to authenticate user in VSCode Azure Account. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/vscodecredential/troubleshoot) (Please run 'az login' to set up account) (Az.Account module >= 2.2.0 is not installed.). System.Private.CoreLib: EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot.` – OpenStack Oct 04 '22 at 02:28
  • looks like you have to add your account in vs code using azure account extension. Also the error mentioned the az.account module is not installed with right version you have to install that too – Jayendran Oct 04 '22 at 05:39
  • I am not using Visual Studio code and that is why I am confused why do I need to install extension is VS Code ? To Install AZ.Account module I used ` Install-Module -Name Az.Accounts -RequiredVersion 2.2.3` command. at the time of Installation I am getting warning `You are installing the modules from an untrusted repository` What is the best way to handle this ? – OpenStack Oct 04 '22 at 12:29
  • You can ignore the warning and proceed with the installation. After the installation, Do `az login` and use the interactive login with your creds and try again running the code. Since you are debugging from login you have to login using azure cli using your creds – Jayendran Oct 04 '22 at 12:52
  • Now there are no errors but service bus trigger is not firing even though service bus has 20 messages – OpenStack Oct 04 '22 at 13:23
  • after running the function, can you insert a new message from the azure portal and see whether it triggers – Jayendran Oct 05 '22 at 04:20