Skip to content

Commit ad34fbc

Browse files
committed
创建HttpApiClient实例加锁
1 parent 582ec87 commit ad34fbc

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

WebApiClient/Internal/HttpApiClientProxy.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ namespace WebApiClient
1414
/// </summary>
1515
static class HttpApiClientProxy
1616
{
17+
/// <summary>
18+
/// 同步锁
19+
/// </summary>
20+
private static readonly object syncRoot = new object();
21+
1722
/// <summary>
1823
/// IApiInterceptor的Intercept方法
1924
/// </summary>
@@ -50,13 +55,15 @@ static class HttpApiClientProxy
5055
/// <returns></returns>
5156
public static object CreateProxyWithInterface(Type interfaceType, IApiInterceptor interceptor)
5257
{
53-
var apiMethods = interfaceType.GetAllApiMethods();
54-
55-
var proxyTypeCtor = proxyTypeCtorCache.GetOrAdd(
56-
interfaceType,
57-
@interface => @interface.ImplementAsHttpApiClient(apiMethods));
58+
lock (syncRoot)
59+
{
60+
var apiMethods = interfaceType.GetAllApiMethods();
61+
var proxyTypeCtor = proxyTypeCtorCache.GetOrAdd(
62+
interfaceType,
63+
@interface => @interface.ImplementAsHttpApiClient(apiMethods));
5864

59-
return proxyTypeCtor.Invoke(new object[] { interceptor, apiMethods });
65+
return proxyTypeCtor.Invoke(new object[] { interceptor, apiMethods });
66+
}
6067
}
6168

6269
/// <summary>

0 commit comments

Comments
 (0)