|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Data;
- using System.Threading.Tasks;
- using Dapper;
- using DotNetCore.CAP;
- using Microsoft.AspNetCore.Mvc;
- using MySql.Data.MySqlClient;
-
- namespace Sample.RabbitMQ.MySql.Controllers
- {
- [Route("api/[controller]")]
- public class ValuesController : Controller
- {
- private readonly ICapPublisher _capBus;
-
- public ValuesController(ICapPublisher capPublisher)
- {
- _capBus = capPublisher;
- }
-
- [Route("~/without/transaction")]
- public async Task<IActionResult> WithoutTransaction()
- {
- await _capBus.PublishAsync("sample.rabbitmq.mysql", DateTime.Now);
-
- return Ok();
- }
-
- [Route("~/adonet/transaction")]
- public IActionResult AdonetWithTransaction()
- {
- using (var connection = new MySqlConnection(AppDbContext.ConnectionString))
- {
- using (var transaction = connection.BeginTransaction(_capBus, autoCommit: false))
- {
- //your business code
- connection.Execute("insert into test(name) values('test')", transaction: (IDbTransaction)transaction.DbTransaction);
-
- for (int i = 0; i < 5; i++)
- {
- _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
- }
-
- transaction.Commit();
- }
- }
-
- return Ok();
- }
-
- [Route("~/ef/transaction")]
- public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
- {
- using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: false))
- {
- dbContext.Persons.Add(new Person() { Name = "ef.transaction" });
-
- for (int i = 0; i < 5; i++)
- {
- _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
- }
-
- dbContext.SaveChanges();
-
- trans.Commit();
- }
- return Ok();
- }
-
- [NonAction]
- [CapSubscribe("#.rabbitmq.mysql")]
- public void Subscriber(DateTime time)
- {
- Console.WriteLine($@"{DateTime.Now}, Subscriber invoked, Sent time:{time}");
- }
- }
- }
|