Azure Function App 无法加载 System.IO.Pipelines 连接到 Redis

Azure Function App Could not load System.IO.Pipelines connecting to Redis

在 VS2019 (16.8.3) 中创建了一个新的 Function App,HTTP 触发器以连接到 Azure Cache for Redis。从 nuget 添加了 StackExchange.Redis 2.2.4。

local.settings.json 包含 RedisConnectionFromKeyVault 的 key/value 和来自门户访问密钥的主连接字符串。

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "RedisConnectionFromKeyVault": "<<SNIP>>.redis.cache.windows.net:6380,password=<<SNIP>>,ssl=True,abortConnect=False"
  }
}

在默认函数代码中添加了以下行:

var connectionRedis = Environment.GetEnvironmentVariable("RedisConnectionFromKeyVault", EnvironmentVariableTarget.Process);
var cache = ConnectionMultiplexer.Connect(connectionRedis).GetDatabase();

当我 运行 并在本地触发函数应用程序时,我在 ConnectionMultiplexer.Connect 调用中收到以下异常。

System.Private.CoreLib: Exception while executing function: Function1. StackExchange.Redis: Could not load file or assembly 'System.IO.Pipelines, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
   at StackExchange.Redis.ConnectionMultiplexer.Connect(ConfigurationOptions configuration, TextWriter log) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 1032
   at StackExchange.Redis.ConnectionMultiplexer.Connect(String configuration, TextWriter log) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 1015
   at FunctionApp1.Function1.<Run>d__0.MoveNext() in E:\GitRepos\FunctionApp1\FunctionApp1\Function1.cs:line 26

在控制台应用程序中尝试了类似的代码并且它工作正常?

我错过了什么?为什么函数应用程序认为找不到 System.IO.Pipelines 程序集?

即使我明确包含 System.IO.Piplelines nuget 包它也找不到它?

好的,我找到了解决方法。

我指的是包 StackExchange.Redis 2.2.4(这让 Azure 想要加载 System.IO.Pipelines 5.x)。

我用 Microsoft.Extensions.Caching.StackExchangeRedis 5.0.1 替换了那个包(它又引用 StackExchange.Redis 2.0.593,后者引用 System.IO.Pipeline 4.5.2)。

现在我的功能可以正常工作了。

虽然不确定这是否是一个长期解决方案。

谢谢。

看起来这是 Azure Functions 的一个已知问题,如 https://github.com/Azure/azure-functions-host/issues/5894

所述

StackExchange.Redis 提出了问题 https://github.com/StackExchange/StackExchange.Redis/issues/1637

https://github.com/StackExchange/StackExchange.Redis/issues/1655

可以通过将如下所示的 _FunctionsSkipCleanOutput 元素添加到 csproj 来解决问题

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput> <!-- *** this line is new ** -->
</PropertyGroup>