【问题标题】:Unable to connect to Azure Table Storage in windows 2012 r2 and .net core 1.0.1在 windows 2012 r2 和 .net core 1.0.1 中无法连接到 Azure 表存储
【发布时间】:2017-08-23 13:45:04
【问题描述】:

我最近将我的 asp.net 核心项目从 rc2 版本升级到 1.0.1。我无法弄清楚如何连接到 Azure 表存储。似乎有什么东西阻止了我的连接。

请注意,此代码在我的本地开发盒 Win10 15063 上运行。它不适用于我的开发服务器,运行 Win2012R2。它也适用于 .net core 预览版 1.0.0-preview2-003133,但不适用于 .net core 最终版 1.0.1

我正在使用最新的 Azure 存储包 8.1.1

这是我的代码

var azureTableStorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=blah_blahstoragedev;AccountKey=Blahblah;EndpointSuffix=core.windows.net"; //Configuration["AzureStorageConnectionString"];

storageAccount = CloudStorageAccount.Parse(connectionString);
tableClient = storageAccount.CreateCloudTableClient();

buildTable = tableClient.GetTableReference(BuildTableName);
buildTable.CreateIfNotExists();

因为这个异常而失败:

Microsoft.WindowsAzure.Storage.StorageException: '无法连接到远程服务器'

  • InnerException {“无法建立连接,因为目标机器主动拒绝 127.0.0.1:8888”} System.Exception {System.Net.Sockets.SocketException}

堆栈跟踪“在 Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy 策略, OperationContext operationContext)

在 Microsoft.WindowsAzure.Storage.Table.TableOperation.Execute(CloudTableClient 客户端,CloudTable 表,TableRequestOptions requestOptions, OperationContext operationContext)

在 Microsoft.WindowsAzure.Storage.Table.CloudTable.Exists(布尔值 primaryOnly, TableRequestOptions requestOptions, OperationContext 操作上下文)

在 Microsoft.WindowsAzure.Storage.Table.CloudTable.CreateIfNotExists(TableRequestOptions requestOptions, OperationContext operationContext)

【问题讨论】:

  • 防火墙?存储模拟器是否在本地运行?
  • 嘿,可能涉及防火墙,但肯定没有运行本地存储。但是我确实指定了连接字符串。他们应该连接到我的连接字符串所说的内容,对吧?
  • 好吧,它说 127.0.0.1 拒绝连接,所以你做错了什么

标签: azure azure-storage azure-table-storage


【解决方案1】:

我在安装了 .NET Core 1.0.1 的 Windows Server 2012 R2 上测试了以下代码,它在我这边运行良好。

public async static void CreateTable(string tableName)
{
    var connectionString = "my connection string";

    var storageAccount = CloudStorageAccount.Parse(connectionString);
    var tableClient = storageAccount.CreateCloudTableClient();

    Microsoft.WindowsAzure.Storage.Table.CloudTable buildTable = tableClient.GetTableReference(tableName);
    await buildTable.CreateIfNotExistsAsync();
    Console.WriteLine("Create success");
}

你和我的代码不同之处在于我在构建应用程序时不能使用 CreateIfNotExists 方法。我向我展示了以下错误。所以我改用了 CreateIfNotExistsAsync 方法。

错误 CS1061:“CloudTable”不包含“CreateIfNotExists”的定义,并且找不到接受“CloudTable”类型的第一个参数的扩展方法“CreateIfNotExists”

请使用异步方法并再次测试您的代码。这是来自 GitHub 的与之相关的讨论。

我们的 NetCore/Netstandard 支持还不包括 API 的同步实现。

Missing syncronous methods for dotnet core?

目标机器主动拒绝 127.0.0.1:8888

8888 通常用作代理或防病毒软件的端口。请确定创建表请求是否被您的防火墙或防病毒软件阻止。建议您使用工具(例如,Wireshark)来检查您的服务器是否成功发送了网络数据包。以下是我使用 .NET Core 1.0.1 和 Azure Storage SDK(由 Fiddler 捕获)创建新表时从我的 Windows Server 2012 R2 发送的请求。

POST https://mystroagename.table.core.windows.net/Tables() HTTP/1.1
Connection: Keep-Alive
Content-Type: application/json
Accept: application/json; odata=minimalmetadata
Accept-Charset: UTF-8
Authorization: SharedKey mystroagename:mykey
User-Agent: Azure-Storage/8.1.1 (.NET Core)
MaxDataServiceVersion: 3.0;NetFx
Prefer: return-no-content
DataServiceVersion: 3.0;
x-ms-client-request-id: 5eeb6570-0ca0-42c7-844a-6acdcc3b7bb9
x-ms-version: 2016-05-31
x-ms-date: Mon, 03 Apr 2017 07:26:43 GMT
Content-Length: 23
Host: mystroagename.table.core.windows.net

{"TableName":"newtablename"}

【讨论】:

  • 您好,感谢您试用。让我试试看。
  • 阿莫尔,感谢您的帮助。原来有一个指向 127.0.0.0:8888 的 defaultproxy 条目 machine.config。不知何故,新的 .net 核心尊重这一点并将我的呼叫重定向到 127.0.0.0:8888
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多