[CSHARP-2393] Driver is not compatible with .NET 4.7.1 Created: 23/Sep/18  Updated: 08/Mar/19  Resolved: 08/Mar/19

Status: Closed
Project: C# Driver
Component/s: Build
Affects Version/s: 2.7.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Maxime Rossini Assignee: Robert Stam
Resolution: Duplicate Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10, Visual Studio 2017 15.8.5, .NET framework 4.7.1


Issue Links:
Depends
depends on CSHARP-2174 Use a single solution and set of proj... Closed
depends on CSHARP-2400 Build Nuget packages from .csproj fil... Closed
is depended on by DOCS-11995 C# and .NET Driver Compatibility upda... Closed
Duplicate
duplicates CSHARP-2033 Update dependency on System.Runtime.I... Closed
Related
is related to CSHARP-2033 Update dependency on System.Runtime.I... Closed
Case:

 Description   

I created a simple console app running under .NET 4.7.1, added the MongoDB.Driver nuget package with highest versions dependencies. I end up with a very simple project with the following files:

  • Program.cs

using MongoDB.Bson;
using MongoDB.Driver;
using System;
 
namespace ConsoleApp1
{
    internal class Program
    {
        private const string connectionString = "xxxxxx";
        private const string databaseName = "xxxxxx";
        private const string collectionName = "test";
 
        private static void Main(string[] args)
        {
            var mongoUrl = new MongoUrl(connectionString);
            var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);
            var mongoClient = new MongoClient(mongoClientSettings);
            IMongoDatabase database = mongoClient.GetDatabase(databaseName);
 
            long count = database.GetCollection<BsonDocument>(collectionName).CountDocuments(FilterDefinition<BsonDocument>.Empty);
            Console.WriteLine(count);
            Console.ReadLine();
        }
    }
}

* packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="DnsClient" version="1.2.0" targetFramework="net471" />
  <package id="MongoDB.Bson" version="2.7.0" targetFramework="net471" />
  <package id="MongoDB.Driver" version="2.7.0" targetFramework="net471" />
  <package id="MongoDB.Driver.Core" version="2.7.0" targetFramework="net471" />
  <package id="System.Buffers" version="4.5.0" targetFramework="net471" />
  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net471" />
</packages>

When I execute this program, I get the following error at runtime:

System.IO.FileLoadException: 'Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception de HRESULT : 0x80131040)'

The exact same program works when I target .NET framework 4.7 instead of 4.7.1 (and update the package.config accordingly).

It seems to be related to the fact that .NET 4.7.1 includes the DLL System.Runtime.InteropServices.RuntimeInformation in-box, whereas .NET 4.7 does not. Maybe the Mongo.Driver.Core package should not reference this nuget package when targeting .NET framework 4.7.1.



 Comments   
Comment by Antuan [ 23/Oct/18 ]

>>mrossini@maskott.com added a comment - Oct 02 2018 09:25:43 AM GMT+0000 

You are absolutely right!

I have encountered with the same problem with MongoDb Driver, System.Runtime.InteropServices.RuntimeInformation and .NET 4.7.1. And i also think, that strict dependence of target version 4.0.0.0 should be removed from Mongo nuget for frameworks 4.7.1 or higher.

Current situation is very painful for us, because our application depends on different components with 4.7.1, therefore we can not decrease framework version.

 

Anyone can tell (approximately) when the fix is planned to be released?

Thanks

 

Comment by Maxime Rossini [ 02/Oct/18 ]

Just to clarify:

  • MongoDB.Driver.Core's net45 target depends on nuget System.Runtime.InteropServices.RuntimeInformation (>= 4.0.0).
  • NuGet System.Runtime.InteropServices.RuntimeInformation v4.0.0 has assembly version 4.0.0.0
  • NuGet System.Runtime.InteropServices.RuntimeInformation v4.3.0 has assembly version 4.0.1.0
  • .NET framework < 4.7.1 doesn't include the System.Runtime.InteropServices.RuntimeInformation assembly
  • .NET framework >= 4.7.1 includes the System.Runtime.InteropServices.RuntimeInformation assembly, in version 4.0.2.0

To actually fix the bug without using a workaround, you need to add a TargetFramework in your solution with ID net471. This target will be used by applications built against .NET framework >= 4.7.1. You need to remove the dependency on the System.Runtime.InteropServices.RuntimeInformation NuGet for this target framework. To be fair, it's a lot easier to do that with a single solution for all target frameworks using the new .csproj format / packageReference syntax.

