当前位置: 首页 > 知识库问答 >
问题:

参考Azure Functions中的Cosmos DB nuget包

常子濯
2023-03-14

我正在尝试在我的Azure Function应用程序中使用Cosmos DB Document客户端。我已按照此处提到的步骤-如何在我的Azure Functions中使用NuGet包?

我依赖于project.json-

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.Azure.DocumentDB.Core": "2.1.3"
      }
    }
   }
}

project.json放在函数app里面,app服务编辑器路径是这样的-

< code>https://

这是我的函数代码-

#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);
}

当我保存并运行应用程序时,我没有收到任何错误或响应。如果我删除宇宙数据库参考代码,它的工作原理。下面是代码-

#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");
}

我是否遗漏了某些内容或错误地执行了此操作?如果我在控制台应用程序上使用它,则相同的代码将起作用。对此有什么帮助吗?

共有2个答案

洪和平
2023-03-14

函数 2.0 使用函数.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>

并删除额外的<code>#r“Microsoft.Azure.Documents.Client”

唐兴思
2023-03-14

要在门户中工作,如果您使用< code>Cosmos DB Trigger添加一个函数会更容易,这会将Cosmos DB扩展拉入函数应用程序,然后您可以删除已创建的函数,创建一个新函数,并拉入< code>DocumentClient:

一旦添加了扩展,您就可以拉客户端添加< code>#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");
}
 类似资料:
  • Azure Functions for Visual Studio Code Use the Azure Functions extension to quickly create, debug, manage, and deploy serverless apps directly from VS Code. Check out the Azure serverless community li

  • Modules 参考了以下开源模块: adm-zip clean-css colors fs-extra glob howdo open uglify-js ydr-utils seajs fis webpack posthtml Articles 感谢以下文章: FED社区:coolie 介绍专辑 FED社区:跟我学 coolie 专辑 知乎:大公司里怎样开发和部署前端代码? InfoQ:前端工

  • http://openstack.redhat.com/Networking_in_too_much_detail http://web.archive.org/web/20150215214007/http://masimum.inf.um.es/fjrm/2013/12/26/the-journey-of-a-packet-within-an-openstack-cloud http://pa

  • ECMAScript 6 入门。 ESlint是一个javascript代码审查框架。 Testem A test runner that makes Javascript unit testing fun. 创建一个testem的测试目录,然后安装testem: $ cd /opt && mkdir testem && cd /opt/testem $ npm install testem -g

  • Hyperledger Composer参考资料包含许多主题,包括npm模块的参考信息、CLI命令、建模语言、API、连接配置文件和常用术语表。 Historian(历史记录) Hyperledger Composer Historian提供了一个库,其中包含历史交易的信息 Hyperledger Composer npm模块 Hyperledger Composer包含许多npm模块,提供使用H

  • 参考 [1] - http://www.autosec.org/pubs/cars-oakland2010.pdf [2] - http://www.autosec.org/pubs/cars-usenixsec2011.pdf [3] - http://illmatics.com/content.zip [4] - http://www.forbes.com/sites/andygreenber

  • koa - Github koa - 官网 Koa - 中文文档 wiki - Coroutine wiki - 地球构造 nikic - 在PHP中使用协程实现多任务调度(译文) nikic - FastRoute 阮一峰 - Generator 函数的含义与用法 阮一峰 - Thunk 函数的含义和用法 阮一峰 - co 函数库的含义和用法 PHP RFC - generator PHP RF

  • 问题内容: 我最近在Mac上安装了许多点文件以及其他一些应用程序(我改为使用iTerm代替Terminal,将Sublime设置为默认文本编辑器),但此后,尽管它们的文件夹位于.virtualenvs中,但我所有的虚拟环境都停止了工作仍然在那里,每当我尝试在其中运行任何命令时,它们都会给出以下错误: 我已经删除了所有与dotfiles相关的文件,并将.bash_profile还原到以前的状态,但是