From 556cb5a0a59083f81b3e4d96615176bc4ba8566b Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 26 Sep 2019 09:49:25 +0800 Subject: [PATCH] code refactor --- CAP.sln | 1 + samples/Sample.RabbitMQ.MySql/AppDbContext.cs | 2 +- samples/Sample.RabbitMQ.MySql/Startup.cs | 7 ++++--- src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs | 3 ++- src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs | 8 +++++--- src/DotNetCore.CAP/Internal/IMessageSender.Default.cs | 2 +- src/DotNetCore.CAP/Messages/TransportMessage.cs | 4 ++-- 7 files changed, 16 insertions(+), 11 deletions(-) diff --git a/CAP.sln b/CAP.sln index f9bcb17..66c3488 100644 --- a/CAP.sln +++ b/CAP.sln @@ -108,6 +108,7 @@ Global {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 {82C403AB-ED68-4084-9A1D-11334F9F08F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU diff --git a/samples/Sample.RabbitMQ.MySql/AppDbContext.cs b/samples/Sample.RabbitMQ.MySql/AppDbContext.cs index ce3535d..346209e 100644 --- a/samples/Sample.RabbitMQ.MySql/AppDbContext.cs +++ b/samples/Sample.RabbitMQ.MySql/AppDbContext.cs @@ -11,7 +11,7 @@ namespace Sample.RabbitMQ.MySql public class AppDbContext : DbContext { - public const string ConnectionString = "Server=localhost;Database=testcap;UserId=root;Password=123123;"; + public const string ConnectionString = "Server=192.168.2.120;Database=captest;UserId=root;Password=123123;"; public DbSet Persons { get; set; } diff --git a/samples/Sample.RabbitMQ.MySql/Startup.cs b/samples/Sample.RabbitMQ.MySql/Startup.cs index cf6b8f8..f402deb 100644 --- a/samples/Sample.RabbitMQ.MySql/Startup.cs +++ b/samples/Sample.RabbitMQ.MySql/Startup.cs @@ -1,4 +1,5 @@ using System; +using DotNetCore.CAP.Messages; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; @@ -16,12 +17,12 @@ namespace Sample.RabbitMQ.MySql { x.UseEntityFramework(); x.UseRabbitMQ("192.168.2.120"); - x.UseDashboard(); + //x.UseDashboard(); x.FailedRetryCount = 5; - x.FailedThresholdCallback = (type, name, content) => + x.FailedThresholdCallback = (type, msg) => { Console.WriteLine( - $@"A message of type {type} failed after executing {x.FailedRetryCount} several times, requiring manual troubleshooting. Message name: {name}, message body: {content}"); + $@"A message of type {type} failed after executing {x.FailedRetryCount} several times, requiring manual troubleshooting. Message name: {msg.GetName()}"); }; }); diff --git a/src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs b/src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs index 76aa126..7e3a380 100644 --- a/src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs @@ -3,6 +3,7 @@ using System; using DotNetCore.CAP.MySql; +using DotNetCore.CAP.Persistence; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -21,7 +22,7 @@ namespace DotNetCore.CAP public void AddServices(IServiceCollection services) { services.AddSingleton(); - //services.AddSingleton(); + services.AddSingleton(); services.AddTransient(); //Add MySqlOptions diff --git a/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs b/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs index f349455..43afd25 100644 --- a/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs +++ b/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs @@ -6,8 +6,7 @@ using DotNetCore.CAP; using DotNetCore.CAP.Abstractions; using DotNetCore.CAP.Internal; using DotNetCore.CAP.Processor; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; +using DotNetCore.CAP.Serialization; using Microsoft.Extensions.DependencyInjection.Extensions; // ReSharper disable once CheckNamespace @@ -58,8 +57,11 @@ namespace Microsoft.Extensions.DependencyInjection services.TryAddSingleton(); //Sender and Executors - services.AddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); + + services.TryAddSingleton(); + // Warning: IPublishMessageSender need to inject at extension project. services.TryAddSingleton(); diff --git a/src/DotNetCore.CAP/Internal/IMessageSender.Default.cs b/src/DotNetCore.CAP/Internal/IMessageSender.Default.cs index 364b4b5..33ee272 100644 --- a/src/DotNetCore.CAP/Internal/IMessageSender.Default.cs +++ b/src/DotNetCore.CAP/Internal/IMessageSender.Default.cs @@ -29,7 +29,7 @@ namespace DotNetCore.CAP.Internal new DiagnosticListener(CapDiagnosticListenerExtensions.DiagnosticListenerName); protected MessageSender( - ILogger logger, + ILogger logger, IOptions options, IDataStorage dataStorage, ISerializer serializer, diff --git a/src/DotNetCore.CAP/Messages/TransportMessage.cs b/src/DotNetCore.CAP/Messages/TransportMessage.cs index b360ea9..93e32bd 100644 --- a/src/DotNetCore.CAP/Messages/TransportMessage.cs +++ b/src/DotNetCore.CAP/Messages/TransportMessage.cs @@ -8,7 +8,7 @@ namespace DotNetCore.CAP.Messages /// public class TransportMessage { - public TransportMessage(Dictionary headers, byte[] body) + public TransportMessage(IDictionary headers, byte[] body) { Headers = headers ?? throw new ArgumentNullException(nameof(headers)); Body = body ?? throw new ArgumentNullException(nameof(body)); @@ -17,7 +17,7 @@ namespace DotNetCore.CAP.Messages /// /// Gets the headers of this message /// - public Dictionary Headers { get; } + public IDictionary Headers { get; } /// /// Gets the body object of this message