无法连接到 windows 2012 r2 和 .net core 1.0.1 中的 Azure Table 存储

Unable to connect to Azure Table Storage in windows 2012 r2 and .net core 1.0.1

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

请注意,此代码适用于我的本地开发箱 Win10 15063。它在我的开发服务器 运行 Win2012R2 上不起作用。它还适用于 .net 核心预览版 1.0.0-preview2-003133,但不适用于 .net 核心最终版 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: 'Unable to connect to the remote server'

StackTrace " at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)

at Microsoft.WindowsAzure.Storage.Table.TableOperation.Execute(CloudTableClient client, CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext)

at Microsoft.WindowsAzure.Storage.Table.CloudTable.Exists(Boolean primaryOnly, TableRequestOptions requestOptions, OperationContext operationContext)

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

我在安装了 .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 方法。

error CS1061: 'CloudTable' 不包含 'CreateIfNotExists' 的定义并且没有扩展方法 'CreateIfNotExists' 接受类型 'CloudTable' 的第一个参数可以被发现

请使用异步方法并再次测试您的代码。这是来自 GitHub.

的相关讨论

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

Missing syncronous methods for dotnet core?

the target machine actively refused it 127.0.0.1:8888

8888 通常用作代理或防病毒软件的端口。请标记确定创建 table 请求是否被您的防火墙或防病毒软件阻止。我建议您使用一个工具(例如Wireshark)来检查您的服务器是否成功发送了网络数据包。以下是当我使用 .NET Core 1.0.1 和 Azure Storage SDK(由 Fiddler 捕获)创建新的 table 时从我的 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"}