Azure Function V2 值不能为空

Azure Function V2 Value cannot be null

运行 在 docker 容器中尝试将我的 Azure Function V2 设置为 运行 时出现问题。该项目从 Azure 服务总线主题中读取。我得到的错误不是描述性的,我不确定什么值实际上是 null。当我在本地 运行 项目时没有问题,但是当我为其创建容器时出现此错误。

它是我没有传入的环境变量还是它没有正确读取 appsettings.json?

环境变量

APPINSIGHTS_INSTRUMENTATIONKEY
AzureWebJobsStorage
AzureFunctionsWebHost__hostid
AzureWebJobsServiceBus

这是我的功能

public async void Run([ServiceBusTrigger(TopicName, SubscriptionName, Connection = "AzureWebJobsServiceBus")] Message message, string lockToken, MessageReceiver messageReceiver, ILogger log)

这是我的 Dockerfile

FROM microsoft/dotnet:2.2-sdk AS installer-env

COPY . /src/dotnet-function-app

RUN cd /src/dotnet-function-app && \
    mkdir -p /home/site/wwwroot && \
    dotnet publish Project/Project.csproj --output /home/site/wwwroot

FROM mcr.microsoft.com/azure-functions/dotnet:2.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]
COPY --from=installer-env ["/src/dotnet-function-app/Project/Microsoft.Azure.WebJobs.Script.WebHost.runtimeconfig.json", "/azure-functions-host/Microsoft.Azure.WebJobs.Script.WebHost.runtimeconfig.json"]

ENTRYPOINT ["/azure-functions-host/Microsoft.Azure.WebJobs.Script.WebHost", "--runtimeconfig", "/azure-functions-host/Microsoft.Azure.WebJobs.Script.WebHost.runtimeconfig.json"]

这是错误

fail: Host.Startup[515]
      A host error has occurred during startup operation '6143cd8a-c857-4cfc-b52a-930e0de0d836'.
System.ArgumentNullException: Value cannot be null.
Parameter name: uriString
   at System.Uri..ctor(String uriString)
   at Microsoft.Azure.ServiceBus.ServiceBusConnection.InitializeConnection(ServiceBusConnectionStringBuilder builder)
   at Microsoft.Azure.ServiceBus.Core.MessageReceiver..ctor(String connectionString, String entityPath, ReceiveMode receiveMode, RetryPolicy retryPolicy, Int32 prefetchCount)
   at Microsoft.Azure.WebJobs.ServiceBus.MessagingProvider.GetOrAddMessageReceiver(String entityPath, String connectionString)
   at Microsoft.Azure.WebJobs.ServiceBus.MessagingProvider.CreateMessageProcessor(String entityPath, String connectionString)
   at Microsoft.Azure.WebJobs.ServiceBus.Listeners.ServiceBusListener..ctor(String entityPath, Boolean isSessionsEnabled, ServiceBusTriggerExecutor triggerExecutor, ServiceBusOptions config, ServiceBusAccount serviceBusAccount, MessagingProvider messagingProvider)
   at Microsoft.Azure.WebJobs.ServiceBus.Listeners.ServiceBusListenerFactory.CreateAsync(CancellationToken cancellationToken)
   at Microsoft.Azure.WebJobs.ServiceBus.Triggers.ServiceBusTriggerBinding.CreateListenerAsync(ListenerFactoryContext context)
   at Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexer.ListenerFactory.CreateAsync(CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Indexers\FunctionIndexer.cs:line 426
   at Microsoft.Azure.WebJobs.Host.Listeners.HostListenerFactory.CreateAsync(CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Listeners\HostListenerFactory.cs:line 67
   at Microsoft.Azure.WebJobs.Host.Listeners.ListenerFactoryListener.StartAsyncCore(CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Listeners\ListenerFactoryListener.cs:line 45
   at Microsoft.Azure.WebJobs.Host.Listeners.ShutdownListener.StartAsync(CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Listeners\ShutdownListener.cs:line 29
   at Microsoft.Azure.WebJobs.JobHost.StartAsyncCore(CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\JobHost.cs:line 103
   at Microsoft.Azure.WebJobs.Script.ScriptHost.StartAsyncCore(CancellationToken cancellationToken) in /src/azure-functions-host/src/WebJobs.Script/Host/ScriptHost.cs:line 249
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Azure.WebJobs.Script.WebHost.WebJobsScriptHostService.UnsynchronizedStartHostAsync(ScriptHostStartupOperation activeOperation, Int32 attemptCount, JobHostStartupMode startupMode) in /src/azure-functions-host/src/WebJobs.Script.WebHost/WebJobsScriptHostService.cs:line 237
info: Microsoft.Azure.WebJobs.Hosting.JobHostService[0]
      Stopping JobHost

更新:

最终使用了错误的连接字符串。

来自 Microsoft 文档:

https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-exceptions

ArgumentException, ArgumentNullException, ArgumentOutOfRangeException:

One or more arguments supplied to the method are invalid.

The URI supplied to NamespaceManager orCreate contains path segment(s).

The URI scheme supplied to NamespaceManager or Create is invalid.

The property value is larger than 32 KB. Check the calling code and make sure the arguments are correct.

换句话说,在这些方法的 either/both 中单步执行您的代码(如果可能)并确保存在有效的 "uriString".

从您的 post 中的调用堆栈来看,我认为错误是因为没有为服务总线触发器设置连接字符串。

您是否正确设置了带有连接字符串的环境变量?

来自 the docs,

[FunctionName("ServiceBusQueueTriggerCSharp")]                    
public static void Run(
    [ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] 
    string myQueueItem,
    Int32 deliveryCount,
    DateTime enqueuedTimeUtc,
    string messageId,
    ILogger log)
{
    log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
    log.LogInformation($"EnqueuedTimeUtc={enqueuedTimeUtc}");
    log.LogInformation($"DeliveryCount={deliveryCount}");
    log.LogInformation($"MessageId={messageId}");
}

如果您有上述代码,那么您需要将环境变量 ServiceBusConnection 设置为等于连接字符串。

我得到了与 PO 相同的错误,问题实际上是由于以下情况,假设您有这样的基本实现:

[FunctionName("Function1")]
public static void Run([ServiceBusTrigger("myqueue")]string myQueueItem, ILogger log)
{
    log.LogInformation($"C# ServiceBus queue trigger function processed message: 
     {myQueueItem}");
}

默认情况下,ServiceBusTrigger 属性使用的 ConnectionString 称为“AzureWebJobsServiceBus”,默认情况下它不会在您的“local.settings.json”或“settings.json”文件中设置。

因此,要解决此问题,您必须通过将以下内容添加到“local.settings.json”文件来设置它的默认值 node/value:

"Values": {
    "AzureWebJobsStorage": "your connection string for your storage account",
    "AzureWebJobsServiceBus": "Your connection string for your service bus which you can get through azure portal"
}

如果您想为此 属性“AzureWebJobsServiceBus”使用不同的名称,则必须在“ServiceBusTrigger”参数中指定名称,例如:

[FunctionName("Function1")]
public static void Run([ServiceBusTrigger("myqueue", Connection = "MyServiceBusConnectionStringName")]string myQueueItem, ILogger log)
{
    log.LogInformation($"C# ServiceBus queue trigger function processed message: 
         {myQueueItem}");
}

然后将您的“local.settings.json”更改为:

"Values": {
    "AzureWebJobsStorage": "your connection string for your storage account",
    "MyServiceBusConnectionStringName": "Your connection string for your service bus which you can get through azure portal"
}

这些细微差别对于刚开始使用 Azure Functions 的人来说非常重要。