Object Reference not set to an Instance of an Object
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.
The offending line is:
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);
My code is:
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(@"homepiPictures" + fileName + ".jpg"))
{
var blobReference = blobClient.GetBlobReferenceFromServer(blob.Uri);
blobReference.DownloadToStream(fileStream);
fileName++;
}
}
}
}
}
The exception is:
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:UsersDiarmaidDocumentsVisual Studio 2015ProjectsCPGetAdvertsCPGetAdvertsProgram.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?
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.
链接地址: http://www.djcxy.com/p/95944.html上一篇: 亚音速3 + SQLite 3
下一篇: 你调用的对象是空的