@@ -10,29 +10,37 @@ namespace DotNetCoreRpc.Client
1010{
1111 public class ClientOptions
1212 {
13- private IServiceCollection _services ;
14- private readonly string _serviceName ;
15-
1613 /// <summary>
1714 /// 服务请求路径
1815 /// </summary>
1916 public string Path { get ; set ; }
2017
18+ /// <summary>
19+ /// 服务名称
20+ /// </summary>
21+ public string ServiceName { get ; }
22+
23+ public IServiceCollection Services { get ; }
24+
2125 public ClientOptions ( IServiceCollection services , string serviceName )
2226 {
23- _services = services ;
24- _serviceName = serviceName ;
27+ Services = services ;
28+ ServiceName = serviceName ;
2529 }
2630
2731 public ClientOptions AddRpcClient < T > ( ) where T : class
2832 {
29- _services . TryAddScoped ( provider => {
30- var httpClientFactory = provider . GetRequiredService < IHttpClientFactory > ( ) ;
31- var proxyGenerator = provider . GetRequiredService < ProxyGenerator > ( ) ;
32- var httpClient = httpClientFactory . CreateClient ( _serviceName ) ;
33- RpcClient rpcClient = new RpcClient ( httpClient , proxyGenerator , Path ) ;
33+ Services . TryAddScoped ( provider => AddRpcClient ( provider ) ) ;
34+
35+ T AddRpcClient ( IServiceProvider serviceProvider )
36+ {
37+ var httpClientFactory = serviceProvider . GetRequiredService < IHttpClientFactory > ( ) ;
38+ var proxyGenerator = serviceProvider . GetRequiredService < ProxyGenerator > ( ) ;
39+ var httpClient = httpClientFactory . CreateClient ( ServiceName ) ;
40+ RpcClient rpcClient = new RpcClient ( this , httpClient , proxyGenerator ) ;
3441 return rpcClient . CreateClient < T > ( ) ;
35- } ) ;
42+ }
43+
3644 return this ;
3745 }
3846 }
0 commit comments