【发布时间】:2019-04-10 16:03:48
【问题描述】:
我有 4 个项目:
A - .NET Core 2.2 项目 - 对 B 和 D 的引用(对 C 一无所知)
B - .NET 4.7.2 类库 - 对 C 和 D 的引用
C - .NET 4.7.2 类库 - 参考 D 和随 NuGet 一起安装的 MongoDB C# 驱动程序(版本 2.7.30)。
D - .NET 4.7.2 类库 - 只是 DTO 类的容器
所有项目都会构建和运行。
===
B 是通用数据存储库。
C 是一个数据库上下文,它使用 MongoDB C# 驱动程序。
当我在“普通”.NET 4.7.2 和 4.5.2 项目中使用它们时,项目 B 和 C 都运行良好,没有任何错误。
===
错误:
当我从 .NET Core 项目 A 调用以 C 结尾的 B 时,此时出现错误:
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;
}
}
异常详情如下:
{System.TypeLoadException:无法从程序集“mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089”加载类型“System.Runtime.Remoting.Messaging.CallContext”。
在 MongoDB.Driver.Core.Events.EventContext.AsyncLocal1.get_Value()
at MongoDB.Driver.Core.Events.EventContext.BeginOperation(Nullable1 operationId)
在 MongoDB.Driver.Core.Operations.FindCommandOperation1.Execute(IReadBinding binding, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.FindOperation1.Execute(IReadBinding 绑定,CancellationToken cancelToken)
在 MongoDB.Driver.OperationExecutor.ExecuteReadOperation[TResult](IReadBinding 绑定,IReadOperation1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl1.ExecuteReadOperation[TResult](IClientSessionHandle 会话,IReadOperation1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl1.ExecuteReadOperation[TResult](IClientSessionHandle 会话,IReadOperation1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl1.FindSync [TProjection](IClientSessionHandle session, FilterDefinition1 filter, FindOptions2 options, CancellationToken cancelToken)
在 MongoDB.Driver.MongoCollectionImpl1.<>c__DisplayClass41_01.b__0(IClientSessionHandle 会话)
在 MongoDB.Driver.MongoCollectionImpl1.UsingImplicitSession[TResult](Func2 func, CancellationToken cancelToken)
在 MongoDB.Driver.MongoCollectionImpl1.FindSync[TProjection](FilterDefinition1 过滤器,FindOptions2 options, CancellationToken cancellationToken)
at MongoDB.Driver.FindFluent2.ToCursor(CancellationToken cancelToken)
在 MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 源,CancellationToken cancelToken)
...}
(对不起,我认为这会有所帮助)
我的问题:
我可以做些什么来解决这个问题?
正如我之前所说 - 这似乎是一个 .NET Core 问题,因为这些项目在其他项目中运行时没有任何错误。
【问题讨论】:
标签: c# mongodb .net-core asp.net-core-webapi