我想在 Azure Function App 的无服务器函数中的函数调用之间使用一些值的简单内存缓存。我正在使用 C# 脚本文件直接在 Azure 门户中进行开发。继suggestion 2) from this blog post by Mark Heath我的函数的 csx 文件中有以下几行:
#r "System.Runtime.Caching"
using System.Runtime.Caching;
using System;
static MemoryCache memoryCache = MemoryCache.Default;
//.....
这应该是System.Runtime.Caching assembly from this doc .
但是在编译时(保存在 Azure 门户中),我得到:
2021-01-05T07:25:39.687 [Information] Script for function 'Intake' changed. Reloading.
2021-01-05T07:25:39.807 [Error] run.csx(1,1): error CS0006: Metadata file 'System.Runtime.Caching' could not be found
2021-01-05T07:25:39.865 [Error] run.csx(2,22): error CS0234: The type or namespace name 'Caching' does not exist in the namespace 'System.Runtime' (are you missing an assembly reference?)
2021-01-05T07:25:39.938 [Error] run.csx(4,8): error CS0246: The type or namespace name 'MemoryCache' could not be found (are you missing a using directive or an assembly reference?)
2021-01-05T07:25:40.008 [Error] run.csx(4,34): error CS0103: The name 'MemoryCache' does not exist in the current context
2021-01-05T07:25:40.041 [Information] Compilation failed.
这是我的host.json,供引用:
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
我需要在此处添加一些内容吗?我本以为,添加对程序集的 #r
引用就足够了。
最佳答案
检查文档后发现,除了文档中提到的众所周知的程序集可以通过#r
引用之外,其他nuget包可能需要使用来上传。我怀疑 proj
文件。
我制作了一个 function.proj
文件,其内容如下
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Runtime.Caching" Version="5.0.0" />
</ItemGroup>
然后我使用 App Service Editor in the portal 上传了 function.proj
完成后,我就能够成功编译 csx
关于azure - 如何在 Azure Function App 中使用 System.Runtime.Caching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65574871/