You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#控制台应用使用Azure Key Vault加密Blob时获取配置参数报错

错误根因

该System.Runtime.InteropServices.MarshalDirectiveException报错的核心原因是CloudConfigurationManager类的运行时兼容性问题:该类属于旧版Azure SDK专属组件,仅支持.NET Framework运行时,若你的项目基于.NET Core/.NET 5+版本,调用该类会触发COM编组异常,此外也可能是依赖包版本不匹配、配置文件未正确加载导致的异常。

排查处理步骤

  • 第一步:替换配置读取方案
    弃用CloudConfigurationManager,该类已经不再被官方推荐用于配置读取,根据你的项目类型选择对应方案:
  1. .NET Core/.NET 5+ 项目
    安装System.Configuration.ConfigurationManager NuGet包,修改代码如下:
using System.Configuration;

// 读取配置
string accountName = ConfigurationManager.AppSettings["accountName"];
string accountKey = ConfigurationManager.AppSettings["accountKey"];

// 增加空值校验
if (string.IsNullOrWhiteSpace(accountName) || string.IsNullOrWhiteSpace(accountKey))
{
    throw new InvalidOperationException("存储账户名或密钥配置缺失");
}

StorageCredentials creds = new StorageCredentials(accountName, accountKey);
  1. .NET Framework 项目
    直接移除Microsoft.WindowsAzure.ConfigurationManager包引用,在项目添加引用→添加程序集引用System.Configuration,同样使用上述ConfigurationManager`代码即可。
  • 第二步:校验配置文件属性
    右键点击项目中的app.config文件,在属性面板中将复制到输出目录设置为如果较新则复制,确保运行时可以正常读取配置内容。
  • 第三步:验证依赖包版本一致性
    确认你使用的Azure.Storage.Blobs、Azure.Security.KeyVault.Keys等相关依赖包与你参照的官方文档要求的版本匹配,避免大版本差异带来的API不兼容问题。
    如果条件允许,更推荐使用Azure AD身份认证访问存储账户,无需硬编码存储账户密钥,安全性更高,示例代码如下:
using Azure.Identity;
using Azure.Storage.Blobs;

var blobServiceClient = new BlobServiceClient(
    new Uri("https://<你的存储账户名>.blob.core.windows.net"),
    new DefaultAzureCredential());

内容的提问来源于stack exchange,提问作者Atul Takekar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 15:54:04