Beispiel #1
0
        public IActionResult PublishNotAutoCommit()
        {
            //NOTE: before your test, your need to create database and collection at first
            //注意:MongoDB 不能在事务中创建数据库和集合,所以你需要单独创建它们,模拟一条记录插入则会自动创建
            //var mycollection = _client.GetDatabase("test").GetCollection<BsonDocument>("test.collection");
            //mycollection.InsertOne(new BsonDocument { { "test", "test" } });

            using (var session = _client.StartTransaction(_capBus, autoCommit: false))
            {
                var collection = _client.GetDatabase("test").GetCollection <BsonDocument>("test.collection");
                collection.InsertOne(session, new BsonDocument {
                    { "hello", "world" }
                });

                _capBus.Publish("sample.rabbitmq.mongodb", DateTime.Now);

                session.CommitTransaction();
            }
            return(Ok("新增记录到MongoDB。"));
        }