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

标签 azure serverless azure-function-app csx

我想在 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 文件。

https://learn.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

关于azure - 如何在 Azure Function App 中使用 System.Runtime.Caching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65574871/

相关文章:

visual-studio - Azure 函数在 function.json 的本地中缺少绑定(bind)

java - Service Fabric Java 应用程序部署在本地集群中失败

node.js - 根据当前阶段更改变量

Azure Function App - 如何获取 Invoke Url 和代码

针对已删除的 Azure 容器的 Azure 警报

c# - 如何获取具有子目录级别(n 级)的 Blob 容器中的所有 Blob?

amazon-dynamodb - 无服务器动态流和触发器

devops - 使用无服务器方法进行 BFSI 有多安全

python - 无法访问 python 的 Azure 函数

python - Blob 触发函数无法读取某些 Excel 文件