What I personnaly don't understand is the fact that the binding redirect from 0.0.0.0-4.0.2.0 to 4.0.2.0 breaks the application, since the same code without any binding redirect actually works. It must be related to the way the assembly linker loads dependent assemblies: one of the conflicting dependencies may be loaded without using the binding redirect instruction, or the binding redirect does not find the 4.7.1 GAC assemblies for some other reason.

Comment by Melvin Go [ 01/Oct/18 ]

Is there an ETA for a fix?

Comment by Robert Stam [ 01/Oct/18 ]

I must confess that I don't completely understand every nuance of what's going on here, but I have been able to reproduce this and have verified a workaround that appears to work.

Note that the C# driver depends on version 4.0.0 of System.Runtime.InteropServices.RuntimeInformation. Version 4.0.0 was the current version number at the time we took the dependency. Since then version 4.3.0 has come out. (Note: confusingly the Assembly Version for 4.3.0 is 4.0.1.0).

I was able to get the test program working when targetting .NET Framework 4.7.1 by:

  1. Explicitly adding a dependency to version 4.3.0 of System.Runtime.InteropServices.RuntimeInformation (just to override the 4.0.0 version implied by the driver dependencies)
  2. Removing the binding redirects in app.config that were added by the Nuget package manager when a dependency to MongoDB.Driver was added

Our current Nuget package has assets for two target frameworks: .NET Framework 4.5 and .NET Standard 1.5. We are not yet in a position to add more, but will be soon (when we finish updating our solution to target multiple frameworks from a single solution).

We also intend to update the dependency on System.Runtime.InteropServices.RuntimeInformation from 4.0.0 to 4.3.0. We have been delaying that because of uncertainty as to whether that would break any existing applications.

Comment by Maxime Rossini [ 30/Sep/18 ]

Interestingly, your workaround is effective.

Some elements of context:

  • Each of my project referencing the MongoDB.Driver has a dependency on the System.Runtime.InteropServices.RuntimeInformation nuget in version 4.3.0.
  • Each of my project referencing the MongoDB.Driver nuget is referencing the System.Runtime.InteropServices.RuntimeInformation DLL in the "packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45" folder.
  • Each of my project referencing the MongoDB.Driver has a binding redirect on System.Runtime.InteropServices.RuntimeInformation redirecting from version 0.0.0.0-4.0.2.0 to version 4.0.2.0. This binding redirect was added by the nuget package manager when reinstalling the MongoDB.Driver nuget.
  • The System.Runtime.InteropServices.RuntimeInformation DLL in the nuget packages folder is in version 4.0.1.0.
  • The System.Runtime.InteropServices.RuntimeInformation DLL in the "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\Facades" folder is in version 4.0.2.0.
  • At runtime, the code breaks with the following exception (the exception I mention in the ticket is the wrong one, but I can't edit my post):

Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

When removing the binding redirect completely, it works, but I don't understand why. Well, actually I don't understand why it doesn't work with the 4.0.2.0 binding redirect while this DLL is located in a folder accessible to the application ("C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\Facades").

Anyway this should be fixed by adding a new framework target in the MongoDB.Driver package (net471) which should not depend on the System.Runtime.InteropServices.RuntimeInformation nuget package, as this DLL is included in the framework starting with net471. By using the workaround, I am afraid any package update/reinstallation will break our application again by re-adding the binding redirect.

Comment by Melvin Go [ 27/Sep/18 ]

My team is running into a very similar issue with .NET 4.7.2 and we have a workaround.

These links contain the other info I think is relevant to the problem you're facing:

https://github.com/dotnet/standard/issues/567

https://docs.microsoft.com/en-us/visualstudio/releasenotes/vs2017-relnotes

 

Check if you have a binding redirect in your App.config. If so, try to remove the binding redirect and see if your code works at runtime. This is the workaround mentioned in the github link above.

I linked to the visual studio 15.8 release notes because that is the version of Visual Studio that officially supports .NET Framework 4.7.2. When I tried to the MongoDB.Driver nuget to a console app while using VS 15.7.4, the redirects were not added. After I upgraded to the latest version, VS 15.8.5, the redirects started happening.

I know you're using 4.7.1, but I mentioned 4.7.2 because that is the version our team was testing with and getting similar results to you.

 

I think the fix is to have the MongoDB.Driver nuget retargeted to .NET 4.7.2.

Generated at Wed Feb 07 21:42:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.