【问题标题】:Azure Function V2 Value cannot be nullAzure 函数 V2 值不能为空
【发布时间】:2019-11-22 20:08:14
【问题描述】:

尝试让我的 Azure Function V2 在 docker 容器中运行时遇到问题。该项目从 Azure 服务总线主题读取。我得到的错误不是描述性的,我不确定什么值实际上是空的。当我在本地运行该项目时,我没有任何问题,但是当我为它创建一个容器时,我得到了这个错误。

它是我没有传入的环境变量,还是没有从 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

更新:

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

【问题讨论】:

  • 问题发生在本地?在 Azure 上?两者都有?

标签: azure function core servicebus


【解决方案1】:

从您帖子中的调用堆栈来看,我认为错误是针对未为服务总线触发器设置的连接字符串。

你是否用连接字符串适当地设置了环境变量?

来自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 等于连接字符串。

【讨论】:

  • 是的,我确实使用适当的环境变量设置了连接字符串。用我的函数更新了原始帖子。
  • github.com/Azure/azure-sdk-for-net/blob/… 可能是引发异常的原因。我想我以前在不小心只处理连接字符串的一部分时看到过这种情况。确保连接字符串的格式可能是值得的——Endpoint=sb://<endpoint>;SharedAccessKeyName=<key-name>;SharedAccessKey=<key>
  • 所以我使用的连接字符串是这种格式:DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net。在本地这工作正常,我需要使用不同的连接字符串吗?
  • 你是如何得到那个连接字符串的?这看起来像你的存储。这就是您希望服务总线的方式(使用共享访问策略)——docs.microsoft.com/en-us/azure/service-bus-messaging/…
  • 是的,你是对的。最终意外切换了连接字符串。非常感谢您的帮助!
【解决方案2】:

来自 Microsoft 文档:

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

ArgumentException、ArgumentNullException、ArgumentOutOfRangeException

提供给该方法的一个或多个参数无效。

提供给的 URI NamespaceManagerCreate 包含路径段。

提供给NamespaceManagerCreate 的URI 方案无效。

属性值大于 32 KB。检查调用代码和 确保参数正确。

换句话说,在这两种方法中的任何一种/两种方法中单步执行您的代码(如果可能)并确保存在有效的“uriString”。

【讨论】:

【解决方案3】:

我遇到了与 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”文件来设置它的默认值:

"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 的人来说非常重要。

【讨论】:

    猜你喜欢
    • 2019-06-12
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多