C#.NET Core 2.2-C#MongoDB.Driver-异常

时间:2019-02-26 05:07:14

标签: c# mongodb .net-core asp.net-core-webapi

我有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,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'中加载类型'System.Runtime.Remoting.Messaging.CallContext'。    位于MongoDB.Driver.Core.Events.EventContext.AsyncLocal 1.get_Value() at MongoDB.Driver.Core.Events.EventContext.BeginOperation(Nullable 1 operationId)    位于MongoDB.Driver.Core.Operations.FindCommandOperation 1.Execute(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.FindOperation 1.Execute(IReadBinding绑定,CancellationToken cancelToken)上    at MongoDB.Driver.OperationExecutor.ExecuteReadOperation [TResult](IReadBinding绑定,IReadOperation 1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl 1.ExecuteReadOperation [TResult](IClientSessionHandle会话,IReadOperation 1 operation, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl 1.ExecuteReadOperation [TResult](IClientSessionHandle会话,IReadOperation {{ 1}}}。FindSync [TProjection](IClientSessionHandle会话,FilterDefinition 1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl 2个选项,CancellationToken cancelToken)    在MongoDB.Driver.MongoCollectionImpl 1 filter, FindOptions 1.b__0(IClientSessionHandle会话)上    位于MongoDB.Driver.MongoCollectionImpl 1.<>c__DisplayClass41_0 2 func,CancelationToken cancelToken处)    在MongoDB.Driver.MongoCollectionImpl 1.UsingImplicitSession[TResult](Func 1过滤器上,FindOptions 1.FindSync[TProjection](FilterDefinition 2.ToCursor(CancellationToken cancelledToken)    在MongoDB.Driver.IAsyncCursorSourceExtensions.ToList [TDocument](IAsyncCursorSource`1源,CancellationToken cancelledToken)处    ...}

(对不起,我很抱歉,但我认为这样会有所帮助)

我的问题:

我该怎么做才能解决此问题?

正如我在前面所说的-这似乎是.NET Core问题,因为这些项目在其他项目中运行时没有任何错误。

2 个答案:

答案 0 :(得分:0)

在调试和测试了无数种情况后,我发现了什么地方出了错。

伙计,这真的很难被发现,因为SO上只有一个小提示对我有所帮助。

所以故事从头到尾都是

1。)我有一个“常规” .NET 4.7.2 DLL,用于托管我的自定义MongoDB上下文。

2。)为此,我将MongoDB Nuget软件包安装到此DLL中。

3。)与其他“正常” .NET 4.7.2 DLL协作时,该程序包运行良好且符合预期。没问题。很高兴。

4。)尝试将4.7.2 DLL与.NET Core项目一起使用时,在开始文章中遇到了异常。妈的。

5。)要记住的是:安装nuget程序包时,似乎安装并引用了以相同(最近的)框架为目标的DLL。在我的案例中,目标是4.7.2,因此NuGet安装了4.5版本的MongoDB.Driver和MongoDB.Driver.Core。

6。)使用.NET Core时,您真正需要的不是4.5版本,而是MongoDB.Driver和MongoDB.Driver.Core的NetStandard 1.5版本。

7。)因此,我手动下载了MongoDB.Driver和MongoDB.Driver.Core的Nuget软件包。 NuGet软件包只是* .zip文件,因此我更改了扩展名并将其解压缩。

8。)在每个软件包中,您都找到一个带有两个文件夹的lib文件夹:net45和netstandard1.5

9。)我提取了MongoDB.Driver和MongoDB.Driver.Core的netstandard 1.5版本。

10。)我用Netstandard 1.5版本-BOOM替换了.NET 4.7.2 DLL中的BOTH MongoDB.Driver和MongoDB.Driver.Core引用。工作!

所有内容总结如下:

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

我真的希望,我的回答可以使其他人节省数小时甚至数天的纯粹困惑。

答案 1 :(得分:0)

就我而言,即使我将4.5、4.52、4.61、4.62与4.5.4.5、4.52、4.61、4.72进行了更改,我的Core Standart库项目也已使用普通库项目,但是直到将普通lib项目转换为Standart库项目,问题才得以解决。 我建议转换为standart lib

相关问题