Преглед изворни кода

Add a check for using Cap in DbContext and throw exception prompt. #1067 #539

master
Savorboard пре 2 година
родитељ
комит
2aecefbd2b
5 измењених фајлова са 36 додато и 2 уклоњено
  1. +7
    -0
      src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs
  2. +8
    -1
      src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlOptions.cs
  3. +7
    -0
      src/DotNetCore.CAP.SqlServer/CAP.SqlServerOptions.cs
  4. +9
    -0
      src/DotNetCore.CAP/Internal/Helper.cs
  5. +5
    -1
      src/DotNetCore.CAP/Internal/IBootstrapper.Default.cs

+ 7
- 0
src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs Прегледај датотеку

@@ -1,6 +1,8 @@
// Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using DotNetCore.CAP.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
@@ -29,6 +31,11 @@ namespace DotNetCore.CAP
{
if (options.DbContextType != null)
{
if (Helper.IsUsingType<ICapPublisher>(options.DbContextType))
{
throw new InvalidOperationException("We detected that you are using ICapPublisher in DbContext, please change the configuration to use the storage extension directly to avoid circular references! eg: x.UseMySql()");
}

using var scope = _serviceScopeFactory.CreateScope();
var provider = scope.ServiceProvider;
using var dbContext = (DbContext)provider.GetRequiredService(options.DbContextType);


+ 8
- 1
src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlOptions.cs Прегледај датотеку

@@ -1,6 +1,8 @@
// Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using DotNetCore.CAP.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
@@ -28,7 +30,12 @@ namespace DotNetCore.CAP
public void Configure(PostgreSqlOptions options)
{
if (options.DbContextType == null) return;

if (Helper.IsUsingType<ICapPublisher>(options.DbContextType))
{
throw new InvalidOperationException("We detected that you are using ICapPublisher in DbContext, please change the configuration to use the storage extension directly to avoid circular references! eg: x.UsePostgreSql()");
}

using var scope = _serviceScopeFactory.CreateScope();
var provider = scope.ServiceProvider;
using var dbContext = (DbContext) provider.GetRequiredService(options.DbContextType);


+ 7
- 0
src/DotNetCore.CAP.SqlServer/CAP.SqlServerOptions.cs Прегледај датотеку

@@ -1,6 +1,8 @@
// Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using DotNetCore.CAP.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
@@ -30,6 +32,11 @@ namespace DotNetCore.CAP
{
if (options.DbContextType == null) return;

if (Helper.IsUsingType<ICapPublisher>(options.DbContextType))
{
throw new InvalidOperationException("We detected that you are using ICapPublisher in DbContext, please change the configuration to use the storage extension directly to avoid circular references! eg: x.UseSqlServer()");
}

using var scope = _serviceScopeFactory.CreateScope();
var provider = scope.ServiceProvider;
using var dbContext = (DbContext)provider.GetRequiredService(options.DbContextType);


+ 9
- 0
src/DotNetCore.CAP/Internal/Helper.cs Прегледај датотеку

@@ -3,6 +3,7 @@

using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

@@ -66,6 +67,14 @@ namespace DotNetCore.CAP.Internal
return Regex.IsMatch(name, pattern) ? Regex.Replace(name, pattern, "_") : name;
}

public static bool IsUsingType<T>(in Type type)
{
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly;
return type.GetFields(flags).Any(x => x.FieldType == typeof(T));
}

public static bool IsInnerIP(string ipAddress)
{
var ipNum = GetIpNum(ipAddress);


+ 5
- 1
src/DotNetCore.CAP/Internal/IBootstrapper.Default.cs Прегледај датотеку

@@ -19,7 +19,7 @@ namespace DotNetCore.CAP.Internal
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<Bootstrapper> _logger;
private readonly CancellationTokenSource _cts = new ();
private readonly CancellationTokenSource _cts = new();
private bool _disposed;
private IEnumerable<IProcessingServer> _processors = default!;

@@ -43,6 +43,10 @@ namespace DotNetCore.CAP.Internal
}
catch (Exception e)
{
if (e is InvalidOperationException)
{
throw;
}
_logger.LogError(e, "Initializing the storage structure failed!");
}



Loading…
Откажи
Сачувај