I created a testing app in .NET Core 7. This is my development environment
OS: win10 x64
.NET: 7.0
TARGET APP:WIN x64
MONGODB SERVER VERSION:3.4.2
MONGODB DRIVER VERSION:2.13.3
This is my DataObject class:
public class MongoDbObject
{
[JsonConstructor]
public MongoDbObject() { }
public ObjectId _id { get; set; }
public string Product { get; set; }
public string Production_Phase { get; set; }
public string TestLine { get; set; }
public string SN { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime Test_Datetime { get; set; }
public string Station { get; set; }
public string Test_Result { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime Upload_Datetime { get; set; }
public string FileName { get; set; }
public string[] Files { get; set; }
public string Equipment_NO { get; set; }
public string Slot { get; set; }
public string Fail_Item { get; set; }
public TestItemObject[] TestItems { get; set; }
}
public class TestItemObject
{
public TestItemObject() { }
public string TestItem { get; set; }
public float Usl { get; set; }
public float Lsl { get; set; }
public float TestValue { get; set; }
public bool Result { get; set; }
}
This is my testing property:
public async static Task DownloadLogfileByTestdate_StationAsync(DateTime startDate,DateTime endDate,string station)
{
try
{
ProcessLogfile.config = config;
var client = new MongoClient(config.DataBaseConfig.db_conn_string);
var db = client.GetDatabase(config.DataBaseConfig.db_name);
var collections = db.GetCollection<MongoDbObject>(config.DataBaseConfig.collection);
Console.WriteLine("数据库建立连接并打开连接...");
List<MongoDbObject>? items = new();
if(config.DataBaseConfig.only_online_data)
items = collections.Find(x =>x.SN.Length>config.DataBaseConfig.filter_sn_length && x.FileName.ToUpper().Contains("ONLINE") && x.SN!=string.Empty && x.Test_Datetime > startDate && x.Test_Datetime < endDate && x.Station.ToUpper().Contains(station.ToUpper().Trim())).ToList();
else
items = collections.Find(x => x.SN != string.Empty && x.SN.Length > config.DataBaseConfig.filter_sn_length && x.Test_Datetime > startDate && x.Test_Datetime < endDate && x.Station.ToUpper().Contains(station.ToUpper().Trim())).ToList();
if (items.Count > 0)
{
Console.WriteLine($"查找到符合条件的数据:\t{items.Count} 条");
await ProcessLogfile.SaveToLocalFileAsync(items.ToArray(), filePath);
}
else
{
Console.Error.WriteLine("No valid data be found!");
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
}
}
It works in debug or release mode, but there is a exception when published to an AOT app.
This is the exception:
No suitable constructor found for serializer type: 'MongoDB.Bson.Serialization.Serializers.DateTimeSerializer'.
at MongoDB.Bson.Serialization.BsonSerializationProviderBase.CreateSerializer(Type, IBsonSerializerRegistry) + 0x1a8
at MongoDB.Bson.Serialization.PrimitiveSerializationProvider.GetSerializer(Type, IBsonSerializerRegistry) + 0x92 at MongoDB.Bson.Serialization.BsonSerializerRegistry.CreateSerializer(Type type) + 0x91 at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey, Func2) + 0x82 at MongoDB.Bson.Serialization.BsonSerializerRegistry.GetSerializer(Type) + 0x5e at MongoDB.Bson.Serialization.BsonMemberMap.GetSerializer() + 0x176 at MongoDB.Bson.Serialization.Attributes.BsonSerializationOptionsAttribute.Apply(BsonMemberMap) + 0x16 at MongoDB.Bson.Serialization.Conventions.AttributeConventionPack.AttributeConvention.Apply(BsonMemberMap) + 0x16d at MongoDB.Bson.Serialization.Conventions.ConventionRunner.Apply(BsonClassMap) + 0x146 at MongoDB.Bson.Serialization.BsonClassMap.AutoMapClass() + 0x33 at MongoDB.Bson.Serialization.BsonClassMap.LookupClassMap(Type) + 0x18c at MongoDB.Bson.Serialization.BsonClassMapSerializationProvider.GetSerializer(Type, IBsonSerializerRegistry) + 0xdb at MongoDB.Bson.Serialization.BsonSerializerRegistry.CreateSerializer(Type type) + 0x91 at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey, Func2) + 0x82 at MongoDB.Bson.Serialization.BsonSerializerRegistry.GetSerializer(Type) + 0x5e at MongoDB.Bson.Serialization.BsonSerializerRegistry.GetSerializerT + 0x25 at MongoDB.Driver.MongoCollectionImpl`1..ctor(IMongoDatabase, CollectionNamespace, MongoCollectionSettings, ICluster, IOperationExecutor) + 0x6d at MongoDB.Driver.MongoDatabaseImpl.GetCollection[TDocument](String, MongoCollectionSettings) + 0xa5 at CpkMongoDbDownload.MongoDbController.<DownloadLogfileByTestdate_StationAsync>d__4.MoveNext() + 0x158
How to solve this issue?