From aedf82cbde3043cef9858b5a65c78f8e7b3f7fe7 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 5 Dec 2019 14:00:44 +0800 Subject: [PATCH] Add sqlsever samples --- CAP.sln | 7 ++ .../Sample.RabbitMQ.SqlServer/AppDbContext.cs | 40 ++++++++++ .../Controllers/ValuesController.cs | 76 +++++++++++++++++++ .../20191205032949_FirstMigration.Designer.cs | 40 ++++++++++ .../20191205032949_FirstMigration.cs | 29 +++++++ .../Migrations/AppDbContextModelSnapshot.cs | 38 ++++++++++ samples/Sample.RabbitMQ.SqlServer/Program.cs | 20 +++++ .../Sample.RabbitMQ.SqlServer.csproj | 22 ++++++ samples/Sample.RabbitMQ.SqlServer/Startup.cs | 39 ++++++++++ .../appsettings.json | 8 ++ 10 files changed, 319 insertions(+) create mode 100644 samples/Sample.RabbitMQ.SqlServer/AppDbContext.cs create mode 100644 samples/Sample.RabbitMQ.SqlServer/Controllers/ValuesController.cs create mode 100644 samples/Sample.RabbitMQ.SqlServer/Migrations/20191205032949_FirstMigration.Designer.cs create mode 100644 samples/Sample.RabbitMQ.SqlServer/Migrations/20191205032949_FirstMigration.cs create mode 100644 samples/Sample.RabbitMQ.SqlServer/Migrations/AppDbContextModelSnapshot.cs create mode 100644 samples/Sample.RabbitMQ.SqlServer/Program.cs create mode 100644 samples/Sample.RabbitMQ.SqlServer/Sample.RabbitMQ.SqlServer.csproj create mode 100644 samples/Sample.RabbitMQ.SqlServer/Startup.cs create mode 100644 samples/Sample.RabbitMQ.SqlServer/appsettings.json diff --git a/CAP.sln b/CAP.sln index 8e82097..fe756d5 100644 --- a/CAP.sln +++ b/CAP.sln @@ -70,6 +70,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.Dashboard", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Kafka.InMemory", "samples\Sample.Kafka.InMemory\Sample.Kafka.InMemory.csproj", "{1B0371D6-36A4-4C78-A727-8ED732FDBA1D}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.RabbitMQ.SqlServer", "samples\Sample.RabbitMQ.SqlServer\Sample.RabbitMQ.SqlServer.csproj", "{F6C5C676-AF05-46D5-A45D-442137B31898}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -151,6 +153,10 @@ Global {1B0371D6-36A4-4C78-A727-8ED732FDBA1D}.Debug|Any CPU.Build.0 = Debug|Any CPU {1B0371D6-36A4-4C78-A727-8ED732FDBA1D}.Release|Any CPU.ActiveCfg = Release|Any CPU {1B0371D6-36A4-4C78-A727-8ED732FDBA1D}.Release|Any CPU.Build.0 = Release|Any CPU + {F6C5C676-AF05-46D5-A45D-442137B31898}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F6C5C676-AF05-46D5-A45D-442137B31898}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F6C5C676-AF05-46D5-A45D-442137B31898}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F6C5C676-AF05-46D5-A45D-442137B31898}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -175,6 +181,7 @@ Global {58B6E829-C6C8-457C-9DD0-C600650254DF} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4} {56FB261C-67AF-4715-9A46-4FA4FAB91B2C} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4} {1B0371D6-36A4-4C78-A727-8ED732FDBA1D} = {3A6B6931-A123-477A-9469-8B468B5385AF} + {F6C5C676-AF05-46D5-A45D-442137B31898} = {3A6B6931-A123-477A-9469-8B468B5385AF} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {2E70565D-94CF-40B4-BFE1-AC18D5F736AB} diff --git a/samples/Sample.RabbitMQ.SqlServer/AppDbContext.cs b/samples/Sample.RabbitMQ.SqlServer/AppDbContext.cs new file mode 100644 index 0000000..fdad2a5 --- /dev/null +++ b/samples/Sample.RabbitMQ.SqlServer/AppDbContext.cs @@ -0,0 +1,40 @@ +using Microsoft.EntityFrameworkCore; + +namespace Sample.RabbitMQ.SqlServer +{ + public class Person + { + public int Id { get; set; } + + public string Name { get; set; } + + public override string ToString() + { + return $"Name:{Name}, Id:{Id}"; + } + } + + public class Person2 + { + public int Id { get; set; } + + public string Name { get; set; } + + public override string ToString() + { + return $"Name:{Name}, Id:{Id}"; + } + } + + public class AppDbContext : DbContext + { + public const string ConnectionString = "Server=192.168.2.120;Database=captest;User Id=sa;Password=P@ssw0rd;"; + + public DbSet Persons { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer(ConnectionString); + } + } +} diff --git a/samples/Sample.RabbitMQ.SqlServer/Controllers/ValuesController.cs b/samples/Sample.RabbitMQ.SqlServer/Controllers/ValuesController.cs new file mode 100644 index 0000000..24d1848 --- /dev/null +++ b/samples/Sample.RabbitMQ.SqlServer/Controllers/ValuesController.cs @@ -0,0 +1,76 @@ +using System; +using System.Data; +using System.Threading.Tasks; +using Dapper; +using DotNetCore.CAP; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Data.SqlClient; + +namespace Sample.RabbitMQ.SqlServer.Controllers +{ + [Route("api/[controller]")] + public class ValuesController : Controller + { + private readonly ICapPublisher _capBus; + + public ValuesController(ICapPublisher capPublisher) + { + _capBus = capPublisher; + } + + [Route("~/without/transaction")] + public async Task WithoutTransaction() + { + await _capBus.PublishAsync("sample.rabbitmq.mysql", new Person() + { + Id = 123, + Name = "Bar" + }); + + return Ok(); + } + + [Route("~/adonet/transaction")] + public IActionResult AdonetWithTransaction() + { + using (var connection = new SqlConnection(AppDbContext.ConnectionString)) + { + using (var transaction = connection.BeginTransaction(_capBus, true)) + { + //your business code + connection.Execute("insert into test(name) values('test')", transaction: transaction); + + _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now); + } + } + + return Ok(); + } + + [Route("~/ef/transaction")] + public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext) + { + using (dbContext.Database.BeginTransaction(_capBus, autoCommit: true)) + { + dbContext.Persons.Add(new Person() { Name = "ef.transaction" }); + + _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now); + } + return Ok(); + } + + [NonAction] + [CapSubscribe("sample.rabbitmq.mysql")] + public void Subscriber(DateTime p) + { + Console.WriteLine($@"{DateTime.Now} Subscriber invoked, Info: {p}"); + } + + [NonAction] + [CapSubscribe("sample.rabbitmq.mysql", Group = "group.test2")] + public void Subscriber2(DateTime p, [FromCap]CapHeader header) + { + Console.WriteLine($@"{DateTime.Now} Subscriber invoked, Info: {p}"); + } + } +} diff --git a/samples/Sample.RabbitMQ.SqlServer/Migrations/20191205032949_FirstMigration.Designer.cs b/samples/Sample.RabbitMQ.SqlServer/Migrations/20191205032949_FirstMigration.Designer.cs new file mode 100644 index 0000000..7feec6f --- /dev/null +++ b/samples/Sample.RabbitMQ.SqlServer/Migrations/20191205032949_FirstMigration.Designer.cs @@ -0,0 +1,40 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Sample.RabbitMQ.SqlServer; + +namespace Sample.RabbitMQ.SqlServer.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20191205032949_FirstMigration")] + partial class FirstMigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("Sample.RabbitMQ.SqlServer.Person", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Name") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Persons"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/samples/Sample.RabbitMQ.SqlServer/Migrations/20191205032949_FirstMigration.cs b/samples/Sample.RabbitMQ.SqlServer/Migrations/20191205032949_FirstMigration.cs new file mode 100644 index 0000000..019bef1 --- /dev/null +++ b/samples/Sample.RabbitMQ.SqlServer/Migrations/20191205032949_FirstMigration.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace Sample.RabbitMQ.SqlServer.Migrations +{ + public partial class FirstMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Persons", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Persons", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Persons"); + } + } +} diff --git a/samples/Sample.RabbitMQ.SqlServer/Migrations/AppDbContextModelSnapshot.cs b/samples/Sample.RabbitMQ.SqlServer/Migrations/AppDbContextModelSnapshot.cs new file mode 100644 index 0000000..4817bb3 --- /dev/null +++ b/samples/Sample.RabbitMQ.SqlServer/Migrations/AppDbContextModelSnapshot.cs @@ -0,0 +1,38 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Sample.RabbitMQ.SqlServer; + +namespace Sample.RabbitMQ.SqlServer.Migrations +{ + [DbContext(typeof(AppDbContext))] + partial class AppDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("Sample.RabbitMQ.SqlServer.Person", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Name") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Persons"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/samples/Sample.RabbitMQ.SqlServer/Program.cs b/samples/Sample.RabbitMQ.SqlServer/Program.cs new file mode 100644 index 0000000..fd0c137 --- /dev/null +++ b/samples/Sample.RabbitMQ.SqlServer/Program.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; + +namespace Sample.RabbitMQ.SqlServer +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/samples/Sample.RabbitMQ.SqlServer/Sample.RabbitMQ.SqlServer.csproj b/samples/Sample.RabbitMQ.SqlServer/Sample.RabbitMQ.SqlServer.csproj new file mode 100644 index 0000000..c68c8ef --- /dev/null +++ b/samples/Sample.RabbitMQ.SqlServer/Sample.RabbitMQ.SqlServer.csproj @@ -0,0 +1,22 @@ + + + + netcoreapp3.0 + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + diff --git a/samples/Sample.RabbitMQ.SqlServer/Startup.cs b/samples/Sample.RabbitMQ.SqlServer/Startup.cs new file mode 100644 index 0000000..38b91f7 --- /dev/null +++ b/samples/Sample.RabbitMQ.SqlServer/Startup.cs @@ -0,0 +1,39 @@ +using System; +using DotNetCore.CAP.Messages; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace Sample.RabbitMQ.SqlServer +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddDbContext(); + + services.AddCap(x => + { + x.UseEntityFramework(); + x.UseRabbitMQ("192.168.2.120"); + x.UseDashboard(); + x.FailedRetryCount = 5; + x.FailedThresholdCallback = (type, msg) => + { + Console.WriteLine( + $@"A message of type {type} failed after executing {x.FailedRetryCount} several times, requiring manual troubleshooting. Message name: {msg.GetName()}"); + }; + }); + + services.AddControllers(); + } + + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/samples/Sample.RabbitMQ.SqlServer/appsettings.json b/samples/Sample.RabbitMQ.SqlServer/appsettings.json new file mode 100644 index 0000000..50fe9a3 --- /dev/null +++ b/samples/Sample.RabbitMQ.SqlServer/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Error" + } + } +}