简体   繁体   中英

Reference Cosmos DB nuget package in Azure Functions

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?

To work in the Portal it's easier if you add a Function with the Cosmos DB Trigger , that will pull the Cosmos DB Extension into the Function App, and then you can delete the created Function and create a new one and pull the DocumentClient :

搜索触发器

添加扩展

Once the Extension is added, you can pull the client adding #r "Microsoft.Azure.DocumentDB.Core" on the top of your Function's code:

#r "Newtonsoft.Json"
#r "Microsoft.Azure.DocumentDB.Core"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;

private static DocumentClient client = new DocumentClient(new Uri("yourendpoint"), "yourkey");


public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    return name != null
        ? (ActionResult)new OkObjectResult($"Hello, {name}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

Function 2.0 uses function.proj as below.

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.1.3"/>
    </ItemGroup>
</Project>

And remove extra #r "Microsoft.Azure.Documents.Client" as the assembly has been added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM