Skip to content

Commit

Permalink
fix:Compatible with net8.0 IsKeyedService (#706)
Browse files Browse the repository at this point in the history
* fix:Compatible with net8.0 IsKeyedService

* fix:格式

* fix:优化写法去除测试冗余数据
  • Loading branch information
duiapro authored Mar 28, 2024
1 parent 73c3083 commit 5b06942
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<FluentValidationAspNetCorePackageVersion>11.1.2</FluentValidationAspNetCorePackageVersion>
<StackExchangeRedisPackageVersion>2.2.4</StackExchangeRedisPackageVersion>
<NESTPackageVersion>7.17.4</NESTPackageVersion>
<IdentityPackageVersion>6.15.0</IdentityPackageVersion>
<IdentityPackageVersion>7.5.0</IdentityPackageVersion>
<IdentityModelPackageVersion>6.0.0</IdentityModelPackageVersion>

<OpenTelemetryVersion>1.5.1</OpenTelemetryVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

[assembly: InternalsVisibleTo("Masa.Contrib.Storage.ObjectStorage.Aliyun.Tests.Isolation")]
Expand All @@ -17,8 +17,13 @@ public static void AddIsolation(
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(isolationBuilder);

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(IsolationProvider)))
return;
#else
if (services.Any(service => service.ImplementationType == typeof(IsolationProvider)))
return;
#endif

services.AddSingleton<IsolationProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ public static IMasaConfigurationBuilder UseDcc(
Action<CallerBuilder>? action = null)
{
var services = builder.Services;

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(DccConfigurationProvider)))
return builder;
#else
if (services.Any(service => service.ImplementationType == typeof(DccConfigurationProvider)))
return builder;
#endif

services.AddSingleton<DccConfigurationProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection InitializeAppConfiguration(this IServiceCollection services)
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(InitializeAppConfigurationProvider)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(InitializeAppConfigurationProvider)))
return services;
#endif

services.AddSingleton<InitializeAppConfigurationProvider>();

Expand Down Expand Up @@ -127,8 +133,14 @@ private static IConfigurationRoot CreateMasaConfiguration(
IEnumerable<IConfigurationSource> originalConfigurationSources,
Action<ConfigurationOptions>? action)
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(MasaConfigurationProvider)))
return new ConfigurationBuilder().Build();
#else
if (services.Any(service => service.ImplementationType == typeof(MasaConfigurationProvider)))
return new ConfigurationBuilder().Build();
#endif

services.AddSingleton<MasaConfigurationProvider>();
services.AddOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace
Expand All @@ -9,7 +9,12 @@ public static class IdGeneratorOptionsExtensions
{
public static void UseSimpleGuidGenerator(this IdGeneratorOptions options)
{

#if (NET8_0_OR_GREATER)
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(SimpleGuidGeneratorProvider))) return;
#else
if (options.Services.Any(service => service.ImplementationType == typeof(SimpleGuidGeneratorProvider))) return;
#endif

options.Services.AddSingleton<SimpleGuidGeneratorProvider>();

Expand All @@ -18,9 +23,9 @@ public static void UseSimpleGuidGenerator(this IdGeneratorOptions options)
options.Services.TryAddSingleton<IIdGenerator>(serviceProvider => serviceProvider.GetRequiredService<IGuidGenerator>());
}

#pragma warning disable S2094
#pragma warning disable S2094
private sealed class SimpleGuidGeneratorProvider
{
}
#pragma warning restore S2094
#pragma warning restore S2094
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace
Expand All @@ -12,7 +12,12 @@ public static void UseSequentialGuidGenerator(this IdGeneratorOptions options)

public static void UseSequentialGuidGenerator(this IdGeneratorOptions options, SequentialGuidType guidType)
{

#if (NET8_0_OR_GREATER)
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(SequentialGuidGeneratorProvider))) return;
#else
if (options.Services.Any(service => service.ImplementationType == typeof(SequentialGuidGeneratorProvider))) return;
#endif

options.Services.AddSingleton<SequentialGuidGeneratorProvider>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

[assembly: InternalsVisibleTo("Masa.Contrib.Data.IdGenerator.Snowflake.Tests")]
Expand All @@ -14,7 +14,12 @@ public static void UseSnowflakeGenerator(this IdGeneratorOptions options)

public static void UseSnowflakeGenerator(this IdGeneratorOptions options, Action<SnowflakeGeneratorOptions>? action)
{

#if (NET8_0_OR_GREATER)
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(SnowflakeGeneratorProvider))) return;
#else
if (options.Services.Any(service => service.ImplementationType == typeof(SnowflakeGeneratorProvider))) return;
#endif

options.Services.AddSingleton<SnowflakeGeneratorProvider>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
Expand All @@ -17,8 +18,14 @@ public static IServiceCollection AddMapster(this IServiceCollection services, Ma

public static IServiceCollection AddMapster(this IServiceCollection services, MapOptions mapOptions)
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(MappingProvider)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(MappingProvider)))
return services;
#endif

