在 .NET Core 上调用 Mongo 的 C# 驱动程序时无法加载类型 'System.Runtime.Remoting.Messaging.CallContext'

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

我正在将我的 Web 应用程序从 ASP.NET MVC 的 .NET Framework 版本升级到 ASP.NET Core。我已经解决了所有 C# 和 Razor 构建错误,现在我第一次尝试 运行 应用程序。当我调用 MongoDB 的 C# 驱动程序时:

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.
        }

错误:

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)<br> at MongoDB.Driver.FindFluent2.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()

我对发生的事情感到困惑,因为 MongoDB 支持 .NET Core,但 System.Runtime.Remoting.Messaging.CallContext 是一种 .NET Framework 方法,.NET Core 不支持它。我看到 OP 面临这个问题,但他的问题是他没有使用最新的 MongoDB C# 驱动程序。我在 .NET Standard (2.0.3) class 库中使用最新的驱动程序 2.8.0。

Nupkg 本质上是一个包含多个版本的 DLL 和一些元数据的 zip 文件。

如果是 Mongo 驱动程序,则有两个目标环境的 DLL

  • .NET Framework 4.5.2
  • .NET 标准 1.5

安装 NuGet 包时,选择了与您的项目匹配的 dll。尽管使用了支持 .NET Standard 的 NuGet 包,但我假设您仍在引用 .NET Framework DLL。

尝试重新安装您的 NuGet 包。 验证进入您的 bin 的 DLL 是否匹配 .NET Standard 版本。