Skip to content

Commit

Permalink
😊 修复 模块化架构中 Inject 重复注册导致异常问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkSoul committed Feb 21, 2024
1 parent bb7d9a2 commit 1651013
Show file tree
Hide file tree
Showing 28 changed files with 120 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.23.1" />
<PackageReference Include="MongoDB.Driver" Version="2.23.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="sqlSugarCore" Version="5.1.4.141" />
<PackageReference Include="sqlSugarCore" Version="5.1.4.142" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion framework/Furion.Pure.Xunit/Furion.Pure.Xunit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Furion.Pure" Version="4.9.1.31" />
<PackageReference Include="xunit.extensibility.execution" Version="2.6.6" />
<PackageReference Include="xunit.extensibility.execution" Version="2.7.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Furion;
using Furion.CorsAccessor;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection;

Expand All @@ -23,6 +24,12 @@ public static class CorsAccessorServiceCollectionExtensions
/// <returns>服务集合</returns>
public static IServiceCollection AddCorsAccessor(this IServiceCollection services, Action<CorsOptions> corsOptionsHandler = default, Action<CorsPolicyBuilder> corsPolicyBuilderHandler = default)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<CorsAccessorSettingsOptions>)))
{
return services;
}

// 添加跨域配置选项
services.AddConfigurableOptions<CorsAccessorSettingsOptions>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using Furion.DataValidation;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -43,7 +45,7 @@ public static IServiceCollection AddDataValidation<TValidationMessageTypeProvide
services.AddDataValidation(configure);

// 单例注册验证消息提供器
services.AddSingleton<IValidationMessageTypeProvider, TValidationMessageTypeProvider>();
services.TryAddSingleton<IValidationMessageTypeProvider, TValidationMessageTypeProvider>();

return services;
}
Expand All @@ -69,6 +71,12 @@ public static IMvcBuilder AddDataValidation(this IMvcBuilder mvcBuilder, Action<
/// <returns></returns>
public static IServiceCollection AddDataValidation(this IServiceCollection services, Action<DataValidationOptions> configure = null)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<ValidationTypeMessageSettingsOptions>)))
{
return services;
}

// 添加验证配置文件支持
services.AddConfigurableOptions<ValidationTypeMessageSettingsOptions>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -37,6 +39,12 @@ public static IMvcBuilder AddDynamicApiControllers(this IMvcBuilder mvcBuilder)
/// <returns></returns>
public static IServiceCollection AddDynamicApiControllers(this IServiceCollection services)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(MvcActionDescriptorChangeProvider)))
{
return services;
}

var partManager = services.FirstOrDefault(s => s.ServiceType == typeof(ApplicationPartManager))?.ImplementationInstance as ApplicationPartManager
?? throw new InvalidOperationException($"`{nameof(AddDynamicApiControllers)}` must be invoked after `{nameof(MvcServiceCollectionExtensions.AddControllers)}`.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -42,7 +44,7 @@ public static IServiceCollection AddFriendlyException<TErrorCodeTypeProvider>(th
services.AddFriendlyException(configure);

// 单例注册异常状态码提供器
services.AddSingleton<IErrorCodeTypeProvider, TErrorCodeTypeProvider>();
services.TryAddSingleton<IErrorCodeTypeProvider, TErrorCodeTypeProvider>();

return services;
}
Expand All @@ -68,6 +70,12 @@ public static IMvcBuilder AddFriendlyException(this IMvcBuilder mvcBuilder, Acti
/// <returns></returns>
public static IServiceCollection AddFriendlyException(this IServiceCollection services, Action<FriendlyExceptionOptions> configure = null)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<FriendlyExceptionSettingsOptions>)))
{
return services;
}

