我正在尝试在我的Azure Function应用中使用Cosmos DB Document客户端。我已遵循此处提到的步骤-How can I use NuGet packages in my Azure Functions?
我在project.json-中有依赖项
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.DocumentDB.Core": "2.1.3"
}
}
}
}
project.json放置在功能应用程序内部,并且应用程序服务编辑器路径如下-
https://<functionappname>.scm.azurewebsites.net/dev/wwwroot/<functionname>/project.json
这是我的功能代码-
#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);
}
当我保存并运行该应用程序时,没有收到任何错误或响应。如果我删除CosmosDB参考代码,它将起作用。下面是代码-
#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");
}
我错过了什么还是做错了吗?如果在控制台应用程序上使用相同的代码,则可以使用。请帮忙吗?
答案 0 :(得分:1)
要在门户网站中工作,如果添加带有Cosmos DB Trigger
的功能会更容易,这会将Cosmos DB扩展名拉入Function App,然后可以删除创建的Function并创建一个新功能并拉出DocumentClient
:
添加扩展后,您可以拉客户端在函数代码顶部添加#r "Microsoft.Azure.DocumentDB.Core"
:
#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");
}
答案 1 :(得分:0)
Function 2.0如下使用function.proj
。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.1.3"/>
</ItemGroup>
</Project>
并在添加程序集后删除多余的#r "Microsoft.Azure.Documents.Client"
。