File tree 6 files changed +130
-2
lines changed
Masa.Contrib.Service.MinimalAPIs
Tests/Masa.Contrib.Service.MinimalAPIs.Tests
6 files changed +130
-2
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,41 @@ public static WebApplication AddServices(
62
62
return app ;
63
63
}
64
64
65
+ public static IServiceCollection AddMasaMinimalAPIs (
66
+ this IServiceCollection services ,
67
+ Action < ServiceGlobalRouteOptions > ? action = null )
68
+ {
69
+ if ( services . All ( service => service . ImplementationType != typeof ( MinimalApisMarkerService ) ) )
70
+ {
71
+ services . AddSingleton < MinimalApisMarkerService > ( ) ;
72
+
73
+ services . AddHttpContextAccessor ( ) ;
74
+ if ( action == null )
75
+ {
76
+ services . Configure < ServiceGlobalRouteOptions > ( _ =>
77
+ {
78
+
79
+ } ) ;
80
+ }
81
+ else
82
+ {
83
+ services . Configure ( action ) ;
84
+ }
85
+
86
+ MasaApp . TrySetServiceCollection ( services ) ;
87
+ var serviceProvider = services . BuildServiceProvider ( ) ;
88
+ var serviceMapOptions = serviceProvider . GetRequiredService < IOptions < ServiceGlobalRouteOptions > > ( ) . Value ;
89
+ var serviceTypes = TypeHelper . GetServiceTypes < ServiceBase > ( serviceMapOptions . Assemblies . ToArray ( ) ) ;
90
+ foreach ( var serviceType in serviceTypes )
91
+ {
92
+ GlobalMinimalApiOptions . AddService ( serviceType ) ;
93
+ services . AddSingleton ( serviceType ) ;
94
+ }
95
+ }
96
+
97
+ return services ;
98
+ }
99
+
65
100
private sealed class MinimalApisMarkerService
66
101
{
67
102
Original file line number Diff line number Diff line change
1
+ // Copyright (c) MASA Stack All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3
+
4
+ // ReSharper disable once CheckNamespace
5
+
6
+ namespace Microsoft . AspNetCore . Builder ;
7
+
8
+ public static class WebApplicationExtensions
9
+ {
10
+ public static void MapMasaMinimalAPIs ( this WebApplication webApplication )
11
+ {
12
+ MasaApp . Build ( webApplication . Services ) ;
13
+ GlobalMinimalApiOptions . WebApplication = webApplication ;
14
+
15
+ var serviceMapOptions = webApplication . Services . GetRequiredService < IOptions < ServiceGlobalRouteOptions > > ( ) . Value ;
16
+ foreach ( var serviceType in GlobalMinimalApiOptions . ServiceTypes )
17
+ {
18
+ var serviceInstance = ( ServiceBase ) webApplication . Services . GetRequiredService ( serviceType ) ;
19
+ if ( serviceInstance . RouteOptions . DisableAutoMapRoute ?? serviceMapOptions . DisableAutoMapRoute ?? false )
20
+ return ;
21
+
22
+ serviceInstance . AutoMapRoute ( serviceMapOptions , serviceMapOptions . Pluralization ) ;
23
+ }
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright (c) MASA Stack All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3
+
4
+ // ReSharper disable once CheckNamespace
5
+
6
+ namespace Masa . Contrib . Service . MinimalAPIs ;
7
+
8
+ [ System . Diagnostics . CodeAnalysis . ExcludeFromCodeCoverage ]
9
+ internal static class GlobalMinimalApiOptions
10
+ {
11
+ #pragma warning disable S2223
12
+ public static WebApplication ? WebApplication ;
13
+ #pragma warning restore S2223
14
+ public static List < Type > ServiceTypes { get ; private set ; } = new ( ) ;
15
+
16
+ public static void AddService ( Type serviceType )
17
+ {
18
+ if ( ServiceTypes . Contains ( serviceType ) )
19
+ return ;
20
+
21
+ ServiceTypes . Add ( serviceType ) ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright (c) MASA Stack All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3
+
4
+ // ReSharper disable once CheckNamespace
5
+
6
+ namespace Masa . Contrib . Service . MinimalAPIs ;
7
+
8
+ [ System . Diagnostics . CodeAnalysis . ExcludeFromCodeCoverage ]
9
+ internal static class TypeHelper
10
+ {
11
+ public static IEnumerable < Type > GetServiceTypes < TService > ( params Assembly [ ] assemblies )
12
+ where TService : class
13
+ => from type in assemblies . SelectMany ( assembly => assembly . GetTypes ( ) )
14
+ where ! type . IsAbstract && BaseOf < TService > ( type )
15
+ select type ;
16
+
17
+ private static bool BaseOf < T > ( Type type )
18
+ {
19
+ if ( type . BaseType == typeof ( T ) ) return true ;
20
+
21
+ return type . BaseType != null && BaseOf < T > ( type . BaseType ) ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change 1
1
// Copyright (c) MASA Stack All rights reserved.
2
2
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3
3
4
+ // ReSharper disable once CheckNamespace
5
+
4
6
namespace Microsoft . AspNetCore . Builder ;
5
7
6
8
public abstract class ServiceBase : IService
7
9
{
8
- public WebApplication App => MasaApp . GetRequiredService < WebApplication > ( ) ;
10
+ private WebApplication ? _webApplication ;
11
+
12
+ public WebApplication App => _webApplication ??= GlobalMinimalApiOptions . WebApplication ?? MasaApp . GetRequiredService < WebApplication > ( ) ;
9
13
10
14
public string BaseUri { get ; init ; }
11
15
Original file line number Diff line number Diff line change @@ -27,7 +27,8 @@ public void TestAddMultiServices()
27
27
public void AddService ( )
28
28
{
29
29
var app = _builder . AddServices ( ) ;
30
- Assert . IsTrue ( _builder . Services . Any ( service => service . ServiceType == typeof ( CustomService ) && service . Lifetime == ServiceLifetime . Scoped ) ) ;
30
+ Assert . IsTrue ( _builder . Services . Any ( service
31
+ => service . ServiceType == typeof ( CustomService ) && service . Lifetime == ServiceLifetime . Scoped ) ) ;
31
32
32
33
var servicePrvider = _builder . Services . BuildServiceProvider ( ) ;
33
34
var customService = servicePrvider . GetService < CustomService > ( ) ;
@@ -46,4 +47,21 @@ public void AddService()
46
47
47
48
Assert . IsTrue ( newCustomService . GetTest2 ( ) == 1 ) ;
48
49
}
50
+
51
+ [ TestMethod ]
52
+ public void TestMapMasaMinimalAPIs ( )
53
+ {
54
+ _builder . Services . AddMasaMinimalAPIs ( ) ;
55
+ Assert . IsTrue ( _builder . Services . Any < CatalogService > ( ) ) ;
56
+ Assert . IsTrue ( _builder . Services . Any < CustomService > ( ) ) ;
57
+ Assert . IsTrue ( _builder . Services . Any < GoodsService > ( ) ) ;
58
+ Assert . IsTrue ( _builder . Services . Any < UserService > ( ) ) ;
59
+
60
+ var app = _builder . Build ( ) ;
61
+
62
+ app . MapMasaMinimalAPIs ( ) ;
63
+
64
+ var service = ( ServiceBase ) app . Services . GetRequiredService ( typeof ( CatalogService ) ) ;
65
+ Assert . AreEqual ( app , service . App ) ;
66
+ }
49
67
}
You can’t perform that action at this time.
0 commit comments