Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
9ecc5254
Commit
9ecc5254
authored
Aug 21, 2018
by
Savorboard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update samples
parent
797b1f71
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
136 additions
and
22 deletions
+136
-22
AppDbContext.cs
samples/Sample.RabbitMQ.MySql/AppDbContext.cs
+12
-1
ValuesController.cs
...les/Sample.RabbitMQ.MySql/Controllers/ValuesController.cs
+26
-21
20180821021736_init.Designer.cs
...RabbitMQ.MySql/Migrations/20180821021736_init.Designer.cs
+35
-0
20180821021736_init.cs
...s/Sample.RabbitMQ.MySql/Migrations/20180821021736_init.cs
+30
-0
AppDbContextModelSnapshot.cs
...le.RabbitMQ.MySql/Migrations/AppDbContextModelSnapshot.cs
+33
-0
No files found.
samples/Sample.RabbitMQ.MySql/AppDbContext.cs
View file @
9ecc5254
...
...
@@ -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
);
}
}
}
samples/Sample.RabbitMQ.MySql/Controllers/ValuesController.cs
View file @
9ecc5254
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
}
"
);
}
}
}
samples/Sample.RabbitMQ.MySql/Migrations/20180821021736_init.Designer.cs
0 → 100644
View file @
9ecc5254
// <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
}
}
}
samples/Sample.RabbitMQ.MySql/Migrations/20180821021736_init.cs
0 → 100644
View file @
9ecc5254
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"
);
}
}
}
samples/Sample.RabbitMQ.MySql/Migrations/AppDbContextModelSnapshot.cs
0 → 100644
View file @
9ecc5254
// <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
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment