c# - 从 OSX (Mac) 上的控制台应用程序访问 Azure 存储帐户

标签 c# macos azure .net-core azure-storage

我正在使用适用于 Mac 的 VS 2017 社区版编写代码,并尝试访问 Azure 存储帐户上的 Blob。但是,我遇到了异常,如下面的代码所示。

WindowsAzure.Storage版本是9.1.1

using System;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;

namespace storageaccount
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnection"));

            //var blobClient = storageAccount.CreateCloudBlobClient();

            //var container = blobClient.GetContainerReference("images");

               //container.CreateIfNotExists(Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Blob);

        //Console.ReadKey();
        }
    }
}

上述代码抛出异常:

System.DllNotFoundException: fusion.dll
  at at (wrapper managed-to-native) Microsoft.Azure.NativeMethods.CreateAssemblyCache(Microsoft.Azure.NativeMethods/IAssemblyCache&,int)
  at Microsoft.Azure.NativeMethods.GetAssemblyPath (System.String name) [0x00037] in <bcbac5dcfbdd47eabe8212983b7d996d>:0
  at Microsoft.Azure.AzureApplicationSettings.GetServiceRuntimeAssembly () [0x0000d] in <bcbac5dcfbdd47eabe8212983b7d996d>:0
  at Microsoft.Azure.AzureApplicationSettings..ctor () [0x0001a] in <bcbac5dcfbdd47eabe8212983b7d996d>:0
  at Microsoft.Azure.CloudConfigurationManager.get_AppSettings () [0x0001e] in <bcbac5dcfbdd47eabe8212983b7d996d>:0
  at Microsoft.Azure.CloudConfigurationManager.GetSetting (System.String name, System.Boolean outputResultsToTrace) [0x00039] in <bcbac5dcfbdd47eabe8212983b7d996d>:0
  at Microsoft.Azure.CloudConfigurationManager.GetSetting (System.String name) [0x00000] in <bcbac5dcfbdd47eabe8212983b7d996d>:0
  at storageaccount.MainClass.Main (System.String[] args) [0x00001] in /Users/Projects/storageaccount/storageaccount/Program.cs:11
<小时/>

代码有什么问题吗?我正在使用 Nuget 包访问云存储帐户。从抛出的异常来看,尚不清楚 OSx 是否支持这一点。

感谢任何帮助。

附加信息: 我在创建新项目时看到 VS 2017(适用于 Mac)中有两个项目选项。 1..Net核心控制台应用程序 2. .Net 控制台应用程序(在 .Net 选项下)- 它针对什么运行?

谢谢

最佳答案

正如您提到的,Microsoft.WindowsAzure.ConfigurationManager 是.net 框架包,它与.net core 平台不兼容

I am still looking for information on how to read app.config for .Net application on Mac

您可以使用System.Configuration.ConfigurationManager来做到这一点。

 var storeageString = ConfigurationManager.AppSettings["storagestring"];

应用程序配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="storagestring" value ="Test string"/>
  </appSettings>
</configuration>

另一种方法是您可以将 appsettings.json 添加到 .NET Core 控制台应用程序。

All that’s required is to add the following NuGet packages and an appsettings.json file.

  • Microsoft.Extensions.Configuration

  • Microsoft.Extensions.Configuration.FileExtensions

  • Microsoft.Extensions.Configuration.Json

演示代码

var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json");
IConfigurationRoot configuration = builder.Build();
var connectionString = configuration["StorageConnection"];
Console.WriteLine(connectionString);

appsettings.json 文件:

{
    "StorageConnection": "Test Connection string"
}

enter image description here

关于c# - 从 OSX (Mac) 上的控制台应用程序访问 Azure 存储帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49842197/

相关文章:

c# - C# 中是否有泛型数字类型?

c# - 使用C#获取当前位置

macos - 如何在 mac os x 10.8.2 上重置 postgresql 9.2 默认用户(通常为 'postgres' )密码?

cocoa - 如何以最佳方式将 NSString 放入矩形中?

azure - 转换 Web 角色中的外部配置

azure - 使用 Entity Framework 将 Web api 部署到 Azure,工作了 1 小时,现在出现错误 500

c# - 在 WPF 中显示 Drawing.Image

c# - Xsd.exe 生成 <xs :list> 的错误实现

macos - 如何运行 fswatch 来调用带有静态参数的程序?

asp.net - 注册后在数据库中使用 Azure B2C 创建用户