当前位置: 首页 > 知识库问答 >
问题:

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

郤浩慨
2023-03-14

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

        }
    }
}

共有1个答案

麹权
2023-03-14

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

详情见此问题。

 类似资料:
  • 我需要将一些html转换为PDF。我遇到了IText7,它看起来是一个很好的解决方案。 我正在. net core 3.1中开发 但是,即使使用它的基本实现,我也会遇到一个错误: 每个人都喜欢这个错误: 对象引用未设置为对象的实例。 内部异常为null,唯一的线索是SerializationStackTraceString是: 在iText. IO. FontCache. cctor() 那么,它

  • 是否有建议使用具有段落的单元格构建表,以避免在向表或文档添加单元格时出现异常?我明白了,但我不知道会发生什么: 以下是使用Windows控制台项目的简单快照(与实际项目相比): 注意,注释的代码行: 添加它可以作为一种变通方法,使文档能够毫无例外地呈现。

  • 在我的ASP.NET程序中。我设置了一个受保护的列表。我在列表中添加了一个值。但它显示对象引用未设置为对象错误的实例 如何解决这个错误?

  • 创建新的PDF文档时会发生以下情况。在第三页上创建第一个文本字段时出错。前两页上创建了多个字段,没有问题。 相关代码: 插入pdfPage。冲洗();在“PdfPage PdfPage=pdfDoc.AddNewPage(pageSize);”之后语句在将第一个文本框添加到第一页时会导致相同的错误。 如何更正错误?

  • 我有以下C#类: 总而言之,区域设置有区域、按钮和字段。区域有中心和东部。中心和东部拥有产权。Fields包含具有属性firstName、lastName和ChooseLocale的标签。 在一个名为GetLocale的方法中,我有以下代码: 运行代码时,在以下行抛出一个“NullReferenceException was unhandled by user code”: 我设置属性title、

  • 为什么下面的和对象不同,当尝试在字符串池中创建时,它检查内容已经有相同的内容,所以引用字符串池中的对象。但我的假设是错误的,那么任何人都会纠正我。