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

Azure Storage Emulator: The value for one of the HTTP headers is not in the correct format

我是 writing/debugging Azure Functions,在我的本地计算机上使用 Azure 存储模拟器。不幸的是,我不太确定应该使用哪个 API(s) 或正确的存储 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;
, 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)
Exception thrown: 'Microsoft.Azure.Documents.DocumentClientException' in System.Private.CoreLib.dll

Azure 存储模拟器 运行 我可以在存储资源管理器中操作它。

谁能帮我找出我做错了什么?我为此使用 'best' API 吗?

根据您的代码,您希望使用 cosmosdb 来存储您的 StoredScore 数据。如果是这样,您应该使用您身边的 CosmosDB 模拟器而不是存储模拟器。

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

  1. Install and run CosmosDB emulator

  2. 在本地创建一个带有集合的 cosmos 数据库:

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

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

3.Create 本地 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 中:

本地cosmos DB中的数据:

希望对您有所帮助。