【问题标题】:Error while getting the config file parameters in C# console application在 C# 控制台应用程序中获取配置文件参数时出错
【发布时间】:2021-12-11 07:56:38
【问题描述】:

我正在关注此页面以使用 C# 使用 azure key vault 加密 blob

https://docs.microsoft.com/en-us/azure/storage/blobs/storage-encrypt-decrypt-blobs-key-vault?WT.mc_id=Portal-Microsoft_Azure_Support&tabs=dotnet11#use-key-vault-secrets

但在此代码块中出现错误

StorageCredentials creds = new StorageCredentials(
                CloudConfigurationManager.GetSetting("accountName"),
                CloudConfigurationManager.GetSetting("accountKey")
            );

未处理的异常。 System.Runtime.InteropServices.MarshalDirectiveException:无法封送“参数 #1”:托管/非托管类型组合无效(不支持与 COM 接口指针之间的封送处理)。

app.config

<appSettings>
    <add key="accountName" value="myaccount"/>
    <add key="accountKey" value="theaccountkey"/>
    <add key="clientId" value="theclientid"/>
    <add key="clientSecret" value="theclientsecret"/>
    <add key="container" value="stuff"/>
</appSettings>

我尝试了下面链接中给出的解决方案,但这些都不起作用

System.argumentnullexception is null using microsoft azure storage

【问题讨论】:

标签: c# azure-blob-storage config azure-keyvault


【解决方案1】:

我试图在我的系统中重现该场景。没有遇到您遇到的错误。我直接传递了凭据,而不是从app.config 读取它们。我认为从app.config 读取凭据存在问题通过凭据直接

输出

【讨论】:

  • StorageCredentials creds = new StorageCredentials(("name"),("key"));我尝试通过,但也没有用,它在同一阶段显示相同的错误
  • 我在运行项目时收到此警告消息warning NU1701: Package 'Microsoft.Azure.ConfigurationManager 4.0.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net5.0'. This package may not be fully compatible with your project.
  • 您使用的是哪个版本的 .net 框架?
  • 在本示例中使用 .NET 5.0
  • Microsoft.Azure.ConfigurationManager 检查,在你使用这个包的应用程序中,我想你用它来读取来自app.config的凭据。请删除那个包,然后再试一次,让我知道面临问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多