@@ -5,39 +5,36 @@ namespace LocalStack.Client.Extensions;
55
66public sealed class AwsClientFactoryWrapper : IAwsClientFactoryWrapper
77{
8- private static readonly string ClientFactoryFullName = "Amazon.Extensions.NETCore.Setup.ClientFactory" ;
8+ private static readonly string ClientFactoryGenericTypeName = "Amazon.Extensions.NETCore.Setup.ClientFactory`1 " ;
99 private static readonly string CreateServiceClientMethodName = "CreateServiceClient" ;
1010
1111 public AmazonServiceClient CreateServiceClient < TClient > ( IServiceProvider provider , AWSOptions ? awsOptions ) where TClient : IAmazonService
1212 {
13- Type ? clientFactoryType = typeof ( ConfigurationException ) . Assembly . GetType ( ClientFactoryFullName ) ;
13+ Type ? genericFactoryType = typeof ( ConfigurationException ) . Assembly . GetType ( ClientFactoryGenericTypeName ) ;
1414
15- if ( clientFactoryType == null )
15+ if ( genericFactoryType == null )
1616 {
17- throw new LocalStackClientConfigurationException ( $ "Failed to find internal ClientFactory in { ClientFactoryFullName } ") ;
17+ throw new LocalStackClientConfigurationException ( $ "Failed to find internal ClientFactory<T> in { ClientFactoryGenericTypeName } ") ;
1818 }
1919
20- ConstructorInfo ? constructorInfo =
21- clientFactoryType . GetConstructor ( BindingFlags . NonPublic | BindingFlags . Instance , null , new [ ] { typeof ( Type ) , typeof ( AWSOptions ) } , null ) ;
20+ // Create ClientFactory<TClient>
21+ Type concreteFactoryType = genericFactoryType . MakeGenericType ( typeof ( TClient ) ) ;
22+ ConstructorInfo ? constructor = concreteFactoryType . GetConstructor ( BindingFlags . NonPublic | BindingFlags . Instance , null , new [ ] { typeof ( AWSOptions ) } , null ) ;
2223
23- if ( constructorInfo == null )
24+ if ( constructor == null )
2425 {
25- throw new LocalStackClientConfigurationException ( "ClientFactory missing a constructor with parameters Type and AWSOptions." ) ;
26+ throw new LocalStackClientConfigurationException ( "ClientFactory<T> missing constructor with AWSOptions parameter ." ) ;
2627 }
2728
28- Type clientType = typeof ( TClient ) ;
29+ object factory = constructor . Invoke ( new object [ ] { awsOptions ! } ) ;
30+ MethodInfo ? createMethod = factory . GetType ( ) . GetMethod ( CreateServiceClientMethodName , BindingFlags . NonPublic | BindingFlags . Instance , null , new [ ] { typeof ( IServiceProvider ) } , null ) ;
2931
30- object clientFactory = constructorInfo . Invoke ( new object [ ] { clientType , awsOptions ! } ) ;
31-
32- MethodInfo ? methodInfo = clientFactory . GetType ( ) . GetMethod ( CreateServiceClientMethodName , BindingFlags . NonPublic | BindingFlags . Instance ) ;
33-
34- if ( methodInfo == null )
32+ if ( createMethod == null )
3533 {
36- throw new LocalStackClientConfigurationException ( $ "Failed to find internal method { CreateServiceClientMethodName } in { ClientFactoryFullName } ") ;
34+ throw new LocalStackClientConfigurationException ( $ "ClientFactory<T> missing { CreateServiceClientMethodName } (IServiceProvider) method. ") ;
3735 }
3836
39- object serviceInstance = methodInfo . Invoke ( clientFactory , new object [ ] { provider } ) ;
40-
37+ object serviceInstance = createMethod . Invoke ( factory , new object [ ] { provider } ) ;
4138 return ( AmazonServiceClient ) serviceInstance ;
4239 }
4340}
0 commit comments