public async Task <bool> InsertAsync <T>(T data, byte[] bytes, string uniqueId = null, bool needComplate = true) where T : IEventBase <K> { var mEvent = new MongoEvent <K>(); mEvent.StateId = data.StateId; mEvent.Version = data.Version; mEvent.TypeCode = data.TypeCode; mEvent.Data = bytes; if (string.IsNullOrEmpty(data.Id)) { mEvent.Id = ObjectId.GenerateNewId().ToString(); data.Id = mEvent.Id; } else { mEvent.Id = data.Id; } mEvent.IsComplete = !needComplate; if (string.IsNullOrEmpty(uniqueId)) { mEvent.MsgId = mEvent.Id; } else { mEvent.MsgId = uniqueId; } try { await GetCollection <MongoEvent <K> >(mongoAttr.EventDataBase, mongoAttr.GetCollection(data.Timestamp).Name).InsertOneAsync(mEvent); return(true); } catch (MongoWriteException ex) { if (ex.WriteError.Category != ServerErrorCategory.DuplicateKey) { throw ex; } else { Global.IocProvider.GetService <ILoggerFactory>().CreateLogger("MongoEventStorage").LogError(ex, $"事件重复插入,Event:{Newtonsoft.Json.JsonConvert.SerializeObject(data)}"); } } return(false); }
public async Task <bool> SaveAsync <T>(T data, byte[] bytes, string uniqueId = null) where T : IEventBase <K> { var mEvent = new MongoEvent <K>(); mEvent.StateId = data.StateId; mEvent.Version = data.Version; mEvent.TypeCode = data.TypeCode; mEvent.Data = bytes; if (string.IsNullOrEmpty(data.Id)) { mEvent.Id = ObjectId.GenerateNewId().ToString(); data.Id = mEvent.Id; } else { mEvent.Id = data.Id; } if (string.IsNullOrEmpty(uniqueId)) { mEvent.MsgId = mEvent.Id; } else { mEvent.MsgId = uniqueId; } try { await mongoStorage.GetCollection <MongoEvent <K> >(mongoAttr.EventDataBase, mongoAttr.GetCollection(mongoStorage, mongoStorage.Config.SysStartTime, data.Timestamp).Name).InsertOneAsync(mEvent); return(true); } catch (MongoWriteException ex) { if (ex.WriteError.Category != ServerErrorCategory.DuplicateKey) { throw ex; } else { logger.LogError(ex, $"事件重复插入,Event:{Newtonsoft.Json.JsonConvert.SerializeObject(data)}"); } } return(false); }