你调用的对象是空的
我正尝试在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(@"homepiPictures" + fileName + ".jpg"))
{
var blobReference = blobClient.GetBlobReferenceFromServer(blob.Uri);
blobReference.DownloadToStream(fileStream);
fileName++;
}
}
}
}
}
例外是:
System.NullReferenceException未处理HResult = -2147467261
消息=对象引用未设置为对象的实例。
Source = CPGetAdverts StackTrace:位于c: Users Diarmaid Documents Visual Studio 2015 Projects CPGetAdverts CPGetAdverts Program.cs中的CPGetAdverts.Program.Main(String [] args):System.AppDomain._nExecuteAssembly (System.ThreadingHelper.ThreadStart_Context(Object state))上Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)上的RuntimeAssembly程序集,String [] args) System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx)上System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx)在System.Threading.ExecutionContext.Run (ExecutionContext executionContext,ContextCallback回调,对象状态)在System.Threading.ThreadHelper.ThreadStart()InnerException:
有没有人有任何想法?
我期望ConfigurationManager.ConnectionStrings [“”]引用连接字符串的名称,而不是连接字符串本身。 如果对它进行硬编码,则检索连接字符串的调用是不必要的。
详情请参阅此问题。
链接地址: http://www.djcxy.com/p/95943.html