Browse Source

check the requirement when CAP start.

master
yangxiaodong 7 years ago
parent
commit
a75c0fd5f6
7 changed files with 52 additions and 14 deletions
  1. +2
    -0
      src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs
  2. +1
    -0
      src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs
  3. +1
    -0
      src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlCapOptionsExtension.cs
  4. +2
    -0
      src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs
  5. +1
    -0
      src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs
  6. +29
    -14
      src/DotNetCore.CAP/CAP.AppBuilderExtensions.cs
  7. +16
    -0
      src/DotNetCore.CAP/CAP.Builder.cs

+ 2
- 0
src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs View File

@@ -16,6 +16,8 @@ namespace DotNetCore.CAP

public void AddServices(IServiceCollection services)
{
services.AddSingleton<CapMessageQueueMakerService>();

var kafkaOptions = new KafkaOptions();
_configure?.Invoke(kafkaOptions);
services.AddSingleton(kafkaOptions);


+ 1
- 0
src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs View File

@@ -18,6 +18,7 @@ namespace DotNetCore.CAP

public void AddServices(IServiceCollection services)
{
services.AddSingleton<CapDatabaseStorageMarkerService>();
services.AddSingleton<IStorage, MySqlStorage>();
services.AddScoped<IStorageConnection, MySqlStorageConnection>();
services.AddScoped<ICapPublisher, CapPublisher>();


+ 1
- 0
src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlCapOptionsExtension.cs View File

@@ -18,6 +18,7 @@ namespace DotNetCore.CAP

public void AddServices(IServiceCollection services)
{
services.AddSingleton<CapDatabaseStorageMarkerService>();
services.AddSingleton<IStorage, PostgreSqlStorage>();
services.AddScoped<IStorageConnection, PostgreSqlStorageConnection>();
services.AddScoped<ICapPublisher, CapPublisher>();


+ 2
- 0
src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs View File

@@ -16,6 +16,8 @@ namespace DotNetCore.CAP

public void AddServices(IServiceCollection services)
{
services.AddSingleton<CapMessageQueueMakerService>();

var options = new RabbitMQOptions();
_configure?.Invoke(options);
services.AddSingleton(options);


+ 1
- 0
src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs View File

@@ -18,6 +18,7 @@ namespace DotNetCore.CAP

public void AddServices(IServiceCollection services)
{
services.AddSingleton<CapDatabaseStorageMarkerService>();
services.AddSingleton<IStorage, SqlServerStorage>();
services.AddSingleton<IStorageConnection, SqlServerStorageConnection>();
services.AddTransient<ICapPublisher, CapPublisher>();


+ 29
- 14
src/DotNetCore.CAP/CAP.AppBuilderExtensions.cs View File

@@ -1,7 +1,6 @@
using System;
using DotNetCore.CAP;
using DotNetCore.CAP.Dashboard.GatewayProxy;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Builder
@@ -23,12 +22,7 @@ namespace Microsoft.AspNetCore.Builder
throw new ArgumentNullException(nameof(app));
}

var marker = app.ApplicationServices.GetService<CapMarkerService>();

if (marker == null)
{
throw new InvalidOperationException("Add Cap must be called on the service collection.");
}
CheckRequirement(app);

var provider = app.ApplicationServices;
var bootstrapper = provider.GetRequiredService<IBootstrapper>();
@@ -36,21 +30,42 @@ namespace Microsoft.AspNetCore.Builder
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)
{
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)
{
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>();
app.UseMiddleware<DashboardMiddleware>();
return app;
var messageQueuemarker = app.ApplicationServices.GetService<CapMessageQueueMakerService>();
if (messageQueuemarker == null)
{
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(...) })");
}
}
}
}

+ 16
- 0
src/DotNetCore.CAP/CAP.Builder.cs View File

@@ -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>
/// Allows fine grained configuration of CAP services.
/// </summary>


Loading…
Cancel
Save