@@ -26,12 +26,12 @@ public partial class HttpApiFactory : IHttpApiFactory, _IHttpApiFactory
2626 /// <summary>
2727 /// 过期的记录
2828 /// </summary>
29- private readonly ConcurrentQueue < ExpiredHandlerEntry > expiredEntries ;
29+ private readonly ConcurrentQueue < ExpiredEntry > expiredEntries ;
3030
3131 /// <summary>
3232 /// 激活记录的创建工厂
3333 /// </summary>
34- private readonly Func < Type , Lazy < ActiveHandlerEntry > > activeEntryFactory ;
34+ private readonly Func < Type , Lazy < ActiveEntry > > activeEntryFactory ;
3535
3636 /// <summary>
3737 /// http接口代理创建选项
@@ -41,19 +41,18 @@ public partial class HttpApiFactory : IHttpApiFactory, _IHttpApiFactory
4141 /// <summary>
4242 /// 激活的记录
4343 /// </summary>
44- private readonly ConcurrentDictionary < Type , Lazy < ActiveHandlerEntry > > activeEntries ;
45-
44+ private readonly ConcurrentDictionary < Type , Lazy < ActiveEntry > > activeEntries ;
4645
4746 /// <summary>
48- /// 获取已过期但还未释放的HttpMessageHandler数量
47+ /// 获取已过期但还未释放的HttpApi实例数量
4948 /// </summary>
50- public int ExpiredHandlerCount
49+ public int ExpiredCount
5150 {
5251 get => this . expiredEntries . Count ;
5352 }
5453
5554 /// <summary>
56- /// 获取或设置HttpMessageHandler的生命周期
55+ /// 获取或设置HttpApi实例的生命周期
5756 /// </summary>
5857 /// <exception cref="ArgumentOutOfRangeException"></exception>
5958 public TimeSpan Lifetime
@@ -73,7 +72,7 @@ public TimeSpan Lifetime
7372 }
7473
7574 /// <summary>
76- /// 获取或设置清理过期的HttpMessageHandler的时间间隔
75+ /// 获取或设置清理过期的HttpApi实例的时间间隔
7776 /// </summary>
7877 /// <exception cref="ArgumentOutOfRangeException"></exception>
7978 public TimeSpan CleanupInterval
@@ -97,10 +96,10 @@ public TimeSpan CleanupInterval
9796 /// </summary>
9897 public HttpApiFactory ( )
9998 {
100- this . expiredEntries = new ConcurrentQueue < ExpiredHandlerEntry > ( ) ;
101- this . activeEntries = new ConcurrentDictionary < Type , Lazy < ActiveHandlerEntry > > ( ) ;
99+ this . expiredEntries = new ConcurrentQueue < ExpiredEntry > ( ) ;
100+ this . activeEntries = new ConcurrentDictionary < Type , Lazy < ActiveEntry > > ( ) ;
102101 this . httpApiCreateOptions = new ConcurrentDictionary < Type , HttpApiCreateOption > ( ) ;
103- this . activeEntryFactory = apiType => new Lazy < ActiveHandlerEntry > ( ( ) => this . CreateActiveEntry ( apiType ) , LazyThreadSafetyMode . ExecutionAndPublication ) ;
102+ this . activeEntryFactory = apiType => new Lazy < ActiveEntry > ( ( ) => this . CreateActiveEntry ( apiType ) , LazyThreadSafetyMode . ExecutionAndPublication ) ;
104103
105104 this . RegisteCleanup ( ) ;
106105 }
@@ -111,8 +110,8 @@ public HttpApiFactory()
111110 /// <typeparam name="TInterface"></typeparam>
112111 /// <param name="config">HttpApiConfig的配置</param>
113112 /// <param name="handlerFactory">HttpMessageHandler创建委托</param>
114- /// <exception cref="InvalidOperationException" ></exception >
115- public void AddHttpApi < TInterface > ( Action < HttpApiConfig > config , Func < HttpMessageHandler > handlerFactory ) where TInterface : class , IHttpApi
113+ /// <returns ></returns >
114+ public bool AddHttpApi < TInterface > ( Action < HttpApiConfig > config , Func < HttpMessageHandler > handlerFactory ) where TInterface : class , IHttpApi
116115 {
117116 if ( handlerFactory == null )
118117 {
@@ -125,62 +124,60 @@ public void AddHttpApi<TInterface>(Action<HttpApiConfig> config, Func<HttpMessag
125124 HandlerFactory = handlerFactory
126125 } ;
127126
128- var state = this . httpApiCreateOptions . TryAdd ( typeof ( TInterface ) , options ) ;
129- if ( state == false )
130- {
131- throw new InvalidOperationException ( $ "接口{ typeof ( TInterface ) } 不能重复注册") ;
132- }
127+ return this . httpApiCreateOptions . TryAdd ( typeof ( TInterface ) , options ) ;
133128 }
134129
135130 /// <summary>
136131 /// 创建指定接口的代理实例
137132 /// </summary>
138133 /// <typeparam name="TInterface"></typeparam>
134+ /// <exception cref="ArgumentException"></exception>
139135 /// <returns></returns>
140136 public TInterface CreateHttpApi < TInterface > ( ) where TInterface : class , IHttpApi
141137 {
142138 var apiType = typeof ( TInterface ) ;
143139 var entry = this . activeEntries . GetOrAdd ( apiType , this . activeEntryFactory ) . Value ;
144- return HttpApiClient . Create < TInterface > ( entry . HttpApiConfig ) ;
140+ return HttpApiClient . Create ( apiType , entry . Interceptor ) as TInterface ;
145141 }
146142
147143 /// <summary>
148- /// 创建激活状态的Handler记录
144+ /// 创建激活状态的记录
149145 /// </summary>
150146 /// <param name="apiType">http接口类型</param>
147+ /// <exception cref="ArgumentException"></exception>
151148 /// <returns></returns>
152- private ActiveHandlerEntry CreateActiveEntry ( Type apiType )
149+ private ActiveEntry CreateActiveEntry ( Type apiType )
153150 {
154151 if ( this . httpApiCreateOptions . TryGetValue ( apiType , out var option ) == false )
155152 {
156153 throw new ArgumentException ( $ "未注册的接口类型{ apiType } ") ;
157154 }
158155
159- var innder = option . HandlerFactory . Invoke ( ) ;
160- var handler = new LifeTimeTrackingHandler ( innder ) ;
156+ var handler = option . HandlerFactory . Invoke ( ) ;
161157 var httpApiConfig = new HttpApiConfig ( handler , false ) ;
158+ var interceptor = new LifeTimeTrackingInterceptor ( httpApiConfig ) ;
162159
163160 if ( option . ConfigAction != null )
164161 {
165162 option . ConfigAction . Invoke ( httpApiConfig ) ;
166163 }
167164
168- return new ActiveHandlerEntry ( this )
165+ return new ActiveEntry ( this )
169166 {
170167 ApiType = apiType ,
171- Disposable = innder ,
172- HttpApiConfig = httpApiConfig
168+ Disposable = httpApiConfig ,
169+ Interceptor = interceptor
173170 } ;
174171 }
175172
176173 /// <summary>
177174 /// 当有记录失效时
178175 /// </summary>
179176 /// <param name="active">激活的记录</param>
180- void _IHttpApiFactory . OnEntryDeactivate ( ActiveHandlerEntry active )
177+ void _IHttpApiFactory . OnEntryDeactivate ( ActiveEntry active )
181178 {
182179 this . activeEntries . TryRemove ( active . ApiType , out var _ ) ;
183- var expired = new ExpiredHandlerEntry ( active ) ;
180+ var expired = new ExpiredEntry ( active ) ;
184181 this . expiredEntries . Enqueue ( expired ) ;
185182 }
186183
0 commit comments