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

Problem when running Azure function locally using Azure Functions Core Tools

我正在开发持久的 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 中调试时它工作正常。数据库是一样的。问题在哪里?

当我在 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 中遇到了同样的问题。如果您的 Entity framework 核心版本在第二个 lib 依赖项中小于 5.x,请升级并尝试一次。或者如果可能,将 Azure Functions Core 工具版本降级到 3 并检查。

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