微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Azure Function App 中使用 System.Runtime.Caching

如何解决如何在 Azure Function App 中使用 System.Runtime.Caching

我想在函数调用之间在 Azure 函数应用程序的无服务器函数中使用简单的内存缓存某些值。我正在使用 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 文件上传.

https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#using-nuget-packages

我制作了一个 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

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。