Could not load type 'System.Runtime.Remoting.Messaging.CallContext' when calling into Mongo's C# Driver on .NET Core

2

I'm upgrading my web application from the .NET Framework version of ASP.NET MVC to ASP.NET Core. I've worked through all of the C# and Razor build errors, and now I'm trying to run the application for the first time. When I make any calls into MongoDB's C# driver:

public static Analytics Find(Enums.Platform platform, DateTime date) {
            var builder = Filter;
            var filters = builder.Eq("Platform", platform) & builder.Eq("Date", date);
            return Collection().Find(filters).FirstOrDefault();// Exception thrown here.
        }

It errors:

System.TypeLoadException HResult=0x80131522 Message=Could not load type 'System.Runtime.Remoting.Messaging.CallContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Source=MongoDB.Driver.Core
StackTrace: 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.FindFluent
2.ToCursor(CancellationToken cancellationToken) at MongoDB.Driver.IAsyncCursorSourceExtensions.FirstOrDefault[TDocument](IAsyncCursorSource1 source, CancellationToken cancellationToken) at MongoDB.Driver.IFindFluentExtensions.FirstOrDefault[TDocument,TProjection](IFindFluent2 find, CancellationToken cancellationToken) at Housters.Data.DataAccess.AnalyticsData.Find(Platform platform, DateTime date) in C:\Housters\Data\DataAccess\Common\AnalyticsData.cs:line 22 at Housters.Business.Services.Common.AnalyticsService.Get(Platform platform, DateTime date) in C:\Housters\Business\Services\Common\AnalyticsService.cs:line 31 at Housters.Business.Services.Common.AnalyticsService.Log(Platform platform, String deviceId, AnalyticsEventType type, Boolean onlyLogIfSignedUp) in C:\Housters\Business\Services\Common\AnalyticsService.cs:line 19 at Housters.Web.Controllers.HomeController.Index() in C:\Housters\Web\Controllers\HomeController.cs:line 8 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()

I'm confused as to what's going on because MongoDB supports .NET Core, and yet System.Runtime.Remoting.Messaging.CallContext is a .NET Framework method, .NET Core doesn't support it. I saw another SO thread where the OP was facing this issue, however his problem was that he wasn't using the latest MongoDB C# Driver. I'm using the latest driver, 2.8.0, within a .NET Standard (2.0.3) class library.

enter image description here

c#
mongodb
asp.net-core
.net-core
mongodb-.net-driver
asked on Stack Overflow May 2, 2019 by Justin

1 Answer

2

Nupkg is essentially a zip file with multiple versions of the DLL and some metadata.

In case of Mongo driver, there are DLLs for two target environments

  • .NET Framework 4.5.2
  • .NET Standard 1.5

When installing a NuGet package, the dll matching your project is selected. I assume you are still referencing .NET framework DLL, despite using a NuGet package that supports .NET Standard.

Try re-installing your NuGet package. Verify that DLL that lands in your bin for matches .NET Standard version.

answered on Stack Overflow May 2, 2019 by Lesiak • edited May 2, 2019 by Lesiak

User contributions licensed under CC BY-SA 3.0