【问题标题】:Type or Namespace name not found in Azure Functions在 Azure Functions 中找不到类型或命名空间名称
【发布时间】:2018-10-10 18:36:23
【问题描述】:

我正在尝试将一些模拟数据推送到 azure iot hub,并使用 azure 函数(C#)将接收到的数据存储到 mongo db 中。接收到 azure 函数的 iot hub 消息正在工作。当我尝试按如下方式将它们推送到 mongo db 时,会出现以下错误。我在执行此操作时遵循了this 教程。

我的 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;
using Newtonsoft.Json.Linq;

public static void Run(string myIoTHubMessage, TraceWriter log)
{
    log.Info($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");
    string deviceId="",data="";
    var raw_obj=JObject.Parse(myIoTHubMessage);
    deviceId=(string)raw_obj["device_id"];
    data=(string)raw_obj["Data"];
    Cosmos cosmos= new Cosmos(deviceId,data);
    cosmos.pushData();
}

//CosmosDB class
public class Cosmos
{
    string deviceId="",data="";
    public BsonDocument document = new BsonDocument();
    public Cosmos(string deviceId, string data)
    {
        this.deviceId=deviceId;
        this.data=data;
    }
    public void pushData()
    {
        MainAsync().Wait();
    }
    public async Task MainAsync()
    {
        string connectionString = 
    @"mongodb://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
        settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12};
        var mongoClient = new MongoClient(settings);
        IMongoDatabase db = mongoClient.GetDatabase("iot");
        var icollection = db.GetCollection<BsonDocument>(deviceId);
        document.Add("Data",data);
        await icollection.InsertOneAsync(document);
    }

}

我的 project.json 文件

{
  "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"
      }
    }
   }
}

当我运行代码时出现以下错误

2018-10-10T18:34:25.990 [Error] Function compilation error
2018-10-10T18:34:26.119 [Error] run.csx(3,27): error CS0234: The type or namespace name 'Description' does not exist in the namespace 'System.ServiceModel' (are you missing an assembly reference?)
2018-10-10T18:34:26.242 [Error] run.csx(4,7): error CS0246: The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)
2018-10-10T18:34:26.332 [Error] run.csx(5,7): error CS0246: The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)
2018-10-10T18:34:26.435 [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?)
2018-10-10T18:34:26.548 [Error] run.csx(7,7): error CS0246: The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)
2018-10-10T18:34:26.666 [Error] run.csx(10,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
2018-10-10T18:34:26.771 [Error] run.csx(11,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
2018-10-10T18:34:26.828 [Warning] run.csx(13,48): warning CS0618: 'TraceWriter' is obsolete: 'Will be removed in an upcoming version. Use ILogger instead.'
2018-10-10T18:34:26.946 [Error] run.csx(28,12): error CS0246: The type or namespace name 'BsonDocument' could not be found (are you missing a using directive or an assembly reference?)
2018-10-10T18:34:27.058 [Error] run.csx(17,17): error CS0103: The name 'JObject' does not exist in the current context
2018-10-10T18:34:27.201 [Error] run.csx(28,40): error CS0246: The type or namespace name 'BsonDocument' could not be found (are you missing a using directive or an assembly reference?)
2018-10-10T18:34:27.304 [Error] run.csx(42,5): error CS0246: The type or namespace name 'MongoClientSettings' could not be found (are you missing a using directive or an assembly reference?)
2018-10-10T18:34:27.431 [Error] run.csx(42,36): error CS0103: The name 'MongoClientSettings' does not exist in the current context
2018-10-10T18:34:27.632 [Error] Executed 'Functions.EventHubTriggerCSharp1' (Failed, Id=32bc6c5d-73fa-4082-b74b-c86a901f6656)

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: c# mongodb azure azure-functions


    【解决方案1】:

    问题是由 Function runtime 的差异引起的。

    您遵循的教程在 ~1 运行时上创建函数,其中代码以 .NET Framework 为目标,而您创建的教程在 ~2 运行时上运行,该运行时在 .NET Core env 上运行。当我们创建一个新的函数应用程序时,它的运行时间现在默认设置为 ~2。

    解决方案是在门户的应用程序设置中将FUNCTIONS_EXTENSION_VERSION 设置为~1。然后点击功能面板上的View Files并编辑function.json使其在v1中工作-将eventHubName更改为path。见event hub trigger configuration

    还有一些改进,删除那些未在上下文中使用的以及 project.json 中的System.ServiceModel.PrimitivesNewtonsoft.Json 程序集存在但未添加到主机,需要显式引用。

    1. 程序集和命名空间使用

      #r "Newtonsoft.Json"
      
      using Newtonsoft.Json.Linq;
      using MongoDB.Bson;
      using MongoDB.Driver;
      using System.Security.Authentication;
      
    2. project.json.

      {
        "frameworks": {
          "net46":{
            "dependencies": {
              "MongoDB.Bson": "2.7.0",
              "MongoDB.Driver": "2.7.0"
            }
          }
         }
      }
      

    【讨论】:

    • 当我更改 FUNCTIONS_EXTENSION_VERSION to ~1 时,它给出了错误 Function (EventHubTriggerCSharp1) Error: Can't figure out which ctor to call.
    • @KrishanMadushanka 对不起,我的遗漏,您还需要点击View files 编辑function.json,将eventHubName 更改为path
    • 用javascript写azure函数解决了这个问题
    • @KrishanMadushanka 太好了,很高兴你成功了。如果不是太麻烦,您可以发布您的答案。
    • 当然我会发布答案。无论如何感谢您的跟进
    【解决方案2】:

    如果您使用的是 run.csx,则需要使用 #r“导入”您引用的程序集。

    The example below is from the documentation.

    #r "System.Web.Http"
    
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;
    
    public static Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
    

    【讨论】:

    • 导入 #r "Newtonsoft.Json" 有效,但 #r "System.Runtime.Serialization" 和 #r "MongoDB" 无效:/
    • 您确定程序集(及其依赖项)也都已上传吗?见这里stackoverflow.com/questions/36559509/…
    • 我已将它们添加到 project.json 文件中
    • 请阅读我在上一条评论中粘贴的链接。您还需要将它们上传到 Azure Functions 实例。
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 2017-07-12
    • 2011-05-13
    • 1970-01-01
    • 2013-03-25
    • 2018-05-09
    • 2012-06-19
    相关资源
    最近更新 更多