编程语言
首页 > 编程语言> > c#-Azure存储模拟器(表)返回400错误请求或403禁止

c#-Azure存储模拟器(表)返回400错误请求或403禁止

作者:互联网

问题:访问Azure开发表存储时我缺少什么?

注意:我可以访问我的Azure CLOUD存储(当然可以使用其他代码),但是尝试访问开发存储时失败.

我正在使用:

> Visual Studio 2012
> .Net Framework 4.0 C#库
> NuGet Widows Azure存储v6.0< ==更正-使用v6.1.0.0
>适用于.net 2.6的Microsoft Azure SDK
> Microsoft Azure存储模拟器v4.0<-更改为v4.2已修复的问题

var cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;

var tableClient = cloudStorageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference("MYTEMPTABLE");
var iscreated = table.CreateIfNotExists();

最后一条语句给出了此异常

The remote server returned an error: (400) Bad Request.
The value for one of the HTTP headers is not in the correct format.
RequestId:f0b37575-30f4-45c1-bec3-2620c3c605e7
Time:2015-11-04T16:12:37.4719620Z

堆栈跟踪

    at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 816
    at Microsoft.WindowsAzure.Storage.Table.TableOperation.Execute(CloudTableClient client, CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Table\TableOperation.cs:line 41
    at Microsoft.WindowsAzure.Storage.Table.CloudTable.Exists(Boolean primaryOnly, TableRequestOptions requestOptions, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Table\CloudTable.cs:line 1605
    at Microsoft.WindowsAzure.Storage.Table.CloudTable.CreateIfNotExists(TableRequestOptions requestOptions, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Table\CloudTable.cs:line 1024
    at USPS.Cloud.Integration.AspProviders.UspsReturnsStorageBase.CreateStorageAccountFromConnectionString() in ... <my local code call stack>

仅供参考:在搜索MSDN,StackOverflow等时,我发现了三种获取CloudStorageAccount对象以访问存储模拟器的方法.前两个给出上面的错误.第三个给出了403错误.

CloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
CloudStorageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
var devAccountName = "devstoreaccount1";
var devAccountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
var devCredentials = new StorageCredentials(devAccountName, devAccountKey);
var cloudStorageAccount = new CloudStorageAccount(devCredentials, true);

更新

如答案中所述,我没有正确的仿真器版本.
开发人员存储连接1& 2以上工作.跟随@Emily Gerner答案中的链接-MSFT使我想到了一个可行的选择3.

var devConnectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;";
CloudStorageAccount = CloudStorageAccount.Parse(devConnectionString);

解决方法:

第三个不起作用,因为您未设置仿真器端点,而是发送到服务帐户devstoreaccount1而不是本地仿真器.例如,尝试使用TableEndpoint = http://127.0.0.1:10002/devstoreaccount1.如有必要,Azure emulator docs具有更多详细信息.

如果看到README section on the Emulator,则将看到最新的存储库版本需要最低仿真器版本4.2.这也应该提供下载链接.由于所使用的库版本使用旧模拟器无法得知的服务版本,因此您会收到400 Bad Request.

标签:visual-studio-2012,azure-storage,c
来源: https://codeday.me/bug/20191027/1946624.html