Azure 函数:值不能为空。参数名称:来源

Azure function: Value cannot be null. Parameter name: source

最近,当我调用 Azure 函数时,每当我尝试向 AddGroup 函数发出 HTTP 请求时,都会看到此异常。

有什么想法吗?

Time 11:04:02 AM
Exception type Microsoft.Azure.WebJobs.Host.FunctionInvocationException
Exception message Exception while executing function: AddGroup <--- Value cannot be null. Parameter name: source
Host.Results
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: AddGroup ---> System.ArgumentNullException: Value cannot be null.

Parameter name: source

   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.FunctionInvocationFilterInvoker.<InvokeAsync>d__9.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<InvokeAsync>d__24.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithWatchersAsync>d__23.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggingAsync>d__22.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)

   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggingAsync>d__16.MoveNext()

   --- End of inner exception stack trace ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggingAsync>d__16.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<TryExecuteAsync>d__13.MoveNext()

我想通了 - 我遇到了 NuGet 包冲突。我的一个项目试图引用版本 Newtonsoft Json 9.0.1(Azure Functions SDK 所依赖的版本)以及另一个项目想要的版本 11.0.2。我最终让另一个项目也使用了 9.0.1 版本的包。

我遇到了同样的问题,对我来说,错误的文本实际上有些相关。我们从 Azure 得到的错误的奇怪之处在于,函数应用程序部署在一个订阅上运行良好,但在另一个订阅下给了我们这个“值不能为空”错误。我无法说明订阅之间的区别,但我可以解释我们是如何解决这个问题的。

我们的 Azure Function App 执行了一个 async Task 方法。在这个方法中,我们调用一个 returns 类型为 IEnumerable<Thing>Task 的可等待方法,其中 Thing 是一个简单的数据模型 class。被调用的代码看起来像这样:

var things = await SomeFactory.LoadXmlRequest<Thing>(xml, auth);

此后我们需要立即检查 things 是否包含任何数据,因此我们进行了如下检查:

if (things.Any()) { ... }

我团队中一位勇敢的开发人员建议我们先检查 things 是否为空,然后再检查它是否包含任何数据。我们将调用修改为如下所示:

if (things != null && things.Any()) { ... }

这成功了。我们所要做的就是在调用 .Any() LINQ 扩展方法之前检查这个对象是否为 null。