简体   繁体   English

当新消息进入服务总线队列时,Azure 函数(服务总线触发器)不启动

[英]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.创建了一个 Azure Function,它是在 Visual Studio 中触发并从 Visual Studio 发布到 Azure 的服务总线。

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当我检查 Application Insight 时,我发现了以下错误日志

The listener for function 'ProcessVideos' was unable to start.函数“ProcessVideos”的侦听器无法启动。 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. local.settings.json的代码,其中设置了服务总线连接字符串。

{
    "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.在 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.一旦弹出窗口打开,添加“connection”作为名称和您的连接字符串作为值。 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.对于 python 项目,您在 function.json 中的连接值需要引用 local.settings.json 中的值。 Should be similar for you:对你来说应该是相似的:

function.json:功能.json:

"connection": "AzureWebJobsMyServiceBus"

local.settings.json:本地设置.json:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 几天后消息进入服务总线队列时,不会触发 Azure 函数 - Azure function is not get trigger when message comes into service bus queue after some days Azure Service Bus和Azure功能队列触发器 - Azure Service Bus and Azure Function Queue Trigger 如果 Azure 服务总线队列触发器功能失败,如何将队列消息返回到 Azure 服务总线队列 - How to return queue message to Azure Service Bus Queue if Azure Service Bus Queue Trigger Func Fails Azure Web作业,Azure服务总线队列触发器可防止消息被删除 - Azure Web Jobs, Azure Service Bus Queue Trigger prevent message from getting deleted How to trigger both Azure Function by Service Bus message and output message to Service Bus within single Azure Function - How to trigger both Azure Function by Service Bus message and output message to Service Bus within single Azure Function Azure 服务总线队列触发器 Function MessageReceiver 错误,操作被取消 - Azure Service Bus Queue Trigger Function MessageReceiver error, The operation was canceled Azure - 从服务总线队列触发通知 - Azure - trigger notification from service bus queue Azure服务总线或队列 - Azure Service Bus or Queue Azure服务总线队列 - Azure Service Bus Queue 通过MessageId在Azure Service Bus死信队列上获取消息 - Getting a Message by MessageId on the Azure Service Bus Dead Letter queue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM