Error CS0006: Metadata file 'MongoDB' could not be found

♀尐吖头ヾ 提交于 2020-01-16 14:10:15

问题


I have created a Azure Function and it has below code in `run.csx'

using System;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
using MongoDB.Bson.IO;
using MongoDB.Bson;
using MongoDB;
using MongoDB.Driver;
using System.Security.Authentication;
using System.Text;
using Newtonsoft.Json;

public static void Run(string myIoTHubMessage, ILogger log)
{
    log.LogInformation($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");
 }

I am having Project.json as follow

  {
    "frameworks": {
    "net46":{
    "dependencies": {
      "Newtonsoft.Json": "10.0.3",
      "System.ServiceModel.Primitives":"4.4.0",
      "MongoDB.Bson": "2.4.0",
      "MongoDB.Driver": "2.4.0",
      "MongoDB.Driver.Core": "2.4.0"
    }
  }
 }
}

I am getting below error while running the azure function

2019-01-11T10:01:14.846 [Error] run.csx(5,27): error CS0234: The type or namespace name 'Description' does not exist in the namespace 'System.ServiceModel' (are you missing an assembly reference?)

2019-01-11T10:01:15.108 [Error] run.csx(6,7): error CS0246: The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)

I even tried adding namespace like below but no luck

#r "Newtonsoft.Json"
#r "System.Xml"
#r "System.Xml.Linq" 
#r "MongoDB"

回答1:


It's probably caused by the difference of Function runtime.

project.json is used for functions on ~1 runtime where code targets at .NET Framework, while the function you create is on ~2 runtime which runs on .NET Core env. When we create a new Function app its runtime is set to ~2 by default now.

So the solution is simple, delete existing functions in the Function app and change Function runtime version to ~1(Find it in portal, Platform features>Function app settings). Then you could recreate an IoT Hub (Event Hub) trigger with steps above, things should work this time.

To work with Function 2.0, use function.proj to install packages.

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="<packageName>" Version="<version>"/>
    </ItemGroup>
</Project>


来源:https://stackoverflow.com/questions/54144242/error-cs0006-metadata-file-mongodb-could-not-be-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!