Skip to content

Commit 5b06942

Browse files
authored
fix:Compatible with net8.0 IsKeyedService (#706)
* fix:Compatible with net8.0 IsKeyedService * fix:格式 * fix:优化写法去除测试冗余数据
1 parent 73c3083 commit 5b06942

File tree

22 files changed

+149
-13
lines changed

22 files changed

+149
-13
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<FluentValidationAspNetCorePackageVersion>11.1.2</FluentValidationAspNetCorePackageVersion>
1919
<StackExchangeRedisPackageVersion>2.2.4</StackExchangeRedisPackageVersion>
2020
<NESTPackageVersion>7.17.4</NESTPackageVersion>
21-
<IdentityPackageVersion>6.15.0</IdentityPackageVersion>
21+
<IdentityPackageVersion>7.5.0</IdentityPackageVersion>
2222
<IdentityModelPackageVersion>6.0.0</IdentityModelPackageVersion>
2323

2424
<OpenTelemetryVersion>1.5.1</OpenTelemetryVersion>

src/BuildingBlocks/Isolation/Masa.BuildingBlocks.Isolation/Extensions/ServiceCollectionExtensions.cs

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

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

20+
#if (NET8_0_OR_GREATER)
21+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(IsolationProvider)))
22+
return;
23+
#else
2024
if (services.Any(service => service.ImplementationType == typeof(IsolationProvider)))
2125
return;
26+
#endif
2227

2328
services.AddSingleton<IsolationProvider>();
2429

src/Contrib/Configuration/ConfigurationApi/Masa.Contrib.Configuration.ConfigurationApi.Dcc/MasaConfigurationExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ public static IMasaConfigurationBuilder UseDcc(
2525
Action<CallerBuilder>? action = null)
2626
{
2727
var services = builder.Services;
28+
29+
#if (NET8_0_OR_GREATER)
30+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(DccConfigurationProvider)))
31+
return builder;
32+
#else
2833
if (services.Any(service => service.ImplementationType == typeof(DccConfigurationProvider)))
2934
return builder;
35+
#endif
3036

3137
services.AddSingleton<DccConfigurationProvider>();
3238

src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/ServiceCollectionExtensions.cs

+12
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ public static class ServiceCollectionExtensions
99
{
1010
public static IServiceCollection InitializeAppConfiguration(this IServiceCollection services)
1111
{
12+
13+
#if (NET8_0_OR_GREATER)
14+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(InitializeAppConfigurationProvider)))
15+
return services;
16+
#else
1217
if (services.Any(service => service.ImplementationType == typeof(InitializeAppConfigurationProvider)))
1318
return services;
19+
#endif
1420

1521
services.AddSingleton<InitializeAppConfigurationProvider>();
1622

@@ -127,8 +133,14 @@ private static IConfigurationRoot CreateMasaConfiguration(
127133
IEnumerable<IConfigurationSource> originalConfigurationSources,
128134
Action<ConfigurationOptions>? action)
129135
{
136+
137+
#if (NET8_0_OR_GREATER)
138+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(MasaConfigurationProvider)))
139+
return new ConfigurationBuilder().Build();
140+
#else
130141
if (services.Any(service => service.ImplementationType == typeof(MasaConfigurationProvider)))
131142
return new ConfigurationBuilder().Build();
143+
#endif
132144

133145
services.AddSingleton<MasaConfigurationProvider>();
134146
services.AddOptions();

src/Contrib/Data/IdGenerator/NormalGuid/Masa.Contrib.Data.IdGenerator.NormalGuid/Extensions/IdGeneratorOptionsExtensions.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) MASA Stack All rights reserved.
1+
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

44
// ReSharper disable once CheckNamespace
@@ -9,7 +9,12 @@ public static class IdGeneratorOptionsExtensions
99
{
1010
public static void UseSimpleGuidGenerator(this IdGeneratorOptions options)
1111
{
12+
13+
#if (NET8_0_OR_GREATER)
14+
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(SimpleGuidGeneratorProvider))) return;
15+
#else
1216
if (options.Services.Any(service => service.ImplementationType == typeof(SimpleGuidGeneratorProvider))) return;
17+
#endif
1318

1419
options.Services.AddSingleton<SimpleGuidGeneratorProvider>();
1520

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

21-
#pragma warning disable S2094
26+
#pragma warning disable S2094
2227
private sealed class SimpleGuidGeneratorProvider
2328
{
2429
}
25-
#pragma warning restore S2094
30+
#pragma warning restore S2094
2631
}

src/Contrib/Data/IdGenerator/SequentialGuid/Masa.Contrib.Data.IdGenerator.SequentialGuid/Extensions/IdGeneratorOptionsExtensions.cs

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

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

1313
public static void UseSequentialGuidGenerator(this IdGeneratorOptions options, SequentialGuidType guidType)
1414
{
15+
16+
#if (NET8_0_OR_GREATER)
17+
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(SequentialGuidGeneratorProvider))) return;
18+
#else
1519
if (options.Services.Any(service => service.ImplementationType == typeof(SequentialGuidGeneratorProvider))) return;
20+
#endif
1621

