Michael
Michael

Reputation: 1010

C# .NET Core 2.2 - C# MongoDB.Driver - Exception

I have 4 projects:

A - .NET Core 2.2 project - reference to B and D (knows nothing about C)

B - .NET 4.7.2 Class Library - reference to C and D

C - .NET 4.7.2 Class Library - reference D and the the MongoDB C# Driver (Version 2.7.30) installed with NuGet.

D - .NET 4.7.2 Class Library - just a container for DTO classes

All projects do build and run.

===

B is a generic data repository.

C is a database context, which uses the MongoDB C# driver.

Both projects B and C work well without any errors, when I use them in "normal" .NET 4.7.2, also 4.5.2 projects.

===

The error:

When I make a call from the .NET Core project A to B which ends in C, I get an error at this point:

private IMongoCollection<T> _Collection;
public IMongoCollection<T> Collection
{
    get
    {
        if (_Collection == null)
        {
            //This is still ok!
            _Collection = _DataBase.GetCollection<T>("MyTableName");
        }
        return _Collection;
    }
}

public IEnumerable<T> All
{
    get
    {
           try
           {
               //Collection is NOT Null and was loaded from the DB
               return Collection.Find(new BsonDocument()).ToList();
           }
           catch (Exception ex)
           {
               //THE EXCEPTION APPEARS HERE                    
           }
           return null;
    }
}

The exception details are:

{System.TypeLoadException: Could not load type 'System.Runtime.Remoting.Messaging.CallContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. at MongoDB.Driver.Core.Events.EventContext.AsyncLocal1.get_Value() at MongoDB.Driver.Core.Events.EventContext.BeginOperation(Nullable1 operationId) at MongoDB.Driver.Core.Operations.FindCommandOperation1.Execute(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.FindOperation1.Execute(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.OperationExecutor.ExecuteReadOperation[TResult](IReadBinding binding, IReadOperation1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation1 operation, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition1 filter, FindOptions2 options, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.<>c__DisplayClass41_01.b__0(IClientSessionHandle session) at MongoDB.Driver.MongoCollectionImpl1.UsingImplicitSession[TResult](Func2 func, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.FindSync[TProjection](FilterDefinition1 filter, FindOptions2 options, CancellationToken cancellationToken) at MongoDB.Driver.FindFluent2.ToCursor(CancellationToken cancellationToken) at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken) ...}

(sorry for the full exception, but I thought it would help)

My question:

What can I do to solve this problem?

As I said earlier above - this seems to be a .NET Core problem, since the projects run without any error in other projects.

Upvotes: 0

Views: 1818

Answers (2)

user11109055
user11109055

Reputation:

In my case my Core standart library project has using normal library project even i have changed 4.5, 4.52, 4.61, 4.72 vs. but problem never ended until converting normal lib project to standart library project. I suggest convert to standart lib

Upvotes: 0

Michael
Michael

Reputation: 1010

After debugging and testing countless scenarios, I figured out, what was wrong.

Man, that was really hard to find out, because there was only one little hint here on SO that helped me.

So the story from beginning to end:

1.) I have a "normal" .NET 4.7.2 DLL, which hosts my custom MongoDB context.

2.) To do this, I installed the MongoDB Nuget package into this DLL.

3.) The package worked well and as expected when in collaboration with other "normal" .NET 4.7.2 DLLs. No problem. Very happy.

4.) When trying to use my 4.7.2 DLL with a .NET Core project, I get the exception in the start post. Shit.

5.) What you have to keep in mind: When you install a nuget package, it seems to install and references the DLLs, that targets the same (nearest?) framework. In my case target was 4.7.2, so NuGet installed 4.5 versions of the MongoDB.Driver and MongoDB.Driver.Core.

6.) What you actually need, when working with .NET Core, are NOT the 4.5 versions, but the NetStandard 1.5 versions of BOTH MongoDB.Driver and MongoDB.Driver.Core.

7.) So I downloaded the Nuget packages of MongoDB.Driver and MongoDB.Driver.Core manually. NuGet packages are just *.zip files, so I changed the extension and unpacked them.

8.) Inside of each package you find a lib folder with two folders: net45 and netstandard1.5

9.) I extracted the netstandard 1.5 versions of BOTH MongoDB.Driver and MongoDB.Driver.Core.

10.) I replaced the references of BOTH MongoDB.Driver and MongoDB.Driver.Core inside my .NET 4.7.2 DLL with the Netstandard 1.5 versions - BOOM! Working!

So everything in summary:

If (.NET Core + MongoDB)
{
   use MongoDB.Driver      Netstandard 1.5;
   use MongoDB.Driver.Core Netstandard 1.5;
}
If (.NET + MongoDB)
{
   use MongoDB.Driver      Net 4.5;
   use MongoDB.Driver.Core Net 4.5;
}

I really hope, my answer saves others hours or even days of pure confusion.

Upvotes: 2

Related Questions