【问题标题】:Connecting to MongoDB from azure function从 azure 函数连接到 MongoDB
【发布时间】:2017-02-04 21:15:12
【问题描述】:

我在从运行在 microsoft azure 上的 C# 函数连接到 MongoDB 时遇到问题。该函数是时间触发的,它应该从MongoDB收集一些数据并将它们放入存储队列。

我已将 mongoDB 依赖项添加到 to project.json,文件如下所示:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "MongoDB.Bson": "2.4.2",
        "MongoDB.Driver": "2.4.2",
        "MongoDB.Driver.Core": "2.4.2",
        "System.Runtime.InteropServices.RuntimeInformation": "4.0.0"
      }
    }
  }
}

但是当我打电话时:

MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(conn));
settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
var client = new MongoClient(settings);

调用最后一行后出现错误:

mscorlib: Exception has been thrown by the target of an invocation. MongoDB.Driver.Core: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

我检查过,.net 版本是 4.6,所以应该没有问题。我还有什么遗漏的吗?

【问题讨论】:

  • 为什么需要mongodb驱动的互操作依赖?
  • MongoDB.Driver.Core 依赖于互操作。为什么?我真的不知道。这是驱动程序开发人员的问题。
  • 我还尝试添加一个全新的函数,直接引用所需的程序集,结果是一样的。

标签: c# mongodb azure azure-functions


【解决方案1】:

我一直在研究他们的 JIRA 问题并找到了JIRA ISSUE。我已将 MongoDB.Driver MongoDB.Driver.CoreMongoDB.Bson 恢复为 2.3.0 版本,它的工作原理就像一个经过微调的手表,所以这似乎是 Mongo 包本身的问题。

我已经使用不同版本的软件包对其进行了测试,并且在 2.4.0 版本中引入了该问题。所有以前的版本都运行良好。

【讨论】:

  • 谢谢伊万,这就是原因。如果有人感兴趣,我为 Azure 功能制作了一个 nuget 包:nuget.org/packages/AzureMongoRepository.AzureFunctions
  • 你是我的主,救世主保佑你
  • 我们如何在 azure 门户中添加 nuget 库?我面临着类似的问题。 stackoverflow.com/questions/54144242/…
  • 如果有人对 JavaScript 有同样的问题,您必须使用 JS Mongo DB 驱动程序,您可以在 here 中找到一个工作代码示例,展示如何安装所需的依赖项。
猜你喜欢
  • 2018-10-26
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
相关资源
最近更新 更多