简体   繁体   English

在 azure 函数中使用 MongoDB.Driver

[英]Using MongoDB.Driver inside azure functions

I'm trying to use an azure function to upload data to a mongodb but I get an error saying "The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)".我正在尝试使用 azure 函数将数据上传到 mongodb,但我收到一条错误消息,提示“找不到类型或命名空间名称‘MongoDB’(您是否缺少 using 指令或程序集引用?)”。 I have a function.proj file that has refs to the MongoDB.Driver package but that doesn't seem to be working.我有一个 function.proj 文件,其中包含对 MongoDB.Driver 包的引用,但这似乎不起作用。 I tried using the #r sytax to import the package but thats not working either.我尝试使用 #r 语法导入包,但那也不起作用。 I'm using version 3 of the azure funtion runtime.我正在使用 azure 功能运行时的第 3 版。 Can anybody point me in the right direction?有人能指出我正确的方向吗?

Contents of the function.proj file function.proj 文件的内容

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="MongoDB.Driver.Core" Version="2.11.3" />
    <PackageReference Include="MongoDB.Driver" Version="2.11.3" />
    <PackageReference Include="MongoDB.Bson" Version="2.11.3" />
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</ItemGroup>

Using directives使用指令

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using MongoDB.Driver;
using MongoDB.Bson;

Update:更新:

Thanks for Ggd Hhdhd's working.感谢 Ggd Hhdhd 的工作。 Azure function can not install some package on azure when based on crx.基于crx的Azure函数无法在azure上安装某些包。 The solution is to send related dll directly to the bin directory on azure.解决办法是将相关的dll直接发送到azure上的bin目录。 This is the doc:这是文档:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#referencing-custom-assemblies https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#referencing-custom-assemblies

And this is the structure:这是结构:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#folder-structure https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#folder-structure

Original Answer:原答案:

Please don't use C# script to do this.请不要使用 C# 脚本来执行此操作。

Some package is not supported to install in C# script azure function on azure.某些包不支持安装在 azure 上的 C# 脚本 azure 函数中。 I notice you install three package, but in fact, you just need to install MongoDB.Driver is ok.我注意到你安装了三个包,但实际上你只需要安装 MongoDB.Driver 就可以了。 MongoDB.Driver is an integrated package that includes MongoDB.Driver.Core and MongoDB.Bson. MongoDB.Driver 是一个集成包,包括 MongoDB.Driver.Core 和 MongoDB.Bson。

Problem is not from your code.问题不是来自您的代码。 The problem comes from just install the MongoDB.Driver package will broken the function.问题来自只安装 MongoDB.Driver 包会破坏功能。

By the way, the format of the function.proj should be like this:顺便说一下,function.proj 的格式应该是这样的:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="MongoDB.Driver" Version="2.11.3" />
    </ItemGroup>
</Project>

You miss the </Project> .你错过了</Project>

I try to find some package but didn't find.我试图找到一些包裹但没有找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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