【问题标题】:connection to Azure table storage fails连接到 Azure 表存储失败
【发布时间】:2018-11-11 15:53:28
【问题描述】:

我正在开发一个新的 Web 应用程序,并尝试连接到现有的 Azure 表存储。

using System.Web.Mvc;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;

namespace WebApplication3.Controllers
{
public class GetEmailAddressesController : Controller
{
   public ActionResult Address()
    {
        string emails = "";

        // Parse the connection string and return a reference to the storage account.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("________"));

        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        CloudTable table = tableClient.GetTableReference("experimentsEmailAddresses");

        // Construct a table query.
        TableQuery<TableData> query = new TableQuery<TableData>();

        foreach (TableData entity in table.ExecuteQuery(query))
        {
            emails += entity.Email + ";";

        }

        ViewBag.Message = " " + emails;
        return View();
    }

}
} 

代码会编译,但是当我在调试模式下运行它时出现错误:

System.ArgumentNullException: '值不能为空。参数名称: 连接字符串'

即我的连接字符串无效,尽管我从 Azure 中的访问密钥复制了它。

什么是最好的解决方案?

【问题讨论】:

  • 您将连接字符串存储在配置文件中的什么位置?
  • 在 web.config 下我添加了
  • 具体是在appSettings还是connectionStrings下?
  • 它在appSettings下
  • 我的另一个建议是: 1. 逐步检查并确保 CloudConfigurationManager.GetSetting("________"));正在返回您认为的内容。 2. 获取应用返回的连接字符串并尝试通过 Azure 存储资源管理器进行连接。

标签: .net azure web-applications azure-table-storage


【解决方案1】:

我变了

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

CloudStorageAccount storageAccount = cloudStorageAccount.Parse("StorageConnectionString");

而且它似乎工作得很好。

【讨论】:

    【解决方案2】:

    我认为你应该改变:

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("________"));
    

    收件人:

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("StorageConnectionString"));
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-25
      • 2018-11-24
      • 2019-10-18
      • 2020-01-18
      • 1970-01-01
      • 2017-12-30
      • 2022-08-03
      • 2020-10-19
      相关资源
      最近更新 更多