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

标签 azure azure-functions azure-servicebus-queues

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

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

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

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.*"

设置服务总线连接字符串的 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=;"
  }
}

实际功能的代码。

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
        }
    }
}

最佳答案

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

关于当新消息进入服务总线队列时,Azure 函数(服务总线触发器)无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71668612/

相关文章:

azure - 使用 Java Spring 应用程序连接到 Azure 服务总线 - 超时

c# - 如何在托管于共享服务器上的 ASP.Net Core 3.1 应用程序中存储诸如连接字符串之类的生产 secret

azure - 工作人员角色中的ffmpeg不会关闭窗口

asp.net-mvc - 如何使用 Azure 网站通过私有(private) IP 连接到 Sql Server VM

具有复杂 C# 代码的 Azure 函数

asp.net-mvc - 根据内容重排多个队列

javascript - MSAL 2.*.js 是否适用于 IE 11?

azure-active-directory - 是否可以将系统托管标识分配给需要用户分配的 Azure AD 企业应用程序?

azure - 如何手动添加预编译函数的function.json文件VS 2017(15.3预览版)

c# - 在 azure 函数中获取队列名称或主题+订阅者名称(Azure 服务总线触发函数)