使用 Azure Functions Core Tools 在本地运行 Azure 函数时出现问题

Posted

技术标签:

【中文标题】使用 Azure Functions Core Tools 在本地运行 Azure 函数时出现问题【英文标题】:Problem when running Azure function locally using Azure Functions Core Tools 【发布时间】:2022-01-09 22:06:17 【问题描述】:

我正在开发一个持久的 Azure 功能。我正在使用 .NET Core 3.1。它是一个 HTTP 触发的函数。当我从 Visual Studio 调试它然后通过调用端点来触发它时,一切正常。

在同一台机器上,我安装了 Azure Functions Core Tools (4.0.3971)。我运行存储模拟器:

C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe start

然后转到我的函数所在的目录并输入:

func start

一切都和以前一样,所以我通过向端点发出请求来触发我的函数。它开始工作,但随后抛出以下错误:

[2021-12-03T14:25:51.081Z] Executed 'MyFunction_Process' (Failed, Id=f709e4c0-f74e-4303-a68b-6d5d55f0f7e5, Duration=2182ms)
[2021-12-03T14:25:51.086Z] System.Private.CoreLib: Exception while executing function: MyFunctionIntegration_Process. Microsoft.EntityFrameworkCore: The type initializer for 'Microsoft.EntityFrameworkCore.EnumerableMethods' threw an exception. System.Linq: Sequence contains more than one matching element.
[2021-12-03T14:25:51.101Z] myfunctionsingleinstance: Function 'MyFunction_Process (Activity)' failed with an error. Reason: System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.EnumerableMethods' threw an exception.
[2021-12-03T14:25:51.106Z]  ---> System.InvalidOperationException: Sequence contains more than one matching element
[2021-12-03T14:25:51.110Z]    at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException()
[2021-12-03T14:25:51.112Z]    at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
[2021-12-03T14:25:51.113Z]    at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
[2021-12-03T14:25:51.115Z]    at Microsoft.EntityFrameworkCore.EnumerableMethods..cctor()
[2021-12-03T14:25:51.116Z]    --- End of inner exception stack trace ---
[2021-12-03T14:25:51.117Z]    at Microsoft.EntityFrameworkCore.EnumerableMethods.get_AnyWithPredicate()
[2021-12-03T14:25:51.119Z]    at Microsoft.EntityFrameworkCore.Query.Internal.AllAnyToContainsRewritingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
[2021-12-03T14:25:51.121Z]    at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
[2021-12-03T14:25:51.124Z]    at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
[2021-12-03T14:25:51.126Z]    at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
[2021-12-03T14:25:51.128Z]    at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
[2021-12-03T14:25:51.129Z]    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
[2021-12-03T14:25:51.131Z]    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
[2021-12-03T14:25:51.133Z]    at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
[2021-12-03T14:25:51.134Z]    at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
[2021-12-03T14:25:51.136Z]    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
[2021-12-03T14:25:51.140Z]    at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
[2021-12-03T14:25:51.141Z]    at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
[2021-12-03T14:25:51.143Z]    at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.IncludableQueryable`2.GetEnumerator()
[2021-12-03T14:25:51.144Z]    at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer)
[2021-12-03T14:25:51.146Z]    at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector)
[2021-12-03T14:25:51.148Z]    at Logistics.Infrastructure.MyFunctionIntegration.Integration.GetDbRows(ILogger log) in C:\Users\Michal\source\repos\Logistics\src\backend\src\infrastructure\Logistics.Infrastructure.MyFunctionIntegration\Integration.cs:line 103
...

第 103 行是:

var dbEntities = _dbContext.Carriers.IgnoreQueryFilters().Include(c => c.CarrierGitd.Address).ToDictionary(c => c.CarrierGitd?.LicenseNumber);

所以当我尝试从数据库中获取数据时会发生这种情况。但它工作正常,当我在 VS 中调试时。数据库是一样的。问题在哪里?

【问题讨论】:

【参考方案1】:

当我在 SecondLib 依赖项中将 Microsoft.EntityFrameworkCore 升级到 5.0.0 时,问题就消失了。

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="5.0.0" />
  </ItemGroup>

TargetFramework 没有被触动

<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>

注意:

我在 Azure Functions 核心工具版本 3 中遇到了同样的问题。如果您的实体框架核心版本在第二个 lib 依赖项中是 5.x,请升级并尝试一次。或者如果可能,将 Azure Functions Core 工具版本降级到 3 并检查。

【讨论】:

我降级到 Azure Functions Core Tools 3 并开始工作。谢谢! 你好@MichalB。很高兴您的问题得到解决!【参考方案2】:

我需要同时支持 v3 和 v4 函数项目,所以我只复制了 V3 工具目录 C:\Program Files\Microsoft\Azure Functions Core Tools(然后将其命名为“Azure Functions Core Tools v3”),安装了 V4 并创建了一个批处理文件在旧 v3 项目的目录中运行正确的“func.exe”。

【讨论】:

以上是关于使用 Azure Functions Core Tools 在本地运行 Azure 函数时出现问题的主要内容,如果未能解决你的问题,请参考以下文章