@@ -8,8 +8,12 @@ namespace DotNetCoreRpc.Server
88{
99 public class RpcServerOptions
1010 {
11- private readonly IDictionary < string , Type > _types = new Dictionary < string , Type > ( ) ;
11+ private readonly IDictionary < string , Type > _serviceTypes = new Dictionary < string , Type > ( ) ;
12+ private readonly HashSet < Type > _serviceTypeSet = new HashSet < Type > ( ) ;
13+ private readonly HashSet < string > _serviceNameSet = new HashSet < string > ( ) ;
14+ private readonly HashSet < string > _serviceNameSpaceSet = new HashSet < string > ( ) ;
1215 private readonly IList < Type > _filterTypes = new List < Type > ( ) ;
16+
1317 private readonly IServiceCollection _services ;
1418
1519 public RpcServerOptions ( IServiceCollection services )
@@ -26,44 +30,59 @@ public RpcServerOptions AddFilter<RpcFilterAttribute>()
2630 public RpcServerOptions AddService < TService > ( )
2731 where TService : class
2832 {
29- Type serviceType = typeof ( TService ) ;
30- _types . TryAdd ( serviceType . FullName , serviceType ) ;
33+ _serviceTypeSet . Add ( typeof ( TService ) ) ;
3134 return this ;
3235 }
3336
3437 public RpcServerOptions AddService ( string serviceName )
3538 {
36- foreach ( var service in _services )
37- {
38- if ( serviceName . StartsWith ( "*" )
39- && service . ServiceType . Name . EndsWith ( serviceName . Substring ( 1 ) ) )
40- {
41- _types . TryAdd ( service . ServiceType . FullName , service . ServiceType ) ;
42- continue ;
43- }
44- if ( service . ServiceType . Name == serviceName )
45- {
46- _types . TryAdd ( service . ServiceType . FullName , service . ServiceType ) ;
47- }
48- }
39+ _serviceNameSet . Add ( serviceName ) ;
4940 return this ;
5041 }
5142
5243 public RpcServerOptions AddNameSpace ( string nameSpace )
5344 {
54- foreach ( var service in _services )
45+ _serviceNameSpaceSet . Add ( nameSpace ) ;
46+ return this ;
47+ }
48+
49+ public IDictionary < string , Type > GetRegisterTypes ( )
50+ {
51+ foreach ( var serviceType in _serviceTypeSet )
5552 {
56- if ( service . ServiceType . Namespace == nameSpace )
53+ _serviceTypes . TryAdd ( serviceType . FullName , serviceType ) ;
54+ }
55+
56+ foreach ( var serviceName in _serviceNameSet )
57+ {
58+ string subServiceName = serviceName [ 1 ..] ;
59+ foreach ( var service in _services )
5760 {
58- _types . TryAdd ( service . ServiceType . FullName , service . ServiceType ) ;
61+ if ( serviceName . StartsWith ( "*" ) && service . ServiceType . Name . EndsWith ( subServiceName ) )
62+ {
63+ _serviceTypes . TryAdd ( service . ServiceType . FullName , service . ServiceType ) ;
64+ continue ;
65+ }
66+
67+ if ( service . ServiceType . Name == serviceName )
68+ {
69+ _serviceTypes . TryAdd ( service . ServiceType . FullName , service . ServiceType ) ;
70+ }
5971 }
6072 }
61- return this ;
62- }
6373
64- public IDictionary < string , Type > GetTypes ( )
65- {
66- return _types . ToDictionary ( i=> i . Key , i=> i . Value ) ;
74+ foreach ( var nameSpace in _serviceNameSpaceSet )
75+ {
76+ foreach ( var service in _services )
77+ {
78+ if ( service . ServiceType . Namespace == nameSpace )
79+ {
80+ _serviceTypes . TryAdd ( service . ServiceType . FullName , service . ServiceType ) ;
81+ }
82+ }
83+ }
84+
85+ return _serviceTypes . ToDictionary ( i => i . Key , i => i . Value ) ;
6786 }
6887
6988 public IEnumerable < Type > GetFilterTypes ( )
0 commit comments