无法将值“”解析为 Azure 函数中 Cosmos DocumentQuery 中的 ResourceId
Failed to parse the value '' as ResourceId in Cosmos DocumentQuery in Azure Function
我有一个 Azure Functions 2.0.12562,它在尝试查询 CosmosDB 时出现以下错误。
Error: DoesCosmosContain failed: Failed to parse the value '' as
ResourceId., Windows/10.0.18362 documentdb-netcore-sdk/2.4.0
Cosmos 中的一个条目看起来像
{
"id": "851a3506-3915-4f7c-9e32-32d927055555",
"_rid": "Tg8ZAI-iVnQBAAAAAAAADQ==",
"_self": "dbs/Tg8ZAA==/colls/Tg8ZAI-iVnQ=/docs/Tg8ZAI-iVnQBAAAAAAAADQ==/",
"_etag": "\"0f00e289-0000-0500-0000-5d1676600000\"",
"Prop_0": "555",
"Prop_1": "5551234",
"_attachments": "attachments/",
"_ts": 1561755555
}
此 Azure 函数接收 POST 中的所有数据库连接器并且正常工作。
该错误在 ExecuteNextAsync 行引发。
[FunctionName("DoesCosmosContain")]
public static async Task<bool> DoesCosmosContain([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
try
{
MyCosmosInformation scaleParams = await MyCosmosInformation.CreateAsync(req);
// shimming out locals
string key = scaleParams.CosmosKey;
string uri = scaleParams.CosmosUri;
Uri cosmosUri = new Uri(uri);
string offerLink = scaleParams.OfferLink;
using (DocumentClient client = new DocumentClient(cosmosUri, key))
{
IDocumentQuery<PhoneNumber> query = client.CreateDocumentQuery<PhoneNumber>(offerLink)
.Where(p => p.AreaCode=="555" && p.Number=="5551234")
.AsDocumentQuery();
while (query.HasMoreResults)
{
foreach (PhoneNumber result in await query.ExecuteNextAsync())
{
return true;
}
}
return false;
}
}
catch (Exception e)
{
string error = $"DoesCosmosContain failed: {e.Message}";
log.Error(error);
}
return true;
}
这是包装器 class 以帮助解析 POST JSON 以创建数据库连接。
// Wrapper Class for Post Json data
public class MyCosmosInformation
{
public async static Task<MyCosmosInformation> CreateAsync(HttpRequestMessage req)
{
string data = await req.Content.ReadAsStringAsync();
MyCosmosInformation returnValue = JsonConvert.DeserializeObject<MyCosmosInformation>(data);
if (returnValue == null)
{
throw new ArgumentNullException("Unable to correctly parse post body into MyCosmosInformation");
}
return returnValue;
}
public int Throughput { get; set; }
public string OfferLink { get; set; }
public string CosmosUri { get; set; }
public string CosmosKey { get; set; }
public string CosmosDbId { get; set; }
public string CosmosCollectionId { get; set; }
}
这是发布的JSON。审查我的秘密。
{
"CosmosCollectionId" : "myCollectionId",
"CosmosUri": "https://myEndpoint.documents.azure.com:443",
"CosmosKey": "MyCosmosKey8eXXXbkrYVSENSOREDgy8eeOMITTED==",
"CosmosDbId": "myDBId",
"OfferLink": "offers/MYID/",
}
PhoneNumberClass
internal sealed class PhoneNumber
{
[JsonProperty(PropertyName = "Prop_0")]
public string AreaCode { get; set; }
[JsonProperty(PropertyName = "Prop_1")]
public string Number { get; set; }
public string ResourceId { get; set; }
}
这是无效的:client.CreateDocumentQuery<PhoneNumber>(offerLink)
您使用的 offerLink 看起来像这样 offers/MYID/
,但是 CreateDocumentQuery
方法需要 collectionLink
而不是 offerLink
。
根据您已有的值,您可以像这样构建 colletionLink
:
var collectionLink = UriFactory.CreateDocumentCollectionUri(scaleParams.CosmosDbId, scaleParams.CosmosCollectionId)
我有一个 Azure Functions 2.0.12562,它在尝试查询 CosmosDB 时出现以下错误。
Error: DoesCosmosContain failed: Failed to parse the value '' as ResourceId., Windows/10.0.18362 documentdb-netcore-sdk/2.4.0
Cosmos 中的一个条目看起来像
{
"id": "851a3506-3915-4f7c-9e32-32d927055555",
"_rid": "Tg8ZAI-iVnQBAAAAAAAADQ==",
"_self": "dbs/Tg8ZAA==/colls/Tg8ZAI-iVnQ=/docs/Tg8ZAI-iVnQBAAAAAAAADQ==/",
"_etag": "\"0f00e289-0000-0500-0000-5d1676600000\"",
"Prop_0": "555",
"Prop_1": "5551234",
"_attachments": "attachments/",
"_ts": 1561755555
}
此 Azure 函数接收 POST 中的所有数据库连接器并且正常工作。 该错误在 ExecuteNextAsync 行引发。
[FunctionName("DoesCosmosContain")]
public static async Task<bool> DoesCosmosContain([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
try
{
MyCosmosInformation scaleParams = await MyCosmosInformation.CreateAsync(req);
// shimming out locals
string key = scaleParams.CosmosKey;
string uri = scaleParams.CosmosUri;
Uri cosmosUri = new Uri(uri);
string offerLink = scaleParams.OfferLink;
using (DocumentClient client = new DocumentClient(cosmosUri, key))
{
IDocumentQuery<PhoneNumber> query = client.CreateDocumentQuery<PhoneNumber>(offerLink)
.Where(p => p.AreaCode=="555" && p.Number=="5551234")
.AsDocumentQuery();
while (query.HasMoreResults)
{
foreach (PhoneNumber result in await query.ExecuteNextAsync())
{
return true;
}
}
return false;
}
}
catch (Exception e)
{
string error = $"DoesCosmosContain failed: {e.Message}";
log.Error(error);
}
return true;
}
这是包装器 class 以帮助解析 POST JSON 以创建数据库连接。
// Wrapper Class for Post Json data
public class MyCosmosInformation
{
public async static Task<MyCosmosInformation> CreateAsync(HttpRequestMessage req)
{
string data = await req.Content.ReadAsStringAsync();
MyCosmosInformation returnValue = JsonConvert.DeserializeObject<MyCosmosInformation>(data);
if (returnValue == null)
{
throw new ArgumentNullException("Unable to correctly parse post body into MyCosmosInformation");
}
return returnValue;
}
public int Throughput { get; set; }
public string OfferLink { get; set; }
public string CosmosUri { get; set; }
public string CosmosKey { get; set; }
public string CosmosDbId { get; set; }
public string CosmosCollectionId { get; set; }
}
这是发布的JSON。审查我的秘密。
{
"CosmosCollectionId" : "myCollectionId",
"CosmosUri": "https://myEndpoint.documents.azure.com:443",
"CosmosKey": "MyCosmosKey8eXXXbkrYVSENSOREDgy8eeOMITTED==",
"CosmosDbId": "myDBId",
"OfferLink": "offers/MYID/",
}
PhoneNumberClass
internal sealed class PhoneNumber
{
[JsonProperty(PropertyName = "Prop_0")]
public string AreaCode { get; set; }
[JsonProperty(PropertyName = "Prop_1")]
public string Number { get; set; }
public string ResourceId { get; set; }
}
这是无效的:client.CreateDocumentQuery<PhoneNumber>(offerLink)
您使用的 offerLink 看起来像这样 offers/MYID/
,但是 CreateDocumentQuery
方法需要 collectionLink
而不是 offerLink
。
根据您已有的值,您可以像这样构建 colletionLink
:
var collectionLink = UriFactory.CreateDocumentCollectionUri(scaleParams.CosmosDbId, scaleParams.CosmosCollectionId)