@@ -11,18 +11,18 @@ namespace WebApiClient
1111 /// 提供HttpApi的配置注册和实例创建
1212 /// 并对实例的生命周期进行自动管理
1313 /// </summary>
14- public class HttpApiFactory < TInterface > : IHttpApiFactory < TInterface > , IHttpApiFactory , _IHttpApiFactory
14+ public class HttpApiFactory < TInterface > : IHttpApiFactory < TInterface > , _IHttpApiFactory
1515 where TInterface : class , IHttpApi
1616 {
1717 /// <summary>
1818 /// HttpApiConfig的配置委托
1919 /// </summary>
20- private readonly Action < HttpApiConfig > configAction ;
20+ private Action < HttpApiConfig > configAction ;
2121
2222 /// <summary>
2323 /// HttpMessageHandler的创建委托
2424 /// </summary>
25- private readonly Func < HttpMessageHandler > handlerFunc ;
25+ private Func < HttpMessageHandler > handlerFunc ;
2626
2727 /// <summary>
2828 /// handler的生命周期
@@ -46,84 +46,70 @@ public class HttpApiFactory<TInterface> : IHttpApiFactory<TInterface>, IHttpApiF
4646
4747
4848 /// <summary>
49- /// 获取已过期但还未释放的HttpApi实例数量
49+ /// HttpApi创建工厂
5050 /// </summary>
51- public int ExpiredCount
51+ public HttpApiFactory ( )
5252 {
53- get => this . expiredEntries . Count ;
53+ this . expiredEntries = new ConcurrentQueue < ExpiredEntry > ( ) ;
54+
55+ this . activeEntryLazy = new Lazy < ActiveEntry > (
56+ this . CreateActiveEntry ,
57+ LazyThreadSafetyMode . ExecutionAndPublication ) ;
58+
59+ this . RegisteCleanup ( ) ;
5460 }
5561
62+
5663 /// <summary>
57- /// 获取或设置HttpApi实例的生命周期
64+ /// 置HttpApi实例的生命周期
5865 /// </summary>
66+ /// <param name="lifeTime">生命周期</param>
5967 /// <exception cref="ArgumentOutOfRangeException"></exception>
60- public TimeSpan Lifetime
68+ public HttpApiFactory < TInterface > SetLifetime ( TimeSpan lifeTime )
6169 {
62- get
63- {
64- return this . lifeTime ;
65- }
66- set
70+ if ( lifeTime <= TimeSpan . Zero )
6771 {
68- if ( value <= TimeSpan . Zero )
69- {
70- throw new ArgumentOutOfRangeException ( ) ;
71- }
72- this . lifeTime = value ;
72+ throw new ArgumentOutOfRangeException ( ) ;
7373 }
74+ this . lifeTime = lifeTime ;
75+ return this ;
7476 }
7577
7678 /// <summary>
7779 /// 获取或设置清理过期的HttpApi实例的时间间隔
7880 /// </summary>
81+ /// <param name="interval">时间间隔</param>
7982 /// <exception cref="ArgumentOutOfRangeException"></exception>
80- public TimeSpan CleanupInterval
83+ public HttpApiFactory < TInterface > SetCleanupInterval ( TimeSpan interval )
8184 {
82- get
85+ if ( interval <= TimeSpan . Zero )
8386 {
84- return this . cleanupInterval ;
85- }
86- set
87- {
88- if ( value <= TimeSpan . Zero )
89- {
90- throw new ArgumentOutOfRangeException ( ) ;
91- }
92- this . cleanupInterval = value ;
87+ throw new ArgumentOutOfRangeException ( ) ;
9388 }
89+ this . cleanupInterval = interval ;
90+ return this ;
9491 }
9592
9693 /// <summary>
97- /// HttpApi创建工厂
94+ /// 配置HttpMessageHandler的创建
9895 /// </summary>
99- public HttpApiFactory ( )
100- : this ( configAction : null )
101- {
102- }
103-
104- /// <summary>
105- /// HttpApi创建工厂
106- /// </summary>
107- /// <param name="configAction">HttpApiConfig的配置委托</param>
108- public HttpApiFactory ( Action < HttpApiConfig > configAction )
109- : this ( configAction , handlerFunc : null )
96+ /// <param name="handlerFunc">创建委托</param>
97+ /// <returns></returns>
98+ public HttpApiFactory < TInterface > ConfigureHttpMessageHandler ( Func < HttpMessageHandler > handlerFunc )
11099 {
100+ this . handlerFunc = handlerFunc ;
101+ return this ;
111102 }
112103
113104 /// <summary>
114- /// HttpApi创建工厂
105+ /// 配置HttpApiConfig
115106 /// </summary>
116- /// <param name="configAction">HttpApiConfig的配置委托 </param>
117- /// <param name="handlerFunc">HttpMessageHandler的创建委托</param >
118- public HttpApiFactory ( Action < HttpApiConfig > configAction , Func < HttpMessageHandler > handlerFunc )
107+ /// <param name="configAction">配置委托 </param>
108+ /// <returns></returns >
109+ public HttpApiFactory < TInterface > ConfigureHttpApiConfig ( Action < HttpApiConfig > configAction )
119110 {
120111 this . configAction = configAction ;
121- this . handlerFunc = handlerFunc ?? new Func < HttpMessageHandler > ( ( ) => new DefaultHttpClientHandler ( ) ) ;
122-
123- this . expiredEntries = new ConcurrentQueue < ExpiredEntry > ( ) ;
124- this . activeEntryLazy = new Lazy < ActiveEntry > ( this . CreateActiveEntry , LazyThreadSafetyMode . ExecutionAndPublication ) ;
125-
126- this . RegisteCleanup ( ) ;
112+ return this ;
127113 }
128114
129115 /// <summary>
@@ -132,14 +118,14 @@ public HttpApiFactory(Action<HttpApiConfig> configAction, Func<HttpMessageHandle
132118 /// <returns></returns>
133119 public TInterface CreateHttpApi ( )
134120 {
135- return ( ( IHttpApiFactory ) this ) . CreateHttpApi ( ) as TInterface ;
121+ return ( ( _IHttpApiFactory ) this ) . CreateHttpApi ( ) as TInterface ;
136122 }
137123
138124 /// <summary>
139125 /// 创建接口的代理实例
140126 /// </summary>
141127 /// <returns></returns>
142- object IHttpApiFactory . CreateHttpApi ( )
128+ object _IHttpApiFactory . CreateHttpApi ( )
143129 {
144130 var interceptor = this . activeEntryLazy . Value . Interceptor ;
145131 return HttpApiClient . Create ( typeof ( TInterface ) , interceptor ) ;
@@ -151,7 +137,8 @@ object IHttpApiFactory.CreateHttpApi()
151137 /// <returns></returns>
152138 private ActiveEntry CreateActiveEntry ( )
153139 {
154- var httpApiConfig = new HttpApiConfig ( this . handlerFunc . Invoke ( ) , true ) ;
140+ var handler = this . handlerFunc ? . Invoke ( ) ?? new DefaultHttpClientHandler ( ) ;
141+ var httpApiConfig = new HttpApiConfig ( handler , true ) ;
155142 var interceptor = new LifeTimeTrackingInterceptor ( httpApiConfig ) ;
156143
157144 if ( this . configAction != null )
@@ -173,12 +160,22 @@ private ActiveEntry CreateActiveEntry()
173160 void _IHttpApiFactory . OnEntryDeactivate ( ActiveEntry active )
174161 {
175162 // 切换激活状态的记录的实例
176- this . activeEntryLazy = new Lazy < ActiveEntry > ( this . CreateActiveEntry , LazyThreadSafetyMode . ExecutionAndPublication ) ;
163+ this . activeEntryLazy = new Lazy < ActiveEntry > (
164+ this . CreateActiveEntry ,
165+ LazyThreadSafetyMode . ExecutionAndPublication ) ;
177166
178167 var expired = new ExpiredEntry ( active ) ;
179168 this . expiredEntries . Enqueue ( expired ) ;
180169 }
181170
171+ /// <summary>
172+ /// 获取生命周期
173+ /// </summary>
174+ TimeSpan _IHttpApiFactory . Lifetime
175+ {
176+ get => this . lifeTime ;
177+ }
178+
182179 /// <summary>
183180 /// 注册清理任务
184181 /// </summary>
0 commit comments