services.AddSingleton<MappingProvider>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Microsoft.Extensions.Options;

[assembly: InternalsVisibleTo("Masa.Contrib.Data.EFCore.Tests")]
[assembly: InternalsVisibleTo("Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation")]

Expand Down Expand Up @@ -36,8 +38,14 @@ private static IServiceCollection AddCoreServices<TDbContextImplementation>(
ServiceLifetime optionsLifetime)
where TDbContextImplementation : DefaultMasaDbContext, IMasaDbContext
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(MasaDbContextProvider<TDbContextImplementation>)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(MasaDbContextProvider<TDbContextImplementation>)))
return services;
#endif

services.AddSingleton<MasaDbContextProvider<TDbContextImplementation>>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace
Expand All @@ -17,8 +17,13 @@ public static IServiceCollection UseUoW<TDbContext>(
{
MasaArgumentException.ThrowIfNull(services, paramName);

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(UoWProvider)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(UoWProvider)))
return services;
#endif

services.AddSingleton<UoWProvider>();
services.TryAddScoped<IUnitOfWorkAccessor, UnitOfWorkAccessor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public static IDomainEventOptions UseRepository<TDbContext>(
IEnumerable<Type>? entityTypes)
where TDbContext : DbContext, IMasaDbContext
{
MasaArgumentException.ThrowIfNull(options.Services);

MasaArgumentException.ThrowIfNull(options.Services);
#if (NET8_0_OR_GREATER)
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(RepositoryProvider)))
return options;
#else
if (options.Services.Any(service => service.ImplementationType == typeof(RepositoryProvider)))
return options;
#endif

options.Services.AddSingleton<RepositoryProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ public static IServiceCollection AddDomainEventBus(
IEnumerable<Assembly> assemblies,
Action<DispatcherOptions>? options = null)
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(DomainEventBusProvider)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(DomainEventBusProvider)))
return services;
#endif

services.AddSingleton<DomainEventBusProvider>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -43,8 +43,14 @@ public static IServiceCollection AddDaprStarter(

private static IServiceCollection AddDaprStarter(this IServiceCollection services, Action action, bool isDelay = true)
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(DaprService)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(DaprService)))
return services;
#endif

services.AddSingleton<DaprService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ public static IServiceCollection AddDaprStarterCore(this IServiceCollection serv

private static IServiceCollection AddDaprStarter(this IServiceCollection services)
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(DaprService)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(DaprService)))
return services;
#endif

services.AddSingleton<DaprService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ internal static IServiceCollection TryAddDaprEventBus<TIntegrationEventLogServic
Action<DaprClientBuilder>? builder = null)
where TIntegrationEventLogService : class, IIntegrationEventLogService
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(IntegrationEventBusProvider)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(IntegrationEventBusProvider)))
return services;
#endif

services.AddSingleton<IntegrationEventBusProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public static IIntegrationEventOptions UseEventLog<TDbContext>(
{
MasaArgumentException.ThrowIfNull(options.Services);

#if (NET8_0_OR_GREATER)
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(EventLogProvider))) return options;
#else
if (options.Services.Any(service => service.ImplementationType == typeof(EventLogProvider))) return options;
#endif

options.Services.AddSingleton<EventLogProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ internal static IServiceCollection TryAddIntegrationEventBus(
Action<IntegrationEventOptions>? options,
Action? action = null)
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(IntegrationEventBusProvider)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(IntegrationEventBusProvider)))
return services;
#endif

services.AddSingleton<IntegrationEventBusProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ public static IServiceCollection AddEventBus(
ServiceLifetime lifetime,
Action<EventBusBuilder>? eventBusBuilder = null)
{

#if (NET8_0_OR_GREATER)
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(EventBusProvider)))
return services;
#else
if (services.Any(service => service.ImplementationType == typeof(EventBusProvider)))
return services;
#endif

services.AddSingleton<EventBusProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ public static IIsolationBuilder UseMultiEnvironment(
string? environmentName,
List<IParserProvider>? parserProviders = null)
{

#if (NET8_0_OR_GREATER)
if (isolationBuilder.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(EnvironmentProvider)))
return isolationBuilder;
#else
if (isolationBuilder.Services.Any(service => service.ImplementationType == typeof(EnvironmentProvider)))
return isolationBuilder;
#endif

isolationBuilder.Services.AddSingleton<EnvironmentProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ public static IIsolationBuilder UseMultiTenant(
string? multiTenantName,
List<IParserProvider>? parserProviders)
{

#if (NET8_0_OR_GREATER)
if (isolationBuilder.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(MultiTenantProvider)))
return isolationBuilder;
#else
if (isolationBuilder.Services.Any(service => service.ImplementationType == typeof(MultiTenantProvider)))
return isolationBuilder;
#endif

isolationBuilder.Services.AddSingleton<MultiTenantProvider>();

Expand Down
Loading

0 comments on commit 5b06942

Please sign in to comment.