c# - 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)
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.
解决方案
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.
推荐阅读
- apache - URL 重写 http > https AND domain.tld > www.domain.tld
- javascript - html2Canvas 下载很模糊,我怎样才能让它更清晰
- coldfusion - ColdFusion 通过 Java 做 OWASP esapi
- amazon-web-services - 在 golang 中订阅 SNS 主题和/或 SQS 队列?
- android - Android Studio 的奇怪行为,一些文件看起来与实际不同
- c# - Xamarin 自定义表视图标题
- python - ElementTree 解析 xml 文件 - 解析问题
- excel - Excel VBA:无法从 VBA 中的日期变量填写
- docker - 如何从 docker 容器在主机中运行 bash 脚本并获得结果
- python - 使用python将列值与数据框中的int进行比较