Using MongoDB.Driver inside azure functions

Viewed 326

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?)". I have a function.proj file that has refs to the MongoDB.Driver package but that doesn't seem to be working. I tried using the #r sytax to import the package but thats not working either. I'm using version 3 of the azure funtion runtime. Can anybody point me in the right direction?

Contents of the function.proj file

<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;
1 Answers

Update:

Thanks for Ggd Hhdhd's working. Azure function can not install some package on azure when based on crx. The solution is to send related dll directly to the bin directory on azure. This is the doc:

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

Original Answer:

Please don't use C# script to do this.

Some package is not supported to install in C# script azure function on azure. I notice you install three package, but in fact, you just need to install MongoDB.Driver is ok. MongoDB.Driver is an integrated package that includes MongoDB.Driver.Core and MongoDB.Bson.

Problem is not from your code. The problem comes from just install the MongoDB.Driver package will broken the function.

By the way, the format of the function.proj should be like this:

<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>.

I try to find some package but didn't find.

Related