public async Task Handle(StorageOps message, IMessageHandlerContext context) { using (TransactionScope scope = new TransactionScope()) { log.Info($"Received StorageOps"); var transactionStarted = new DistributedTransactionStarted() { OrderId = context.MessageId }; await context.Publish(transactionStarted); log.Info($"DistributedTransaction, Id = {context.MessageId} - Started"); using (var sqlContext = new ToDoSqlContext()) { ToDoModel todoModel = new ToDoModel() { Content = $"Mongo Actor Document with Id: {2}", Title = $"MongoActor : {2}" }; sqlContext.ToDos.Add(todoModel); sqlContext.SaveChanges(); } using (var sqlContext = new ToDoSqlContext2()) { ToDoModel todoModel = new ToDoModel() { Content = $"Mongo Actor Document with Id: {2}", Title = $"MongoActor : {2}" }; sqlContext.ToDos.Add(todoModel); sqlContext.SaveChanges(); } throw new Exception("Mongo Exception"); var transactionEnded = new DistributedTransactionEnded() { OrderId = context.MessageId, }; await context.Publish(transactionEnded); log.Info($"DistributedTransaction, Id = {context.MessageId} - Ended"); scope.Complete(); } }
public async Task Handle(StorageOps message, IMessageHandlerContext context) { var transactionStarted = new DistributedTransactionStarted() { OrderId = context.MessageId }; await context.Publish(transactionStarted); log.Info($"DistributedTransaction, Id = {context.MessageId} - Started"); using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { log.Info($"Received StorageOps"); // Sql1 using (var sqlContext = new ToDoSqlContext()) { ToDoModel todoModel = new ToDoModel() { Content = $"Mongo Actor Document with Id: {2}", Title = $"MongoActor : {2}" }; sqlContext.ToDos.Add(todoModel); sqlContext.SaveChanges(); } // Mongo var _todoMongoContext = new TodoMongoContext(new MongoDbConfig()); var _todoMongoRepository = new TodoMongoRepository(_todoMongoContext); var id = await _todoMongoRepository.GetNextId(); // Bad Spot-1 Todo todo = new Todo() { Id = id, Content = $"Mongo Actor Document with Id: {id}", Title = $"MongoActor : {id}" }; await _todoMongoRepository.Create(todo); // Bad Spot-2 // sql2 using (var sqlContext = new ToDoSqlContext2()) { ToDoModel todoModel = new ToDoModel() { Content = $"Mongo Actor Document with Id: {2}", Title = $"MongoActor : {2}" }; sqlContext.ToDos.Add(todoModel); sqlContext.SaveChanges(); } throw new Exception("Mongo Exception"); var transactionEnded = new DistributedTransactionEnded() { OrderId = context.MessageId, }; await context.Publish(transactionEnded); log.Info($"DistributedTransaction, Id = {context.MessageId} - Ended"); scope.Complete(); } }