// 添加友好异常配置文件支持
services.AddConfigurableOptions<FriendlyExceptionSettingsOptions>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ private static void CreateSwaggerDocs(SwaggerGenOptions swaggerGenOptions)
{
foreach (var group in DocumentGroups)
{
if (swaggerGenOptions.SwaggerGeneratorOptions.SwaggerDocs.ContainsKey(group)) continue;

var groupOpenApiInfo = GetGroupOpenApiInfo(group) as OpenApiInfo;
swaggerGenOptions.SwaggerDoc(group, groupOpenApiInfo);
}
Expand Down Expand Up @@ -547,7 +549,8 @@ private static void ConfigureSecurities(SwaggerGenOptions swaggerGenOptions)
foreach (var securityDefinition in _specificationDocumentSettings.SecurityDefinitions)
{
// Id 必须定义
if (string.IsNullOrWhiteSpace(securityDefinition.Id)) continue;
if (string.IsNullOrWhiteSpace(securityDefinition.Id)
|| swaggerGenOptions.SwaggerGeneratorOptions.SecuritySchemes.ContainsKey(securityDefinition.Id)) continue;

// 添加安全定义
var openApiSecurityScheme = securityDefinition as OpenApiSecurityScheme;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Furion;
using Furion.SpecificationDocument;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -35,6 +36,12 @@ public static IMvcBuilder AddSpecificationDocuments(this IMvcBuilder mvcBuilder,
/// <returns>服务集合</returns>
public static IServiceCollection AddSpecificationDocuments(this IServiceCollection services, Action<SwaggerGenOptions> configure = default)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<SchemaGeneratorOptions>)))
{
return services;
}

// 判断是否启用规范化文档
if (App.Settings.InjectSpecificationDocument != true) return services;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Furion.UnifyResult;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -61,6 +62,12 @@ public static IMvcBuilder AddUnifyResult<TUnifyResultProvider>(this IMvcBuilder
public static IServiceCollection AddUnifyResult<TUnifyResultProvider>(this IServiceCollection services)
where TUnifyResultProvider : class, IUnifyResultProvider
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<UnifyResultSettingsOptions>)))
{
return services;
}

// 添加配置
services.AddConfigurableOptions<UnifyResultSettingsOptions>();

Expand Down
2 changes: 1 addition & 1 deletion framework/Furion.Xunit/Furion.Xunit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Furion" Version="4.9.1.31" />
<PackageReference Include="xunit.extensibility.execution" Version="2.6.6" />
<PackageReference Include="xunit.extensibility.execution" Version="2.7.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Furion;
using Furion.CorsAccessor;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection;

Expand All @@ -23,6 +24,12 @@ public static class CorsAccessorServiceCollectionExtensions
/// <returns>服务集合</returns>
public static IServiceCollection AddCorsAccessor(this IServiceCollection services, Action<CorsOptions> corsOptionsHandler = default, Action<CorsPolicyBuilder> corsPolicyBuilderHandler = default)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<CorsAccessorSettingsOptions>)))
{
return services;
}

// 添加跨域配置选项
services.AddConfigurableOptions<CorsAccessorSettingsOptions>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using Furion.DataValidation;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -43,7 +45,7 @@ public static IServiceCollection AddDataValidation<TValidationMessageTypeProvide
services.AddDataValidation(configure);

// 单例注册验证消息提供器
services.AddSingleton<IValidationMessageTypeProvider, TValidationMessageTypeProvider>();
services.TryAddSingleton<IValidationMessageTypeProvider, TValidationMessageTypeProvider>();

return services;
}
Expand All @@ -69,6 +71,12 @@ public static IMvcBuilder AddDataValidation(this IMvcBuilder mvcBuilder, Action<
/// <returns></returns>
public static IServiceCollection AddDataValidation(this IServiceCollection services, Action<DataValidationOptions> configure = null)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<ValidationTypeMessageSettingsOptions>)))
{
return services;
}

// 添加验证配置文件支持
services.AddConfigurableOptions<ValidationTypeMessageSettingsOptions>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -37,6 +39,12 @@ public static IMvcBuilder AddDynamicApiControllers(this IMvcBuilder mvcBuilder)
/// <returns></returns>
public static IServiceCollection AddDynamicApiControllers(this IServiceCollection services)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(MvcActionDescriptorChangeProvider)))
{
return services;
}

