MongoDB驱动程序抛出文件没有找到异常

问题描述:

我正在将ASP.NET Core 1应用程序从RC 1迁移到RC 2.应用程序具有Web应用程序本身(netcoreapp1.0目标)和两个类库(.NET Core)数据访问层(netstandard1.5定位)。这些类库使用MongoDB驱动程序2.2.4。该应用程序恢复依赖关系并编译时没有任何错误。但是,在MongoDB客户端初始化期间FileNotFound异常被抛出。MongoDB驱动程序抛出文件没有找到异常

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 

enter image description here

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

Web App的project.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" ] 
    } 
    } 
} 

数据访问project.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然后我曾尝试将现有源代码移至示例ASP.NET Core 1 RC 2 Web应用程序Visual Studio创建2015年

UPDATE

移动到net451框架项目文件看看以下

Web App的

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

DataAcess后

{ 
    "version": "1.0.0-*", 

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

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

您不能在netappcore1.0应用程序中将MongoDB驱动程序作为目标!请不要在netappcore1.0进口中使用dnx451。 .NET 4.5.1程序集不能与.NET Core应用程序一起使用!只有.NET核心程序集(win8和win8.1/uwp基于.NET,dnx451是而不是)。它基本上说:“在.NET核心应用程序中安装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的项目和子项目。

+0

非常感谢您解决此问题! –

+0

在答案中的例子是否是正确的做法或不正确? –

+0

@Son_of_Sam:这是问题中“Web App project.json”部分的摘录。这是做错的方式。 'dnx451'不应该在那里,因为dnx451与netcoreapp1.0不兼容。它只会强制nuget将dnx451软件包安装到'netcoreapp1.0'中,仅此而已。 – Tseng