Browse Source

update samples

master
Savorboard 6 years ago
parent
commit
dae03f33b7
4 changed files with 17 additions and 14 deletions
  1. +8
    -8
      samples/Sample.Kafka.MySql/Controllers/ValuesController.cs
  2. +3
    -1
      samples/Sample.Kafka.MySql/Startup.cs
  3. +2
    -2
      samples/Sample.RabbitMQ.MongoDB/Controllers/ValuesController.cs
  4. +4
    -3
      samples/Sample.RabbitMQ.MySql/Controllers/ValuesController.cs

+ 8
- 8
samples/Sample.Kafka.MySql/Controllers/ValuesController.cs View File

@@ -1,8 +1,11 @@
using System; using System;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using Dapper; using Dapper;
using DotNetCore.CAP; using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
//using MySql.Data.MySqlClient;


namespace Sample.Kafka.MySql.Controllers namespace Sample.Kafka.MySql.Controllers
{ {
@@ -28,17 +31,14 @@ namespace Sample.Kafka.MySql.Controllers
public IActionResult AdonetWithTransaction() public IActionResult AdonetWithTransaction()
{ {
//NOTE: Add `IgnoreCommandTransaction=true;` to your connection string, see https://github.com/mysql-net/MySqlConnector/issues/474 //NOTE: Add `IgnoreCommandTransaction=true;` to your connection string, see https://github.com/mysql-net/MySqlConnector/issues/474
using (var connection = new MySqlConnection(Startup.ConnectionString))
using (var connection = new SqlConnection(Startup.ConnectionString))
{ {
using (var transaction = connection.BeginAndJoinToTransaction(_capBus, autoCommit: false))
using (var transaction = connection.BeginTransaction(_capBus, autoCommit: false))
{ {
//your business code //your business code
connection.Execute("insert into test(name) values('test')", transaction);
connection.Execute("insert into dbo.test1(tname) values('test');", transaction: (IDbTransaction)transaction.DbTransaction);


for (int i = 0; i < 5; i++)
{
_capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
}
_capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);


transaction.Commit(); transaction.Commit();
} }


+ 3
- 1
samples/Sample.Kafka.MySql/Startup.cs View File

@@ -6,13 +6,15 @@ namespace Sample.Kafka.MySql
public class Startup public class Startup
{ {
public const string ConnectionString = "Server=localhost;Database=testcap;UserId=root;Password=123123;"; public const string ConnectionString = "Server=localhost;Database=testcap;UserId=root;Password=123123;";
//public const string ConnectionString = "Server=(localdb)\\ProjectsV13;Integrated Security=SSPI;Database=testcap";


public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddCap(x => services.AddCap(x =>
{ {
x.UseMySql(ConnectionString); x.UseMySql(ConnectionString);
x.UseKafka("localhost:9092");
//x.UseSqlServer(ConnectionString);
x.UseKafka("192.168.10.110:9092");
x.UseDashboard(); x.UseDashboard();
}); });




+ 2
- 2
samples/Sample.RabbitMQ.MongoDB/Controllers/ValuesController.cs View File

@@ -35,7 +35,7 @@ namespace Sample.RabbitMQ.MongoDB.Controllers
//var mycollection = _client.GetDatabase("test").GetCollection<BsonDocument>("test.collection"); //var mycollection = _client.GetDatabase("test").GetCollection<BsonDocument>("test.collection");
//mycollection.InsertOne(new BsonDocument { { "test", "test" } }); //mycollection.InsertOne(new BsonDocument { { "test", "test" } });


using (var session = _client.StartAndJoinToTransaction(_capBus, autoCommit: false))
using (var session = _client.StartTransaction(_capBus, autoCommit: false))
{ {
var collection = _client.GetDatabase("test").GetCollection<BsonDocument>("test.collection"); var collection = _client.GetDatabase("test").GetCollection<BsonDocument>("test.collection");
collection.InsertOne(session, new BsonDocument { { "hello", "world" } }); collection.InsertOne(session, new BsonDocument { { "hello", "world" } });
@@ -55,7 +55,7 @@ namespace Sample.RabbitMQ.MongoDB.Controllers
//var mycollection = _client.GetDatabase("test").GetCollection<BsonDocument>("test.collection"); //var mycollection = _client.GetDatabase("test").GetCollection<BsonDocument>("test.collection");
//mycollection.InsertOne(new BsonDocument { { "test", "test" } }); //mycollection.InsertOne(new BsonDocument { { "test", "test" } });


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


+ 4
- 3
samples/Sample.RabbitMQ.MySql/Controllers/ValuesController.cs View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Data;
using Dapper; using Dapper;
using DotNetCore.CAP; using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -30,10 +31,10 @@ namespace Sample.RabbitMQ.MySql.Controllers
//NOTE: Add `IgnoreCommandTransaction=true;` to your connection string, see https://github.com/mysql-net/MySqlConnector/issues/474 //NOTE: Add `IgnoreCommandTransaction=true;` to your connection string, see https://github.com/mysql-net/MySqlConnector/issues/474
using (var connection = new MySqlConnection(AppDbContext.ConnectionString)) using (var connection = new MySqlConnection(AppDbContext.ConnectionString))
{ {
using (var transaction = connection.BeginAndJoinToTransaction(_capBus, autoCommit: false))
using (var transaction = connection.BeginTransaction(_capBus, autoCommit: false))
{ {
//your business code //your business code
connection.Execute("insert into test(name) values('test')", transaction);
connection.Execute("insert into test(name) values('test')", transaction: (IDbTransaction)transaction.DbTransaction);


for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
@@ -50,7 +51,7 @@ namespace Sample.RabbitMQ.MySql.Controllers
[Route("~/ef/transaction")] [Route("~/ef/transaction")]
public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext) public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
{ {
using (var trans = dbContext.Database.BeginAndJoinToTransaction(_capBus, autoCommit: false))
using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: false))
{ {
dbContext.Persons.Add(new Person() { Name = "ef.transaction" }); dbContext.Persons.Add(new Person() { Name = "ef.transaction" });




Loading…
Cancel
Save