部署后Azure Function App无法删除Blob镜像问题求助
问题:Azure Function部署后无法删除Blob,报错找不到System.Memory.Data程序集
环境与背景
- .NET版本:.NET 8
- 开发语言:C#
- 功能:开发了一个Azure Function App,用于删除Azure Storage中的Blob镜像,本地运行(指向Azure存储账户)完全正常
- 配置:本地和Azure端已配置相同的环境变量(含存储连接字符串)
问题现象
部署到Azure后,函数无法执行Blob删除操作,报错信息如下:
Exception while executing function: EliminarImagen Could not load file or assembly 'System.Memory.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
已尝试的排查操作
- 部署模式:分别尝试了Framework-dependent和Self-contained
- 目标运行时:分别设置为win-x64和portable
以上调整均未解决问题,错误依旧。
相关配置与代码
.csproj文件内容
<PackageReference Include="Azure.Storage.Blobs" Version="12.22.2" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.5.0" /> <PackageReference Include="System.Memory.Data" Version="8.0.1" />
Blob删除逻辑代码
using Azure.Storage.Blobs; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.Extensions.Logging; using System; using System.Threading.Tasks; namespace FuncionEliminarImagenes.Clases { public static class EliminarImagen { [FunctionName("EliminarImagen")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "eliminaImagen/{containerName}/{imageName}")] HttpRequest req, string containerName, string imageName, ILogger log) { log.LogInformation("Initializing the function, ready to receive requests..."); BlobServiceClient blobServiceClient = new BlobServiceClient(Conexion.connectionString); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); // 检查容器是否存在 if (!await containerClient.ExistsAsync()) { log.LogError($"容器 {containerName} 不存在。"); return new NotFoundObjectResult($"容器 {containerName} 不存在。"); } BlobClient blobClient = containerClient.GetBlobClient(imageName); log.LogInformation($"尝试删除容器 {containerName} 中的镜像:{imageName}"); if (!await blobClient.ExistsAsync()) { log.LogWarning($"容器 {containerName} 中不存在镜像 {imageName}。"); return new NotFoundObjectResult($"容器 {containerName} 中不存在镜像 {imageName}。"); } else { var deleteResponse = await blobClient.DeleteIfExistsAsync(); if (deleteResponse) { log.LogInformation($"镜像 {imageName} 已从容器 {containerName} 中删除。"); return new OkObjectResult($"镜像 {imageName} 删除成功。"); } return new StatusCodeResult(200); } } } }
解决方案
- 添加程序集绑定重定向配置
在项目根目录创建runtimeconfig.template.json文件,添加以下内容,让运行时自动适配版本:
{ "runtimeOptions": { "assemblyBinding": { "dependencies": [ { "name": "System.Memory.Data", "publicKeyToken": "cc7b13ffcd2ddd51", "version": "8.0.1.0", "culture": "neutral", "bindingRedirects": [ { "oldVersion": "0.0.0.0-8.0.1.0", "newVersion": "8.0.1.0" } ] } ] } } }
- 强制复制所有依赖项到输出目录
在.csproj文件的<PropertyGroup>节点中添加以下配置:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
- 升级Microsoft.NET.Sdk.Functions版本
当前使用的4.5.0版本对.NET 8的兼容性不足,升级到最新稳定版本(如4.11.1及以上):
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.11.1" />
- 清理并重新部署
- 本地执行
dotnet clean和dotnet publish命令,清理旧编译输出 - 删除Azure Function App上的旧部署文件,重新上传新发布包
内容的提问来源于stack exchange,提问作者Jorge Torselli
相关产品推荐
相关产品推荐

