c# - Azure 连接字符串 - 对象引用未设置为对象的实例

标签 c# azure console-application azure-blob-storage

我正在尝试在 Windows 控制台应用程序中下载 azure blob。当我构建和调试应用程序时,我的 azure 连接字符串抛出异常。该字符串在我的其他 ASP.NET 应用程序中工作正常。

有问题的行是:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["DefaultEndpointsProtocol=https;AccountName=xxxxx;AccountKey=xxxxx==;BlobEndpoint=https://xxxxx.blob.core.windows.net/;TableEndpoint=https://xxxxx.table.core.windows.net/;QueueEndpoint=https://xxxxx.queue.core.windows.net/;FileEndpoint=https://xxxxx.file.core.windows.net/"].ConnectionString);

我的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Threading;

namespace CPGetAdverts
{
    class Program
    {
        static void Main(string[] args)
        {
            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["DefaultEndpointsProtocol=https;AccountName=xxxxx;AccountKey=xxxxx==;BlobEndpoint=https://xxxxx.blob.core.windows.net/;TableEndpoint=https://xxxxx.table.core.windows.net/;QueueEndpoint=https://xxxxx.queue.core.windows.net/;FileEndpoint=https://xxxxx.file.core.windows.net/"].ConnectionString);
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            var container = blobClient.GetContainerReference("newadverts").ListBlobs();
            // Retrieve filenames from container List
            var urls = new List<string>();
            int fileName = 1;

            foreach (var blob in container)
            {
                using (var fileStream = System.IO.File.OpenWrite(@"\home\pi\Pictures\" + fileName + ".jpg"))
                {
                    var blobReference = blobClient.GetBlobReferenceFromServer(blob.Uri);
                    blobReference.DownloadToStream(fileStream);
                    fileName++;
                }
            }

        }
    }
}

异常(exception)情况是:

System.NullReferenceException was unhandled HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=CPGetAdverts StackTrace: at CPGetAdverts.Program.Main(String[] args) in c:\Users\Diarmaid\Documents\Visual Studio 2015\Projects\CPGetAdverts\CPGetAdverts\Program.cs:line 24 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

请问大家有什么想法吗?

最佳答案

我希望 ConfigurationManager.ConnectionStrings[""] 引用连接字符串的名称,而不是连接字符串本身。如果对连接字符串进行硬编码,则不需要调用检索连接字符串。

参见this question了解详情。

关于c# - Azure 连接字符串 - 对象引用未设置为对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35188670/

相关文章:

c# - Azure 函数触发两次 - VS 2017 预览版

c - 从控制台应用程序显示系统资源使用情况

c# - StyleCop/FxCop 10 - 如何仅在命名空间级别正确抑制消息?

azure - 使用部署槽进行交换时处理数据库迁移的推荐方法

azure - 表存储获取实体的列

c# - 将硬盘路径转换为应用程序文件夹路径

java - 如何在Java控制台应用程序中制作进度条?

c# - 如何通过 $.ajax() 发布到 ASP.NET WEB API?

c# - 如何创建与 Microsoft.Azure.Management.Compute 一起使用的 ServiceClientCredential

c# - EF - 将泛型类映射到复杂类型