首页 > 解决方案 > Exception when trying to get reference to MongoDB collections with .Net Core 3.0 (MongoDB.Driver 2.8.0)

问题描述

My Environment:

Microsoft.NETCore.Platforms (3.0.0-preview5.19224.8)

MongoDB.Driver (2.8.0)

enter image description here

My problem:

Before updating the version of "Microsoft.NETCore.Platforms" the following code worked perfectly:

//Collection of resales
public IMongoCollection<Revenda> CollRevendas;

public BaseRepository(IConfiguration config)
{
    try
    {
        // Location of the database, configured in the "appsettings.json" file
        var client = new MongoClient(config.GetConnectionString("Config.api.mstiDFE"));

        // Name of the database
        Database = client.GetDatabase("api_mstiDFE_II");

        // Get the reference for the collection "Resales"
        CollRevendas = Database.GetCollection<Revenda>("Revendas");

    }
    catch (System.Exception ex)
    {
        throw;
    }

}

After updating, when attempting to execute the "CollRevendas = Database.GetCollection("Revendas");" statement, the following exception is thrown:

{"The type initializer for 'MongoDB.Bson.Serialization.BsonClassMap' threw an exception."}

enter image description here

With the following stack trace:

System.TypeInitializationException: The type initializer for 'MongoDB.Bson.Serialization.BsonClassMap' threw an exception. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Reflection.IntrospectionExtensions.GetTypeInfo(Type type)
at MongoDB.Bson.Serialization.BsonClassMap..cctor()
--- End of inner exception stack trace ---
at MongoDB.Bson.Serialization.BsonClassMap.LookupClassMap(Type classType)
at MongoDB.Bson.Serialization.BsonClassMapSerializationProvider.GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Bson.Serialization.BsonSerializerRegistry.CreateSerializer(Type type)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at MongoDB.Bson.Serialization.BsonSerializerRegistry.GetSerializer[T]()
at MongoDB.Driver.MongoCollectionImpl`1..ctor(IMongoDatabase database, CollectionNamespace collectionNamespace, MongoCollectionSettings settings, ICluster cluster, IOperationExecutor operationExecutor)
at MongoDB.Driver.MongoDatabaseImpl.GetCollection[TDocument](String name, MongoCollectionSettings settings)
at api.mstiDFE.Infra.Repositories.BaseRepository..ctor(IConfiguration config) in C:\Users\Source\Workspace\api.mstiDFE\api.mstiDFE\Infra\Repositories\BaseRepository.cs:line 27

Unfortunately I can not downgrade the "Microsoft.NETCore.Platforms". So any hint will be very welcome.

标签: c#mongodb.net-core

解决方案


Version 2.8.1 of the MongoDB Driver for C # was released yesterday (05-15-19). Soon after asking this question, I obtained the information in the following link:

CSHARP-2595: Fix initialization on .NET Core 3.0 preview 4.

After upgrading to version 2.8.1, the issue was resolved.

So I'll leave the question here as it can serve other people with the same problem.


推荐阅读