Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Publisher Api #188

Closed
yang-xiaodong opened this issue Aug 29, 2018 · 0 comments

Comments

@yang-xiaodong
Copy link
Member

commented Aug 29, 2018

In versions 2.3+, we made some breaking changes for the publisher's API. When using a publishing method with a transaction, you need to adopt a new API method:

PostgreSql and MySql

This is a sample for mysql:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly ICapPublisher _capBus;

    public ValuesController(ICapPublisher capPublisher)
    {
        _capBus = capPublisher;
    }

    [Route("~/adonet/transaction")]
    public IActionResult AdonetWithTransaction()
    {
        using (var connection = new MySqlConnection(""))
        {
            using (var transaction = connection.BeginTransaction(_capBus, autoCommit: false))
            {
                //your business code sample
                connection.Execute("insert into test(name) values('test')", transaction: (IDbTransaction)transaction.DbTransaction);
                
                _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);                               

                transaction.Commit();
            }
        }

        return Ok();
    }
}

In MySql and Postgresql the connection.BeginTransaction extension method will return the ICapTransaction interface, you need to use this interface to commit the transaction. The ICapTransaction interface contains a DbTransaction property, which represents a transaction of the current database, which is an Object type that you have cast to IDBTransaction to use.

SqlServer and MongoDb

This is a sample for SqlServer:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly ICapPublisher _capBus;

    public ValuesController(ICapPublisher capPublisher)
    {
        _capBus = capPublisher;
    }

    [Route("~/adonet/transaction")]
    public IActionResult AdonetWithTransaction()
    {
        using (var connection = new SqlConnection(""))
        {
            using (var transaction = connection.BeginTransaction(_capBus, autoCommit: false))
            {
                //your business code sample
                connection.Execute("insert into test(name) values('test')", transaction);
                
                _capBus.Publish("sample.rabbitmq.sqlserver", DateTime.Now);                               

                transaction.Commit();
            }
        }

        return Ok();
    }
}

In SqlServer the connection.BeginTransaction extension method will return the IDbTransaction, you can use it directly and allow you to submit transactions using IDbTransaction.

In MongoDb,it is similar to SqlServer, the connection.BeginTransaction extension method will return the IClientSessionHandle, It is a transaction context object in MongoDB that you can use to commit transactions.

@dotnetcore dotnetcore locked and limited conversation to collaborators Aug 29, 2018

@yang-xiaodong yang-xiaodong added this to the 2.3.0 milestone Aug 29, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
1 participant
You can’t perform that action at this time.