Commit 9424cfd7 authored by Savorboard's avatar Savorboard

Instead of manually configuring app.UseCAP using StartupFilter injection

parent 690380a3
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
using System; using System;
using DotNetCore.CAP; using DotNetCore.CAP;
using DotNetCore.CAP.Dashboard.GatewayProxy; using DotNetCore.CAP.Dashboard.GatewayProxy;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
...@@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Builder ...@@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Builder
/// <summary> /// <summary>
/// app extensions for <see cref="IApplicationBuilder" /> /// app extensions for <see cref="IApplicationBuilder" />
/// </summary> /// </summary>
public static class AppBuilderExtensions internal static class AppBuilderExtensions
{ {
/// <summary> /// <summary>
/// Enables cap for the current application /// Enables cap for the current application
...@@ -70,4 +71,17 @@ namespace Microsoft.AspNetCore.Builder ...@@ -70,4 +71,17 @@ namespace Microsoft.AspNetCore.Builder
} }
} }
} }
sealed class CapStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
app.UseCap();
next(app);
};
}
}
} }
\ No newline at end of file
...@@ -8,6 +8,8 @@ using DotNetCore.CAP.Abstractions; ...@@ -8,6 +8,8 @@ using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Internal; using DotNetCore.CAP.Internal;
using DotNetCore.CAP.Processor; using DotNetCore.CAP.Processor;
using DotNetCore.CAP.Processor.States; using DotNetCore.CAP.Processor.States;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
...@@ -24,9 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -24,9 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="services">The services available in the application.</param> /// <param name="services">The services available in the application.</param>
/// <param name="setupAction">An action to configure the <see cref="CapOptions" />.</param> /// <param name="setupAction">An action to configure the <see cref="CapOptions" />.</param>
/// <returns>An <see cref="CapBuilder" /> for application services.</returns> /// <returns>An <see cref="CapBuilder" /> for application services.</returns>
public static CapBuilder AddCap( public static CapBuilder AddCap(this IServiceCollection services, Action<CapOptions> setupAction)
this IServiceCollection services,
Action<CapOptions> setupAction)
{ {
if (setupAction == null) if (setupAction == null)
{ {
...@@ -36,6 +36,7 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -36,6 +36,7 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddSingleton<CapMarkerService>(); services.TryAddSingleton<CapMarkerService>();
services.Configure(setupAction); services.Configure(setupAction);
//Consumer service
AddSubscribeServices(services); AddSubscribeServices(services);
//Serializer and model binder //Serializer and model binder
...@@ -49,18 +50,18 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -49,18 +50,18 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddSingleton<MethodMatcherCache>(); services.TryAddSingleton<MethodMatcherCache>();
//Bootstrapper and Processors //Bootstrapper and Processors
services.AddSingleton<IProcessingServer, ConsumerHandler>(); services.TryAddEnumerable(ServiceDescriptor.Singleton<IProcessingServer, ConsumerHandler>());
services.AddSingleton<IProcessingServer, CapProcessingServer>(); services.TryAddEnumerable(ServiceDescriptor.Singleton<IProcessingServer, CapProcessingServer>());
services.AddSingleton<IBootstrapper, DefaultBootstrapper>(); services.TryAddSingleton<IBootstrapper, DefaultBootstrapper>();
services.AddSingleton<IStateChanger, StateChanger>(); services.TryAddSingleton<IStateChanger, StateChanger>();
//Queue's message processor //Queue's message processor
services.AddTransient<NeedRetryMessageProcessor>(); services.TryAddSingleton<NeedRetryMessageProcessor>();
//Sender and Executors //Sender and Executors
services.AddSingleton<IDispatcher, Dispatcher>(); services.TryAddSingleton<IDispatcher, Dispatcher>();
// Warning: IPublishMessageSender need to inject at extension project. // Warning: IPublishMessageSender need to inject at extension project.
services.AddSingleton<ISubscriberExecutor, DefaultSubscriberExecutor>(); services.TryAddSingleton<ISubscriberExecutor, DefaultSubscriberExecutor>();
//Options and extension service //Options and extension service
var options = new CapOptions(); var options = new CapOptions();
...@@ -69,9 +70,11 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -69,9 +70,11 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
serviceExtension.AddServices(services); serviceExtension.AddServices(services);
} }
services.AddSingleton(options); services.AddSingleton(options);
//Startup and Middleware
services.AddTransient<IStartupFilter, CapStartupFilter>();
return new CapBuilder(services); return new CapBuilder(services);
} }
...@@ -90,7 +93,7 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -90,7 +93,7 @@ namespace Microsoft.Extensions.DependencyInjection
foreach (var service in consumerListenerServices) foreach (var service in consumerListenerServices)
{ {
services.AddTransient(service.Key, service.Value); services.TryAddEnumerable(ServiceDescriptor.Transient(service.Key, service.Value));
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment