Azure存储模拟器:HTTP标头之一的值格式不正确

我正在使用本地计算机上的Azure存储模拟器编写/调试Azure函数。不幸的是,我不确定应该使用哪个API或用于存储的正确URL。我的代码如下:

CosmosClient client = new CosmosClient(storageURL,authKeyString);
Database db = client.GetDatabase("devstoreaccount1");
container = db.getcontainer("leaderboard");
ItemResponse<StoredScore> resp = await container.CreateItemAsync<StoredScore>(newScore);

该函数的输入是:

storageURL = "http://127.0.0.1:10002"
authKeyString = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="

在上面代码的最后一行,我得到一个异常:

DocDBTrace Warning: 0 : initializetask failed microsoft.Azure.Documents.DocumentClientException: <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code>InvalidHeaderValue</code>
  <message xml:lang="en-US">The value for one of the HTTP headers is not in the correct format.
RequestId:ed82efd8-6cb8-4a2e-a793-16a38b3c8f2a
Time:2019-12-20T20:07:19.9539313Z</message>
</error>
Requesturi: http://127.0.0.1:10002/;
RequestMethod: GET;
Header: x-ms-version Length: 10;
Header: User-Agent Length: 98;
Header: x-ms-date Length: 29;
Header: Authorization Length: 84;,Request URI: /,RequestStats:,SDK: Windows/10.0.16299 cosmos-netstandard-sdk/3.4.2
   at microsoft.Azure.Cosmos.GatewayStoreclient.ParseResponseAsync(HttpResponseMessage responseMessage,JsonSerializerSettings serializerSettings,DocumentServiceRequest request)
   at microsoft.Azure.Cosmos.GatewayaccountReader.GetDatabaseaccountAsync(Uri serviceEndpoint)
   at microsoft.Azure.Cosmos.Routing.GlobalEndpointManager.GetDatabaseaccountFromAnyLocationsAsync(Uri defaultEndpoint,IList`1 locations,Func`2 getDatabaseaccountFn)
   at microsoft.Azure.Cosmos.GatewayaccountReader.InitializeReaderAsync()
   at microsoft.Azure.Cosmos.CosmosaccountServiceConfiguration.InitializeAsync()
   at microsoft.Azure.Cosmos.DocumentClient.InitializeGatewayConfigurationReaderAsync()
   at microsoft.Azure.Cosmos.DocumentClient.GetInitializationTaskAsync(IStoreclientFactory storeclientFactory)
   at microsoft.Azure.Cosmos.DocumentClient.EnsureValidClientAsync()
DocDBTrace Error: 0 : DocumentClientException with status code BadRequest,message: <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code>InvalidHeaderValue</code>
  <message xml:lang="en-US">The value for one of the HTTP headers is not in the correct format.
RequestId:67e1f70a-c9c8-4aed-892c-964e8ff0f0e1
Time:2019-12-20T20:11:28.0006476Z</message>
</error>
Requesturi: http://127.0.0.1:10002/;
RequestMethod: GET;
Header: x-ms-version Length: 10;
Header: User-Agent Length: 98;
Header: x-ms-date Length: 29;
Header: Authorization Length: 82;,inner exception: null,and response headers: {
"Server": "microsoft-HTTPAPI/2.0","x-ms-request-id": "67e1f70a-c9c8-4aed-892c-964e8ff0f0e1","Date": "Fri,20 Dec 2019 20:11:27 GMT",}
DocDBTrace Information: 0 : Fail to reach global gateway http://127.0.0.1:10002/,microsoft.Azure.Documents.DocumentClientException: <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code>InvalidHeaderValue</code>
  <message xml:lang="en-US">The value for one of the HTTP headers is not in the correct format.
RequestId:67e1f70a-c9c8-4aed-892c-964e8ff0f0e1
Time:2019-12-20T20:11:28.0006476Z</message>
</error>
Requesturi: http://127.0.0.1:10002/;
RequestMethod: GET;
Header: x-ms-version Length: 10;
Header: User-Agent Length: 98;
Header: x-ms-date Length: 29;
Header: Authorization Length: 82;,Func`2 getDatabaseaccountFn)
Exception thrown: 'microsoft.Azure.Documents.DocumentClientException' in System.Private.CoreLib.dll

Azure存储模拟器正在运行,我可以在Storage Explorer中对其进行操作。

有人可以帮助我弄清楚我做错了什么吗?我是否为此使用了“最佳” API?

pao98222 回答:Azure存储模拟器:HTTP标头之一的值格式不正确

根据您的代码,您想使用cosmosdb存储StoredScore数据。如果是这样,则应使用CosmosDB仿真器而不是存储仿真器。

请按照以下步骤使您的代码正常工作:

  1. Install and run CosmosDB emulator

  2. 使用您的本地计算机上的集合创建一个cosmos数据库:

enter image description here

在这种情况下,我的cosmos数据库名称为localdb,而我的容器名称为localcontainer

您可以在此处找到您的cosmosdb主机和密钥: enter image description here

3。在VS中本地创建一个HTTP触发的Azure函数,这是我的以下代码:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos;

namespace cosmostest
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous,"get","post",Route = null)] HttpRequest req,ILogger log)
        {
            CosmosClient client = new CosmosClient("https://localhost:8081/","<your key here>");
            Database db = client.GetDatabase("<your local db name>");
            Container container = db.GetContainer("<your local container name>");
            var newScore = new StoredScore() { id ="1",name ="stan",score =100 };
            ItemResponse<StoredScore> resp = await container.CreateItemAsync<StoredScore>(newScore);
            return new OkObjectResult(resp.StatusCode);
        }
    }

    public class StoredScore
    {

        public string id { get; set; }
        public string name { get; set; }
        public int score { get; set; }
    }

}

运行该函数并触发它,如果响应为“ 201”,则您的数据已成功保存到本地cosmos DB中: enter image description here

本地cosmos DB中的数据: enter image description here

希望有帮助。

本文链接:https://www.f2er.com/2883427.html

大家都在问