Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 409
Silvair L. Soares Avatar asked May 16 '19 11:05

Silvair L. Soares


People also ask

Can we use MongoDB with .NET core?

Designing a Document Model and Database Service within . NET Core. Before we start designing each of the RESTful API endpoints with . NET Core, we need to create and configure our MongoDB service and define the data model for our API.

Can Entity Framework work with MongoDB?

Short answer - no, it's for sure possible, but not reasonable. MongoDB is document database and not support any physical relations between collections. EF is a good fit for relational databases like SQL, MySQL, etc. MongoDB works faster with embedded documents.


2 Answers

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.

like image 50
Silvair L. Soares Avatar answered Oct 02 '22 21:10

Silvair L. Soares


The same issue was happening in 2.9.0-beta1 also but updating to 2.9.0-beta2 fixed the issue for me.

like image 45
kiran_Joy Avatar answered Oct 02 '22 22:10

kiran_Joy