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

Ram*_*tla 4 c# azure azure-servicebus-queues azure-functions

创建了一个 Azure Function,它是在 Visual Studio 中触发的服务总线,并从 Visual Studio 发布到 Azure。

每当消息进入队列时,手动运行时该函数都会在本地正常运行。但期望的是当消息在队列中时该函数应该自动触发。

我只是手动添加一条新消息,并查看该函数是否自动触发的日志,但事实并非如此。当我检查 Application Insight 时,我发现以下错误日志

函数“ProcessVideos”的侦听器无法启动。服务总线帐户连接字符串“连接”不存在。确保它是已定义的应用程序设置。*”

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=;"
  }
}
Run Code Online (Sandbox Code Playgroud)

实际功能的代码。

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tik*_*uly 5

本地设置不会上传到云端。要添加连接字符串,您需要执行以下操作。转到 Azure 中的函数应用。从左侧菜单项中选择“设置”下的“配置”。在此屏幕上,您应该单击“+ 新应用程序设置”按钮。弹出窗口打开后,添加“connection”作为名称,添加连接字符串作为值。单击“确定”,然后在下一个屏幕上单击“保存”以保存并应用设置。希望这可以帮助