Reputation: 8531
I have an Azure Function 2.0.12562 that is getting the following error when trying to query CosmosDB.
Error: DoesCosmosContain failed: Failed to parse the value '' as ResourceId., Windows/10.0.18362 documentdb-netcore-sdk/2.4.0
An entry in Cosmos looks like
{
"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
}
This Azure function receives all the DB connectors in the POST and that working. The error throws on the ExecuteNextAsync line.
[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;
}
This is the wrapper class to help parse the POST JSON to create the DB Connection.
// 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; }
}
This is the posted JSON. Censored for my secrets.
{
"CosmosCollectionId" : "myCollectionId",
"CosmosUri": "https://myEndpoint.documents.azure.com:443",
"CosmosKey": "MyCosmosKey8eXXXbkrYVSENSOREDgy8eeOMITTED==",
"CosmosDbId": "myDBId",
"OfferLink": "offers/MYID/",
}
The 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; }
}
Upvotes: 0
Views: 886
Reputation: 7190
This is not valid: client.CreateDocumentQuery<PhoneNumber>(offerLink)
You are using an offerLink which looks like this offers/MYID/
, however the CreateDocumentQuery
method expects a collectionLink
not an offerLink
.
Based on the values you already have you can construct the colletionLink
like this:
var collectionLink = UriFactory.CreateDocumentCollectionUri(scaleParams.CosmosDbId, scaleParams.CosmosCollectionId)
Upvotes: 1