Pylyp Lebediev
Pylyp Lebediev

Reputation: 2131

MongoDB driver throws file not found exception

I am migrating ASP.NET Core 1 application from RC 1 to RC 2. An application has web application itself (netcoreapp1.0 targeted) and two class libraries (.NET Core) for models and data access layer (netstandard1.5 targeted). These class libraries use MongoDB driver 2.2.4. The application restores dependencies and compiles without any errors. However, during the initialization of the MongoDB client FileNotFound exception is thrown.

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

What can cause this issue? Is it possible currently use legacy MongoDB driver for ASP.NET Core RC 2 application without waiting for driver for core version of .NET at all? In addition, there were no issues with ASP.NET Core RC 1 application.

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%" ]
  }
}

Models 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" ]
    }
  }
}

DataAccess 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" ]
    }
  }
}

During the migration I was following official documentation and then I have tried to move existing source code to sample ASP.NET Core 1 RC 2 web application created by Visual Studio 2015.

UPDATE

After moving to net451 framework the project files look the following

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%" ]
  }
}

Models

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

Upvotes: 3

Views: 2335

Answers (1)

Tseng
Tseng

Reputation: 64229

You can't target MongoDB drivers in a netappcore1.0 application! And please don't use dnx451 in netappcore1.0 imports. .NET 4.5.1 assemblies don't work with .NET Core applications! Only .NET Core assemblies (win8 and win8.1/uwp are based on .NET, dnx451 is not). It essentially says: "Install MongoDb in .NET Core Application, even if it doesn't target/support it".

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

This will force nuget to restore dnx451/net451 packages, even if they are not supported. The imports section is only there as a workaround to force restore of packages that are compatible with .NET core but do not target netstandard1.5 yet (like packages written for UWP, Win8 or Win8.1).

If you want to use MongoDB drivers, you have to target net451, not netcoreapp1.0 or netstandard1.5. This applies for all of your projects and subprojects that reference MongoDB.

Upvotes: 3

Related Questions