Skip to content

Commit 0abcd43

Browse files
author
liguoliang
committed
优化代码
1 parent dc65b83 commit 0abcd43

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/DotNetCoreRpc.Client/ClientOptions.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/DotNetCoreRpc.Client/RpcClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ internal class RpcClient
1313
private ProxyGenerator _proxyGenerator;
1414
private string _path;
1515

16-
public RpcClient(HttpClient httpClient, ProxyGenerator proxyGenerator, string path)
16+
public RpcClient(ClientOptions clientOptions, HttpClient httpClient, ProxyGenerator proxyGenerator)
1717
{
18+
_path = clientOptions.Path;
1819
_httpClient = httpClient;
1920
_proxyGenerator = proxyGenerator;
20-
_path = path;
2121
}
2222

2323
internal T CreateClient<T>() where T : class

0 commit comments

Comments
 (0)