1722
options.Services.AddSingleton<SequentialGuidGeneratorProvider>();
1823

src/Contrib/Data/IdGenerator/Snowflake/Masa.Contrib.Data.IdGenerator.Snowflake/Extensions/IdGeneratorOptionsExtensions.cs

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

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

1515
public static void UseSnowflakeGenerator(this IdGeneratorOptions options, Action<SnowflakeGeneratorOptions>? action)
1616
{
17+
18+
#if (NET8_0_OR_GREATER)
19+
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(SnowflakeGeneratorProvider))) return;
20+
#else
1721
if (options.Services.Any(service => service.ImplementationType == typeof(SnowflakeGeneratorProvider))) return;
22+
#endif
1823

1924
options.Services.AddSingleton<SnowflakeGeneratorProvider>();
2025

src/Contrib/Data/Mapping/Masa.Contrib.Data.Mapping.Mapster/ServiceCollectionExtensions.cs

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

44
// ReSharper disable once CheckNamespace
5+
56
namespace Microsoft.Extensions.DependencyInjection;
67

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

1819
public static IServiceCollection AddMapster(this IServiceCollection services, MapOptions mapOptions)
1920
{
21+
22+
#if (NET8_0_OR_GREATER)
23+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(MappingProvider)))
24+
return services;
25+
#else
2026
if (services.Any(service => service.ImplementationType == typeof(MappingProvider)))
2127
return services;
28+
#endif
2229

2330
services.AddSingleton<MappingProvider>();
2431

src/Contrib/Data/Orm/EFCore/Masa.Contrib.Data.EFCore/Extensions/ServiceCollectionExtensions.cs

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

4+
using Microsoft.Extensions.Options;
5+
46
[assembly: InternalsVisibleTo("Masa.Contrib.Data.EFCore.Tests")]
57
[assembly: InternalsVisibleTo("Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation")]
68

@@ -36,8 +38,14 @@ private static IServiceCollection AddCoreServices<TDbContextImplementation>(
3638
ServiceLifetime optionsLifetime)
3739
where TDbContextImplementation : DefaultMasaDbContext, IMasaDbContext
3840
{
41+
42+
#if (NET8_0_OR_GREATER)
43+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(MasaDbContextProvider<TDbContextImplementation>)))
44+
return services;
45+
#else
3946
if (services.Any(service => service.ImplementationType == typeof(MasaDbContextProvider<TDbContextImplementation>)))
4047
return services;
48+
#endif
4149

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

src/Contrib/Data/UoW/Masa.Contrib.Data.UoW.EFCore/Extensions/ServiceCollectionExtensions.cs

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

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

20+
#if (NET8_0_OR_GREATER)
21+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(UoWProvider)))
22+
return services;
23+
#else
2024
if (services.Any(service => service.ImplementationType == typeof(UoWProvider)))
2125
return services;
26+
#endif
2227

2328
services.AddSingleton<UoWProvider>();
2429
services.TryAddScoped<IUnitOfWorkAccessor, UnitOfWorkAccessor>();

src/Contrib/Ddd/Domain/Masa.Contrib.Ddd.Domain.Repository.EFCore/DispatcherOptionsExtensions.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ public static IDomainEventOptions UseRepository<TDbContext>(
1818
IEnumerable<Type>? entityTypes)
1919
where TDbContext : DbContext, IMasaDbContext
2020
{
21-
MasaArgumentException.ThrowIfNull(options.Services);
2221

22+
MasaArgumentException.ThrowIfNull(options.Services);
23+
#if (NET8_0_OR_GREATER)
24+
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(RepositoryProvider)))
25+
return options;
26+
#else
2327
if (options.Services.Any(service => service.ImplementationType == typeof(RepositoryProvider)))
2428
return options;
29+
#endif
2530

2631
options.Services.AddSingleton<RepositoryProvider>();
2732

src/Contrib/Ddd/Domain/Masa.Contrib.Ddd.Domain/ServiceCollectionExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ public static IServiceCollection AddDomainEventBus(
1919
IEnumerable<Assembly> assemblies,
2020
Action<DispatcherOptions>? options = null)
2121
{
22+
23+
#if (NET8_0_OR_GREATER)
24+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(DomainEventBusProvider)))
25+
return services;
26+
#else
2227
if (services.Any(service => service.ImplementationType == typeof(DomainEventBusProvider)))
2328
return services;
29+
#endif
2430

2531
services.AddSingleton<DomainEventBusProvider>();
2632

src/Contrib/Development/Masa.Contrib.Development.DaprStarter.AspNetCore/ServiceCollectionExtensions.cs

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

44
// ReSharper disable once CheckNamespace
@@ -43,8 +43,14 @@ public static IServiceCollection AddDaprStarter(
4343

4444
private static IServiceCollection AddDaprStarter(this IServiceCollection services, Action action, bool isDelay = true)
4545
{
46+
47+
#if (NET8_0_OR_GREATER)
48+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(DaprService)))
49+
return services;
50+
#else
4651
if (services.Any(service => service.ImplementationType == typeof(DaprService)))
4752
return services;
53+
#endif
4854