var partManager = services.FirstOrDefault(s => s.ServiceType == typeof(ApplicationPartManager))?.ImplementationInstance as ApplicationPartManager
?? throw new InvalidOperationException($"`{nameof(AddDynamicApiControllers)}` must be invoked after `{nameof(MvcServiceCollectionExtensions.AddControllers)}`.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -42,7 +44,7 @@ public static IServiceCollection AddFriendlyException<TErrorCodeTypeProvider>(th
services.AddFriendlyException(configure);

// 单例注册异常状态码提供器
services.AddSingleton<IErrorCodeTypeProvider, TErrorCodeTypeProvider>();
services.TryAddSingleton<IErrorCodeTypeProvider, TErrorCodeTypeProvider>();

return services;
}
Expand All @@ -68,6 +70,12 @@ public static IMvcBuilder AddFriendlyException(this IMvcBuilder mvcBuilder, Acti
/// <returns></returns>
public static IServiceCollection AddFriendlyException(this IServiceCollection services, Action<FriendlyExceptionOptions> configure = null)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<FriendlyExceptionSettingsOptions>)))
{
return services;
}

// 添加友好异常配置文件支持
services.AddConfigurableOptions<FriendlyExceptionSettingsOptions>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ private static void CreateSwaggerDocs(SwaggerGenOptions swaggerGenOptions)
{
foreach (var group in DocumentGroups)
{
if (swaggerGenOptions.SwaggerGeneratorOptions.SwaggerDocs.ContainsKey(group)) continue;

var groupOpenApiInfo = GetGroupOpenApiInfo(group) as OpenApiInfo;
swaggerGenOptions.SwaggerDoc(group, groupOpenApiInfo);
}
Expand Down Expand Up @@ -547,7 +549,8 @@ private static void ConfigureSecurities(SwaggerGenOptions swaggerGenOptions)
foreach (var securityDefinition in _specificationDocumentSettings.SecurityDefinitions)
{
// Id 必须定义
if (string.IsNullOrWhiteSpace(securityDefinition.Id)) continue;
if (string.IsNullOrWhiteSpace(securityDefinition.Id)
|| swaggerGenOptions.SwaggerGeneratorOptions.SecuritySchemes.ContainsKey(securityDefinition.Id)) continue;

// 添加安全定义
var openApiSecurityScheme = securityDefinition as OpenApiSecurityScheme;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Furion;
using Furion.SpecificationDocument;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -35,6 +36,12 @@ public static IMvcBuilder AddSpecificationDocuments(this IMvcBuilder mvcBuilder,
/// <returns>服务集合</returns>
public static IServiceCollection AddSpecificationDocuments(this IServiceCollection services, Action<SwaggerGenOptions> configure = default)
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<SchemaGeneratorOptions>)))
{
return services;
}

// 判断是否启用规范化文档
if (App.Settings.InjectSpecificationDocument != true) return services;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Furion.UnifyResult;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -61,6 +62,12 @@ public static IMvcBuilder AddUnifyResult<TUnifyResultProvider>(this IMvcBuilder
public static IServiceCollection AddUnifyResult<TUnifyResultProvider>(this IServiceCollection services)
where TUnifyResultProvider : class, IUnifyResultProvider
{
// 解决服务重复注册问题
if (services.Any(u => u.ServiceType == typeof(IConfigureOptions<UnifyResultSettingsOptions>)))
{
return services;
}

// 添加配置
services.AddConfigurableOptions<UnifyResultSettingsOptions>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.9.1.31" />
<PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.9.1.31" />
<PackageReference Include="Furion.Pure" Version="4.9.1.31" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.141" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.142" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.9.1.31" />
<PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.9.1.31" />
<PackageReference Include="Furion.Pure" Version="4.9.1.31" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.141" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.142" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Furion.Pure" Version="4.9.1.31" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.141" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.142" />
<PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.9.1.31" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 1651013

Please sign in to comment.