I am trying to use Cosmos DB Document client in my Azure Function App. I have followed the steps mentioned here- How can I use NuGet packages in my Azure Functions?
I have the dependecy in project.json-
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.DocumentDB.Core": "2.1.3"
}
}
}
}
The project.json is placed inside the function app and the app service editor path is like this-
https://<functionappname>.scm.azurewebsites.net/dev/wwwroot/<functionname>/project.json
This is my function code-
#r "Newtonsoft.Json"
#r "Microsoft.Azure.Documents.Client"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Formatting;
using System.Threading;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string endPoint = <ep>;
string databaseId = <dbID>;
string collectionId = <cid>;
string documentId = <did>;
string resourceLink = string.Format("dbs/{0}/colls/{1}/docs/{2}", databaseId, collectionId, documentId);
string primaryKey = <pk>;
IDocumentClient documentClient = new DocumentClient(new Uri(endPoint), primaryKey);
var response = documentClient.ReadDocumentAsync(resourceLink).Result;
return new OkObjectResult(response);
}
When I Save and Run the app, I don't get any error or response. If I remove the CosmosDB reference codes it works. Below is the code-
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Formatting;
using System.Threading;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
return new OkObjectResult("response");
}
Am I missing something or doing this incorrectly? The same code works if I use it on a Console application. Any help with this please?