【问题标题】:MongoDB driver throws file not found exceptionMongoDB 驱动程序抛出文件未找到异常
【发布时间】:2016-06-08 14:01:16
【问题描述】:

我正在将 ASP.NET Core 1 应用程序从 RC 1 迁移到 RC 2。一个应用程序具有 Web 应用程序本身(以 netcoreapp1.0 为目标)和两个用于模型和数据访问层的类库 (.NET Core) (netstandard1.5有针对性)。这些类库使用 MongoDB 驱动程序 2.2.4。应用程序恢复依赖关系并编译没有任何错误。但是,在初始化 MongoDB 客户端的过程中会抛出 FileNotFound 异常。

An exception of type 'System.IO.FileNotFoundException' occurred in MongoDB.Driver.dll but was not handled in user code

Additional information: Could not load file or assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

    at MongoDB.Driver.MongoUrlBuilder.Parse(String url)
    at MongoDB.Driver.MongoUrl..ctor(String url)
    at MongoDB.Driver.MongoClient..ctor(String connectionString)
    at GS.Quest.DataAccess.Context..ctor() in
CoreWebApp\src\DataAccess\Context.cs:line 9

什么会导致这个问题?目前是否可以将旧版 MongoDB 驱动程序用于 ASP.NET Core RC 2 应用程序,而无需等待 .NET 核心版本的驱动程序?此外,ASP.NET Core RC 1 应用程序没有问题。

Web 应用项目.json

{
  "userSecretsId": "aspnet-CoreWebApp-6f0c9639-a671-45df-afcb-f126e55c6e3e",

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "MongoDB.Driver": "2.2.4",
    "Models": "1.0.0.*",
    "DataAccess": "1.0.0.*"
  },

  "tools": {
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    },
    "Microsoft.Extensions.SecretManager.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "dnx451",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

模型 project.json

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.5.0-rc2-24027",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1-rc2-24027",
    "MongoDB.Driver": "2.2.4"
  },

  "frameworks": {
    "netstandard1.5": {
      "imports": [ "dnxcore50", "dnx451" ]
    }
  }
}

数据访问项目.json

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.5.0-rc2-24027",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1-rc2-24027",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "MongoDB.Driver": "2.2.4",
    "Models": "1.0.0-*"
  },

  "frameworks": {
    "netstandard1.5": {
      "imports": [ "dnxcore50", "dnx451" ]
    }
  }
}

在迁移过程中,我关注official documentation,然后我尝试将现有源代码移动到由 Visual Studio 2015 创建的示例 ASP.NET Core 1 RC 2 Web 应用程序。

更新

迁移到 net451 框架后,项目文件如下所示

网络应用

{
  "userSecretsId": "aspnet-CoreWebApp-6f0c9639-a671-45df-afcb-f126e55c6e3e",

  "dependencies": {
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "MongoDB.Driver": "2.2.4",
    "Models": "1.0.0.*",
    "DataAccess": "1.0.0.*"
  },

  "tools": {
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    },
    "Microsoft.Extensions.SecretManager.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "frameworks": {
    "net451": { }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

型号

{
  "version": "1.0.0-*",

  "dependencies": {
    "MongoDB.Driver": "2.2.4"
  },

  "frameworks": {
    "net451": { }
  }
}

数据访问

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Models": "1.0.0-*"
  },

  "frameworks": {
    "net451": { }
  }
}

【问题讨论】:

    标签: mongodb asp.net-core asp.net-core-1.0


    【解决方案1】:

    您不能在 netappcore1.0 应用程序中定位 MongoDB 驱动程序!请不要在netappcore1.0 导入中使用dnx451。 .NET 4.5.1 程序集不适用于 .NET Core 应用程序!仅 .NET Core 程序集(win8 和 win8.1/uwp 基于 .NET,dnx451 不是)。它本质上说:“在 .NET Core 应用程序中安装 MongoDb,即使它不针对/不支持它”。

    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "dnx451",
        "portable-net45+win8"
      ]
    }
    

    这将强制 nuget 恢复 dnx451/net451 包,即使它们不受支持。 imports 部分仅作为一种解决方法来强制恢复与 .NET 核心兼容但尚未针对 netstandard1.5 的包(如为 UWP、Win8 或 Win8.1 编写的包)。

    如果你想使用 MongoDB 驱动程序,你必须定位 net451,而不是 netcoreapp1.0netstandard1.5。这适用于所有引用 MongoDB 的项目和子项目。

    【讨论】:

    • 非常感谢您解决了这个问题!
    • 你回答中的例子是正确的还是不正确的?
    • @Son_of_Sam:这是问题中“Web App project.json”部分的摘录。这是错误的做法。 dnx451 不应该在那里,因为 dnx451 与 netcoreapp1.0 不兼容。它只强制 nuget 将 dnx451 包安装到 netcoreapp1.0 中,仅此而已。
    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多