简体   繁体   中英

Azure Function (Service Bus Trigger) Not Getting started when a new message comes into the service bus queue

Created an Azure Function which is a service bus triggered in Visual Studio and published to Azure from Visual Studio.

Whenever a message goes to queue, the function is running fine from local when manually run. But the expectation is the function should automatically trigger when a message is in the queue.

I am just adding a new message manually and seeing the logs if the function got triggered automatically but it is not. When I checked the Application Insight I found the below error logs

The listener for function 'ProcessVideos' was unable to start. Service Bus account connection string 'connection' does not exist. Make sure that it is a defined App Setting.*"

Code for local.settings.json where the Service Bus connection string is set.

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "connection": "Endpoint=sb://videoupload10000.servicebus.windows.net/;SharedAccessKeyName=Listen;SharedAccessKey=80n8a0MCmh+3UZN4+4B7gDy4gp3hKCxfDI/9urDmaP8=;"
  }
}

Code for the actual function.

using System;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace ReceiveMessages
{
    public static class Process
    {
        private static string blob_connection_string = "DefaultEndpointsProtocol=https;AccountName=videostorage1000;AccountKey=y6CVtXafqKuShZuv6BMbVj9DrymzVdNDpjDVxp6hZMvuRRjcCz/i8TrOGfM5T/JCvfG33sY3xqqW+ASt3p6V+Q==;EndpointSuffix=core.windows.net";
        private static string source_container_name = "unprocessed";
        private static string destination_container_name = "processed";

        private static readonly string _connection_string = "AccountEndpoint=https://videodbupdate.documents.azure.com:443/;AccountKey=gmR051bG7uq7o2i519m7J9nh6tb4LLctfOQ3nPMUxMu9QJWsmh1SPiY8ylvxoY3bn7kWR4cS2qwanBdIoXSrpg==;";
        private static readonly string _database_name = "appdb";
        private static readonly string _container_name = "video";

        [FunctionName("ProcessVideos")]
        public static async Task Run([ServiceBusTrigger("videoqueue", Connection = "connection")]ServiceBusReceivedMessage myQueueItem, ILogger log)
        {
            ReceivedMessage _message = JsonSerializer.Deserialize<ReceivedMessage>(Encoding.UTF8.GetString(myQueueItem.Body));
            
            BlobServiceClient _client = new BlobServiceClient(blob_connection_string);
            BlobContainerClient _source_container_client = _client.GetBlobContainerClient(source_container_name);
            BlobClient _source_blob_client = _source_container_client.GetBlobClient(_message.VideoName);

            BlobContainerClient _destination_container_client = _client.GetBlobContainerClient(destination_container_name);
            BlobClient _destination_blob_client = _destination_container_client.GetBlobClient(_message.VideoName);

            CosmosClient _cosmosclient = new CosmosClient(_connection_string, new CosmosClientOptions());
            Container _container = _cosmosclient.GetContainer(_database_name, _container_name);

            BlobDownloadInfo _info = _source_blob_client.Download();
            // Copy the blob to the destination container
            await _destination_blob_client.StartCopyFromUriAsync(_source_blob_client.Uri);

            log.LogInformation(_info.Details.LastModified.ToString());
            log.LogInformation(_info.ContentLength.ToString());

            BlobDetails _blobdetails = new BlobDetails();
            _blobdetails.BlobName = _message.VideoName;
            _blobdetails.BlobLocation = "https://videostorage100.blob.core.windows.net/processed/" + _message.VideoName;
            _blobdetails.ContentLength = _info.ContentLength.ToString();
            _blobdetails.LastModified = _info.Details.LastModified.ToString();
            _blobdetails.id = Guid.NewGuid().ToString();

            //_container.CreateItemAsync(_blobdetails, new PartitionKey(_message.VideoName)).GetAwaiter().GetResult();
           // await _container.CreateItemAsync(_blobdetails, new PartitionKey(_message.VideoName));
            Console.WriteLine("Item created");

            // Delete the blob from the unprocessed container
            _source_blob_client.Delete();
            // Add the details of the blob to an Azure Cosmos DB account
        }
    }
}

The local settings are not uploaded to the cloud. to add your connection string you need to do the following. Go to your function app in azure. Select "configuration" under "settings" from the left side menu items. On this screen, you should click on the button "+ New Application Settings". Once the popup opens add "connection" as the name and your connection string as the value. Click on "OK" and then on the next screen click on "save" to save and apply the settings. Hope this helps

For a python project, your connection value in function.json needs to refer to the value in local.settings.json. Should be similar for you:

function.json:

"connection": "AzureWebJobsMyServiceBus"

local.settings.json:

"AzureWebJobsMyServiceBus": "Endpoint=sb://..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM