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

本文关键字:对象 实例 设置 连接 字符串 对象引用 Azure | 更新日期: 2023-09-27 18:33:11

我正在尝试在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++;
                }
            }
        }
    }
}

例外情况是:

System.NullReferenceException was unhandle HResult=-2147467261
消息=对象引用未设置为对象的实例。
来源=CPGetAdverts StackTrace: at CPGetAdverts.Program.Main(String[] args( in c:''Users''Diarmaid''Documents''Visual Studio 2015''项目''CPGetAdverts''CPGetAdverts''Program.cs:第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(( 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态( 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:

请问有人有什么想法吗?

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

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

有关详细信息,请参阅此问题。