My Environment:
Microsoft.NETCore.Platforms (3.0.0-preview5.19224.8)
MongoDB.Driver (2.8.0)
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."}
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.
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.
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.
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.
The same issue was happening in 2.9.0-beta1 also but updating to 2.9.0-beta2 fixed the issue for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With