Azure连接字符串-对象引用未设置为对象的实例 [英] Azure Connection String - Object Reference not set to an Instance of an Object

查看:91
本文介绍了Azure连接字符串-对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to download azure blobs in a Windows Console Application. When I build and debug the application my azure connection string is throwing an exception. This string works fine in my other asp.net app.

违规行是:

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 HResult = -2147467261
Message =对象引用未设置为对象的实例.
Source = CPGetAdverts StackTrace: 在c:\ Users \ Diarmaid \ Documents \ Visual Studio中的CPGetAdverts.Program.Main(String [] args)中 2015 \ Projects \ CPGetAdverts \ CPGetAdverts \ Program.cs:第24行 在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String []参数) 在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)中 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 reserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executeContext,ContextCallback回调,对象状态,布尔值 reserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态) 在System.Threading.ThreadHelper.ThreadStart()处InnerException:

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:

请问有人有什么想法吗?

Does anyone have any ideas please?

推荐答案

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

I would expect ConfigurationManager.ConnectionStrings[""] to reference the name of the connection string, not the connection string itself. The call to retrieve the connection string is unnecessary if are hard-coding it.

有关详细信息,请参见此问题.

See this question for details.

这篇关于Azure连接字符串-对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