4955
services.AddSingleton<DaprService>();
5056

src/Contrib/Development/Masa.Contrib.Development.DaprStarter/ServiceCollectionExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ public static IServiceCollection AddDaprStarterCore(this IServiceCollection serv
2727

2828
private static IServiceCollection AddDaprStarter(this IServiceCollection services)
2929
{
30+
31+
#if (NET8_0_OR_GREATER)
32+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(DaprService)))
33+
return services;
34+
#else
3035
if (services.Any(service => service.ImplementationType == typeof(DaprService)))
3136
return services;
37+
#endif
3238

3339
services.AddSingleton<DaprService>();
3440

src/Contrib/Dispatcher/IntegrationEvents/Masa.Contrib.Dispatcher.IntegrationEvents.Dapr/Extensions/ServiceCollectionExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ internal static IServiceCollection TryAddDaprEventBus<TIntegrationEventLogServic
3333
Action<DaprClientBuilder>? builder = null)
3434
where TIntegrationEventLogService : class, IIntegrationEventLogService
3535
{
36+
37+
#if (NET8_0_OR_GREATER)
38+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(IntegrationEventBusProvider)))
39+
return services;
40+
#else
3641
if (services.Any(service => service.ImplementationType == typeof(IntegrationEventBusProvider)))
3742
return services;
43+
#endif
3844

3945
services.AddSingleton<IntegrationEventBusProvider>();
4046

src/Contrib/Dispatcher/IntegrationEvents/Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EFCore/IntegrationEventOptionsExtensions.cs

+4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ public static IIntegrationEventOptions UseEventLog<TDbContext>(
2121
{
2222
MasaArgumentException.ThrowIfNull(options.Services);
2323

24+
#if (NET8_0_OR_GREATER)
25+
if (options.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(EventLogProvider))) return options;
26+
#else
2427
if (options.Services.Any(service => service.ImplementationType == typeof(EventLogProvider))) return options;
28+
#endif
2529

2630
options.Services.AddSingleton<EventLogProvider>();
2731

src/Contrib/Dispatcher/IntegrationEvents/Masa.Contrib.Dispatcher.IntegrationEvents/Extensions/ServiceCollectionExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ internal static IServiceCollection TryAddIntegrationEventBus(
4949
Action<IntegrationEventOptions>? options,
5050
Action? action = null)
5151
{
52+
53+
#if (NET8_0_OR_GREATER)
54+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(IntegrationEventBusProvider)))
55+
return services;
56+
#else
5257
if (services.Any(service => service.ImplementationType == typeof(IntegrationEventBusProvider)))
5358
return services;
59+
#endif
5460

5561
services.AddSingleton<IntegrationEventBusProvider>();
5662

src/Contrib/Dispatcher/Masa.Contrib.Dispatcher.Events/Extensions/ServiceCollectionExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ public static IServiceCollection AddEventBus(
2424
ServiceLifetime lifetime,
2525
Action<EventBusBuilder>? eventBusBuilder = null)
2626
{
27+
28+
#if (NET8_0_OR_GREATER)
29+
if (services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(EventBusProvider)))
30+
return services;
31+
#else
2732
if (services.Any(service => service.ImplementationType == typeof(EventBusProvider)))
2833
return services;
34+
#endif
2935

3036
services.AddSingleton<EventBusProvider>();
3137

src/Contrib/Isolation/Masa.Contrib.Isolation.MultiEnvironment/IsolationBuilderExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ public static IIsolationBuilder UseMultiEnvironment(
1818
string? environmentName,
1919
List<IParserProvider>? parserProviders = null)
2020
{
21+
22+
#if (NET8_0_OR_GREATER)
23+
if (isolationBuilder.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(EnvironmentProvider)))
24+
return isolationBuilder;
25+
#else
2126
if (isolationBuilder.Services.Any(service => service.ImplementationType == typeof(EnvironmentProvider)))
2227
return isolationBuilder;
28+
#endif
2329

2430
isolationBuilder.Services.AddSingleton<EnvironmentProvider>();
2531

src/Contrib/Isolation/Masa.Contrib.Isolation.MultiTenant/IsolationBuilderExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ public static IIsolationBuilder UseMultiTenant(
2121
string? multiTenantName,
2222
List<IParserProvider>? parserProviders)
2323
{
24+
25+
#if (NET8_0_OR_GREATER)
26+
if (isolationBuilder.Services.Any(service => service.IsKeyedService == false && service.ImplementationType == typeof(MultiTenantProvider)))
27+
return isolationBuilder;
28+
#else
2429
if (isolationBuilder.Services.Any(service => service.ImplementationType == typeof(MultiTenantProvider)))
2530
return isolationBuilder;
31+
#endif
2632

2733
isolationBuilder.Services.AddSingleton<MultiTenantProvider>();
2834

0 commit comments

Comments
 (0)