Commit a75c0fd5 authored by yangxiaodong's avatar yangxiaodong

check the requirement when CAP start.

parent 74370557
...@@ -16,6 +16,8 @@ namespace DotNetCore.CAP ...@@ -16,6 +16,8 @@ namespace DotNetCore.CAP
public void AddServices(IServiceCollection services) public void AddServices(IServiceCollection services)
{ {
services.AddSingleton<CapMessageQueueMakerService>();
var kafkaOptions = new KafkaOptions(); var kafkaOptions = new KafkaOptions();
_configure?.Invoke(kafkaOptions); _configure?.Invoke(kafkaOptions);
services.AddSingleton(kafkaOptions); services.AddSingleton(kafkaOptions);
......
...@@ -18,6 +18,7 @@ namespace DotNetCore.CAP ...@@ -18,6 +18,7 @@ namespace DotNetCore.CAP
public void AddServices(IServiceCollection services) public void AddServices(IServiceCollection services)
{ {
services.AddSingleton<CapDatabaseStorageMarkerService>();
services.AddSingleton<IStorage, MySqlStorage>(); services.AddSingleton<IStorage, MySqlStorage>();
services.AddScoped<IStorageConnection, MySqlStorageConnection>(); services.AddScoped<IStorageConnection, MySqlStorageConnection>();
services.AddScoped<ICapPublisher, CapPublisher>(); services.AddScoped<ICapPublisher, CapPublisher>();
......
...@@ -18,6 +18,7 @@ namespace DotNetCore.CAP ...@@ -18,6 +18,7 @@ namespace DotNetCore.CAP
public void AddServices(IServiceCollection services) public void AddServices(IServiceCollection services)
{ {
services.AddSingleton<CapDatabaseStorageMarkerService>();
services.AddSingleton<IStorage, PostgreSqlStorage>(); services.AddSingleton<IStorage, PostgreSqlStorage>();
services.AddScoped<IStorageConnection, PostgreSqlStorageConnection>(); services.AddScoped<IStorageConnection, PostgreSqlStorageConnection>();
services.AddScoped<ICapPublisher, CapPublisher>(); services.AddScoped<ICapPublisher, CapPublisher>();
......
...@@ -16,6 +16,8 @@ namespace DotNetCore.CAP ...@@ -16,6 +16,8 @@ namespace DotNetCore.CAP
public void AddServices(IServiceCollection services) public void AddServices(IServiceCollection services)
{ {
services.AddSingleton<CapMessageQueueMakerService>();
var options = new RabbitMQOptions(); var options = new RabbitMQOptions();
_configure?.Invoke(options); _configure?.Invoke(options);
services.AddSingleton(options); services.AddSingleton(options);
......
...@@ -18,6 +18,7 @@ namespace DotNetCore.CAP ...@@ -18,6 +18,7 @@ namespace DotNetCore.CAP
public void AddServices(IServiceCollection services) public void AddServices(IServiceCollection services)
{ {
services.AddSingleton<CapDatabaseStorageMarkerService>();
services.AddSingleton<IStorage, SqlServerStorage>(); services.AddSingleton<IStorage, SqlServerStorage>();
services.AddSingleton<IStorageConnection, SqlServerStorageConnection>(); services.AddSingleton<IStorageConnection, SqlServerStorageConnection>();
services.AddTransient<ICapPublisher, CapPublisher>(); services.AddTransient<ICapPublisher, CapPublisher>();
......
using System; using System;
using DotNetCore.CAP; using DotNetCore.CAP;
using DotNetCore.CAP.Dashboard.GatewayProxy; using DotNetCore.CAP.Dashboard.GatewayProxy;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Builder namespace Microsoft.AspNetCore.Builder
...@@ -23,12 +22,7 @@ namespace Microsoft.AspNetCore.Builder ...@@ -23,12 +22,7 @@ namespace Microsoft.AspNetCore.Builder
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
var marker = app.ApplicationServices.GetService<CapMarkerService>(); CheckRequirement(app);
if (marker == null)
{
throw new InvalidOperationException("Add Cap must be called on the service collection.");
}
var provider = app.ApplicationServices; var provider = app.ApplicationServices;
var bootstrapper = provider.GetRequiredService<IBootstrapper>(); var bootstrapper = provider.GetRequiredService<IBootstrapper>();
...@@ -36,21 +30,42 @@ namespace Microsoft.AspNetCore.Builder ...@@ -36,21 +30,42 @@ namespace Microsoft.AspNetCore.Builder
return app; return app;
} }
///<summary>
/// Enables cap dashboard for the current application
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> instance this method extends.</param>
/// <returns>The <see cref="IApplicationBuilder"/> instance this method extends.</returns>
public static IApplicationBuilder UseCapDashboard(this IApplicationBuilder app) public static IApplicationBuilder UseCapDashboard(this IApplicationBuilder app)
{ {
if (app == null) throw new ArgumentNullException(nameof(app)); if (app == null) throw new ArgumentNullException(nameof(app));
var marker = app.ApplicationServices.GetService<CapMarkerService>(); CheckRequirement(app);
app.UseMiddleware<GatewayProxyMiddleware>();
app.UseMiddleware<DashboardMiddleware>();
return app;
}
private static void CheckRequirement(IApplicationBuilder app)
{
var marker = app.ApplicationServices.GetService<CapMarkerService>();
if (marker == null) if (marker == null)
{ {
throw new InvalidOperationException("Add Cap must be called on the service collection."); throw new InvalidOperationException("AddCap must be called on the service collection. eg: services.AddCap(...)");
} }
app.UseMiddleware<GatewayProxyMiddleware>(); var messageQueuemarker = app.ApplicationServices.GetService<CapMessageQueueMakerService>();
app.UseMiddleware<DashboardMiddleware>(); if (messageQueuemarker == null)
{
return app; throw new InvalidOperationException("You must be config used database provider at AddCap() options! eg: services.AddCap(options=>{ options.UseKafka(...) })");
}
var databaseMarker = app.ApplicationServices.GetService<CapDatabaseStorageMarkerService>();
if (databaseMarker == null)
{
throw new InvalidOperationException("You must be config used database provider at AddCap() options! eg: services.AddCap(options=>{ options.UseSqlServer(...) })");
}
} }
} }
} }
\ No newline at end of file
...@@ -10,6 +10,22 @@ namespace DotNetCore.CAP ...@@ -10,6 +10,22 @@ namespace DotNetCore.CAP
{ {
} }
/// <summary>
/// Used to verify cap database storage extension was added on a ServiceCollection
/// </summary>
public class CapDatabaseStorageMarkerService
{
}
/// <summary>
/// Used to verify cap message queue extension was added on a ServiceCollection
/// </summary>
public class CapMessageQueueMakerService
{
}
/// <summary> /// <summary>
/// Allows fine grained configuration of CAP services. /// Allows fine grained configuration of CAP services.
/// </summary> /// </summary>
......
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