From d42c34c1b2f521bdf8377c42870269f9e93cf478 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Mon, 24 Jul 2017 18:29:18 +0800 Subject: [PATCH] update samples. --- CAP.sln | 9 ++++- .../Controllers/ValuesController.cs | 39 ++++++++----------- samples/Sample.Kafka/Program.cs | 10 ++++- ...a.csproj => Sample.Kafka.SqlServer.csproj} | 10 ++--- samples/Sample.Kafka/Startup.cs | 19 +-------- .../Sample.Kafka/appsettings.Development.json | 10 ----- samples/Sample.Kafka/appsettings.json | 8 ---- 7 files changed, 37 insertions(+), 68 deletions(-) rename samples/Sample.Kafka/{Sample.Kafka.csproj => Sample.Kafka.SqlServer.csproj} (87%) delete mode 100644 samples/Sample.Kafka/appsettings.Development.json delete mode 100644 samples/Sample.Kafka/appsettings.json diff --git a/CAP.sln b/CAP.sln index 5100e49..257ccf9 100644 --- a/CAP.sln +++ b/CAP.sln @@ -35,7 +35,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP", "src\DotNe EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{3A6B6931-A123-477A-9469-8B468B5385AF}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Kafka", "samples\Sample.Kafka\Sample.Kafka.csproj", "{2F095ED9-5BC9-4512-9013-A47685FB2508}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Kafka.SqlServer", "samples\Sample.Kafka\Sample.Kafka.SqlServer.csproj", "{2F095ED9-5BC9-4512-9013-A47685FB2508}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.Kafka", "src\DotNetCore.CAP.Kafka\DotNetCore.CAP.Kafka.csproj", "{C42CDE33-0878-4BA0-96F2-4CB7C8FDEAAD}" EndProject @@ -63,6 +63,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetCore.CAP.MySql", "src EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetCore.CAP.MySql.Test", "test\DotNetCore.CAP.MySql.Test\DotNetCore.CAP.MySql.Test.csproj", "{80A84F62-1558-427B-BA74-B47AA8A665B5}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.RabbitMQ.MySql", "samples\Sample.RabbitMQ.MySql\Sample.RabbitMQ.MySql.csproj", "{9F3F9BFE-7B6A-4A7A-A6E6-8B517D611873}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -104,6 +106,10 @@ Global {80A84F62-1558-427B-BA74-B47AA8A665B5}.Debug|Any CPU.Build.0 = Debug|Any CPU {80A84F62-1558-427B-BA74-B47AA8A665B5}.Release|Any CPU.ActiveCfg = Release|Any CPU {80A84F62-1558-427B-BA74-B47AA8A665B5}.Release|Any CPU.Build.0 = Release|Any CPU + {9F3F9BFE-7B6A-4A7A-A6E6-8B517D611873}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F3F9BFE-7B6A-4A7A-A6E6-8B517D611873}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F3F9BFE-7B6A-4A7A-A6E6-8B517D611873}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F3F9BFE-7B6A-4A7A-A6E6-8B517D611873}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -119,5 +125,6 @@ Global {DA00FA38-C4B9-4F55-8756-D480FBC1084F} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0} {FA15685A-778A-4D2A-A2FE-27FAD2FFA65B} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4} {80A84F62-1558-427B-BA74-B47AA8A665B5} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0} + {9F3F9BFE-7B6A-4A7A-A6E6-8B517D611873} = {3A6B6931-A123-477A-9469-8B468B5385AF} EndGlobalSection EndGlobal diff --git a/samples/Sample.Kafka/Controllers/ValuesController.cs b/samples/Sample.Kafka/Controllers/ValuesController.cs index 6783b19..61b511a 100644 --- a/samples/Sample.Kafka/Controllers/ValuesController.cs +++ b/samples/Sample.Kafka/Controllers/ValuesController.cs @@ -1,7 +1,7 @@ using System; +using System.Diagnostics; using System.Threading.Tasks; using DotNetCore.CAP; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; namespace Sample.Kafka.Controllers @@ -9,46 +9,39 @@ namespace Sample.Kafka.Controllers [Route("api/[controller]")] public class ValuesController : Controller, ICapSubscribe { - private readonly ICapPublisher _producer; - private readonly AppDbContext _dbContext ; + private readonly ICapPublisher _capBus; + private readonly AppDbContext _dbContext; public ValuesController(ICapPublisher producer, AppDbContext dbContext) { - _producer = producer; + _capBus = producer; _dbContext = dbContext; } - [Route("/")] - public IActionResult Index() + [Route("~/publish")] + public IActionResult PublishMessage() { + _capBus.Publish("sample.rabbitmq.mysql", ""); return Ok(); } - public string ServerPath => ((IHostingEnvironment)HttpContext.RequestServices.GetService(typeof(IHostingEnvironment))).ContentRootPath; - [CapSubscribe("zzwl.topic.finace.callBack", Group = "test")] - public void KafkaTest(Person person) + [Route("~/publishWithTrans")] + public async Task PublishMessageWithTransaction() { - Console.WriteLine(DateTime.Now); - } - - [Route("~/send")] - public async Task SendTopic() - { - using (var trans = _dbContext.Database.BeginTransaction()) + using (var trans = await _dbContext.Database.BeginTransactionAsync()) { - await _producer.PublishAsync("zzwl.topic.finace.callBack",""); - + await _capBus.PublishAsync("sample.rabbitmq.mysql", ""); trans.Commit(); } - return Ok(); } - public class Person + [NonAction] + [CapSubscribe("sample.kafka.sqlserver", Group = "test")] + public void KafkaTest() { - public string Name { get; set; } - - public int Age { get; set; } + Console.WriteLine("[sample.kafka.sqlserver] message received"); + Debug.WriteLine("[sample.kafka.sqlserver] message received"); } } } \ No newline at end of file diff --git a/samples/Sample.Kafka/Program.cs b/samples/Sample.Kafka/Program.cs index 659106f..37d3089 100644 --- a/samples/Sample.Kafka/Program.cs +++ b/samples/Sample.Kafka/Program.cs @@ -1,13 +1,21 @@ using System.IO; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; namespace Sample.Kafka { public class Program { - public static void Main(string[] args) { + public static void Main(string[] args) + { + var config = new ConfigurationBuilder() + .AddCommandLine(args) + .AddEnvironmentVariables("ASPNETCORE_") + .Build(); + var host = new WebHostBuilder() + .UseConfiguration(config) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() diff --git a/samples/Sample.Kafka/Sample.Kafka.csproj b/samples/Sample.Kafka/Sample.Kafka.SqlServer.csproj similarity index 87% rename from samples/Sample.Kafka/Sample.Kafka.csproj rename to samples/Sample.Kafka/Sample.Kafka.SqlServer.csproj index 5c9e38f..675a95b 100644 --- a/samples/Sample.Kafka/Sample.Kafka.csproj +++ b/samples/Sample.Kafka/Sample.Kafka.SqlServer.csproj @@ -2,14 +2,9 @@ netcoreapp1.1 + Sample.Kafka.SqlServer - - - - - - - + @@ -17,6 +12,7 @@ + diff --git a/samples/Sample.Kafka/Startup.cs b/samples/Sample.Kafka/Startup.cs index add80bb..08291c5 100644 --- a/samples/Sample.Kafka/Startup.cs +++ b/samples/Sample.Kafka/Startup.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -8,19 +7,6 @@ namespace Sample.Kafka { public class Startup { - public Startup(IHostingEnvironment env) - { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); - } - - public IConfigurationRoot Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext(); @@ -28,18 +14,15 @@ namespace Sample.Kafka services.AddCap(x => { x.UseEntityFramework(); - //x.UseSqlServer("Server=DESKTOP-M9R8T31;Initial Catalog=Test;User Id=sa;Password=P@ssw0rd;MultipleActiveResultSets=True"); x.UseKafka("localhost:9092"); }); - // Add framework services. services.AddMvc(); } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); + loggerFactory.AddConsole(); loggerFactory.AddDebug(); app.UseMvc(); diff --git a/samples/Sample.Kafka/appsettings.Development.json b/samples/Sample.Kafka/appsettings.Development.json deleted file mode 100644 index fa8ce71..0000000 --- a/samples/Sample.Kafka/appsettings.Development.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - } -} diff --git a/samples/Sample.Kafka/appsettings.json b/samples/Sample.Kafka/appsettings.json deleted file mode 100644 index 5fff67b..0000000 --- a/samples/Sample.Kafka/appsettings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Warning" - } - } -}