【问题标题】:Unable to user Memory Cache with Azure Function App无法使用 Azure Function App 使用内存缓存
【发布时间】:2021-06-16 21:00:58
【问题描述】:

我已经创建了带有 Http 触发器的 Azure Function App,因为我想使用来自 system.runtime 命名空间的内存缓存来缓存令牌。但是当我运行应用程序时,它会抛出错误" System.Runtime.Caching: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified."

我已确保在项目中安装了确切的版本。这是csproj文件。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
    <PackageReference Include="System.Runtime.Caching" Version="5.0.0" />
  </ItemGroup>
  <ItemGroup>

这是我遇到问题的代码的最低版本。

public static class Function1
    {
        public static MemoryCache accessTokenCache = new MemoryCache("accessToken");
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            accessTokenCache.Add("123", "123", new CacheItemPolicy() { Priority = CacheItemPriority.Default });

            return new OkObjectResult("");
        }
    }

【问题讨论】:

  • 以上代码在 4.7.0 版本的 system.runtime.caching nuget 上运行良好。我现在已经降级以使其正常工作。
  • 您好,既然您已经找到了解决方法,您能否在下面的区域发布答案以供其他社区参考?

标签: azure asp.net-core .net-core azure-functions memorycache


【解决方案1】:

上面的代码适用于 system.runtime.caching nuget 版本 4.7.0。我现在已经降级以使其正常工作。

我还在 github 上发布了这个问题,以便进一步跟进。 https://github.com/Azure/Azure-Functions/issues/1867

【讨论】:

    猜你喜欢
    • 2019-10-15
    • 2022-10-13
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多