Browse Source

update samples

master
Savorboard 6 years ago
parent
commit
9ecc525433
5 changed files with 136 additions and 22 deletions
  1. +12
    -1
      samples/Sample.RabbitMQ.MySql/AppDbContext.cs
  2. +26
    -21
      samples/Sample.RabbitMQ.MySql/Controllers/ValuesController.cs
  3. +35
    -0
      samples/Sample.RabbitMQ.MySql/Migrations/20180821021736_init.Designer.cs
  4. +30
    -0
      samples/Sample.RabbitMQ.MySql/Migrations/20180821021736_init.cs
  5. +33
    -0
      samples/Sample.RabbitMQ.MySql/Migrations/AppDbContextModelSnapshot.cs

+ 12
- 1
samples/Sample.RabbitMQ.MySql/AppDbContext.cs View File

@@ -2,11 +2,22 @@

namespace Sample.RabbitMQ.MySql
{
public class Person
{
public int Id { get; set; }

public string Name { get; set; }
}

public class AppDbContext : DbContext
{
public const string ConnectionString = "Server=192.168.10.110;Database=testcap;UserId=root;Password=123123;IgnoreCommandTransaction=true;";

public DbSet<Person> Persons { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql("Server=192.168.10.110;Database=testcap;UserId=root;Password=123123;");
optionsBuilder.UseMySql(ConnectionString);
}
}
}

+ 26
- 21
samples/Sample.RabbitMQ.MySql/Controllers/ValuesController.cs View File

@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Dapper;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
@@ -9,64 +10,68 @@ namespace Sample.RabbitMQ.MySql.Controllers
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly AppDbContext _dbContext;
private readonly ICapPublisher _capBus;

public ValuesController(AppDbContext dbContext, ICapPublisher capPublisher)
public ValuesController(ICapPublisher capPublisher)
{
_dbContext = dbContext;
_capBus = capPublisher;
}

[Route("~/publish")]
public IActionResult PublishMessage()
[Route("~/without/transaction")]
public IActionResult WithoutTransaction()
{
_capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);

return Ok();
}

[Route("~/publish2")]
public IActionResult PublishMessage2()
[Route("~/adonet/transaction")]
public IActionResult AdonetWithTransaction()
{
using (var connection = new MySqlConnection("Server=192.168.10.110;Database=testcap;UserId=root;Password=123123;"))
//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 transaction = connection.BeginAndJoinToTransaction(_capBus))
using (var transaction = connection.BeginAndJoinToTransaction(_capBus, autoCommit: false))
{
//your business code
connection.Execute("insert into test(name) values('test')", transaction);

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

transaction.Commit();
}
}
}

return Ok();
}

[Route("~/publishWithTrans")]
public async Task<IActionResult> PublishMessageWithTransaction()
[Route("~/ef/transaction")]
public async Task<IActionResult> EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
{
using (var trans = await _dbContext.Database.BeginTransactionAsync())
using (var capTrans = _capBus.Transaction.Begin(trans))
using (var trans = dbContext.Database.BeginAndJoinToTransaction(_capBus, autoCommit: false))
{
for (int i = 0; i < 10; i++)
dbContext.Persons.Add(new Person() { Name = "ef.transaction" });

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

capTrans.Commit();
dbContext.SaveChanges();

trans.Commit();
}
return Ok();
}



[NonAction]
[CapSubscribe("#.rabbitmq.mysql")]
public void ReceiveMessage(DateTime time)
public void Subscriber(DateTime time)
{
Console.WriteLine("[sample.rabbitmq.mysql] message received: " + DateTime.Now + ",sent time: " + time);
Console.WriteLine($@"{DateTime.Now}, Subscriber invoked, Sent time:{time}");
}
}
}

+ 35
- 0
samples/Sample.RabbitMQ.MySql/Migrations/20180821021736_init.Designer.cs View File

@@ -0,0 +1,35 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Sample.RabbitMQ.MySql;

namespace Sample.RabbitMQ.MySql.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20180821021736_init")]
partial class init
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.1-rtm-30846")
.HasAnnotation("Relational:MaxIdentifierLength", 64);

modelBuilder.Entity("Sample.RabbitMQ.MySql.Person", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<string>("Name");

b.HasKey("Id");

b.ToTable("Persons");
});
#pragma warning restore 612, 618
}
}
}

+ 30
- 0
samples/Sample.RabbitMQ.MySql/Migrations/20180821021736_init.cs View File

@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Sample.RabbitMQ.MySql.Migrations
{
public partial class init : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Persons",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Persons", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Persons");
}
}
}

+ 33
- 0
samples/Sample.RabbitMQ.MySql/Migrations/AppDbContextModelSnapshot.cs View File

@@ -0,0 +1,33 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Sample.RabbitMQ.MySql;

namespace Sample.RabbitMQ.MySql.Migrations
{
[DbContext(typeof(AppDbContext))]
partial class AppDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.1-rtm-30846")
.HasAnnotation("Relational:MaxIdentifierLength", 64);

modelBuilder.Entity("Sample.RabbitMQ.MySql.Person", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<string>("Name");

b.HasKey("Id");

b.ToTable("Persons");
});
#pragma warning restore 612, 618
}
}
}

Loading…
Cancel
Save