@@ -14,11 +14,6 @@ namespace WebApiClient
1414 /// </summary>
1515 static class HttpApiClientProxy
1616 {
17- /// <summary>
18- /// 同步锁
19- /// </summary>
20- private static readonly object syncRoot = new object ( ) ;
21-
2217 /// <summary>
2318 /// IApiInterceptor的Intercept方法
2419 /// </summary>
@@ -42,7 +37,7 @@ static class HttpApiClientProxy
4237 /// <summary>
4338 /// 接口类型与代理类型的构造器缓存
4439 /// </summary>
45- private static readonly ConcurrentDictionary < Type , ConstructorInfo > proxyTypeCtorCache = new ConcurrentDictionary < Type , ConstructorInfo > ( ) ;
40+ private static readonly ConcurrentDictionary < Type , Lazy < ConstructorInfo > > proxyTypeCtorCache = new ConcurrentDictionary < Type , Lazy < ConstructorInfo > > ( ) ;
4641
4742 /// <summary>
4843 /// 创建HttpApiClient代理类
@@ -55,20 +50,18 @@ static class HttpApiClientProxy
5550 /// <returns></returns>
5651 public static object CreateProxyWithInterface ( Type interfaceType , IApiInterceptor interceptor )
5752 {
58- lock ( syncRoot )
59- {
60- var apiMethods = interfaceType . GetAllApiMethods ( ) ;
61- var proxyTypeCtor = proxyTypeCtorCache . GetOrAdd (
62- interfaceType ,
63- @interface => @interface . ImplementAsHttpApiClient ( apiMethods ) ) ;
53+ var apiMethods = interfaceType . GetAllApiMethods ( ) ;
54+ var proxyTypeCtor = proxyTypeCtorCache . GetOrAdd (
55+ interfaceType ,
56+ @interface => new Lazy < ConstructorInfo > ( ( ) => @interface . ImplementAsHttpApiClient ( apiMethods ) ) ) ;
6457
65- return proxyTypeCtor . Invoke ( new object [ ] { interceptor , apiMethods } ) ;
66- }
58+ return proxyTypeCtor . Value . Invoke ( new object [ ] { interceptor , apiMethods } ) ;
6759 }
6860
6961 /// <summary>
7062 /// 继承HttpApiClient并实现接口
7163 /// 并返回代理类的构造器
64+ /// 对于相同的interfaceType,不允许并发执行
7265 /// </summary>
7366 /// <param name="interfaceType">接口类型</param>
7467 /// <param name="apiMethods">接口方法集合</param>
0 commit comments