Skip to content

Commit 276e8eb

Browse files
author
liguoliang
committed
修重构客户端注册方式
1 parent cd7f746 commit 276e8eb

4 files changed

Lines changed: 54 additions & 21 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Castle.DynamicProxy;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.DependencyInjection.Extensions;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Net.Http;
7+
using System.Text;
8+
9+
namespace DotNetCoreRpc.Client
10+
{
11+
public class ClientOptions
12+
{
13+
private IServiceCollection _services;
14+
private readonly string _serviceName;
15+
16+
public ClientOptions(IServiceCollection services, string serviceName)
17+
{
18+
_services = services;
19+
_serviceName = serviceName;
20+
}
21+
22+
public ClientOptions AddRpcClient<T>() where T : class
23+
{
24+
_services.TryAddScoped(provider => {
25+
var httpClientFactory = provider.GetRequiredService<IHttpClientFactory>();
26+
var proxyGenerator = provider.GetRequiredService<ProxyGenerator>();
27+
RpcClient rpcClient = new RpcClient(httpClientFactory.CreateClient(_serviceName), proxyGenerator);
28+
return rpcClient.CreateClient<T>();
29+
});
30+
return this;
31+
}
32+
}
33+
}

src/DotNetCoreRpc.Client/RequestHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private async Task<T> TaskResultHandle<T>(MethodInfo methodInfo, params object[]
7171
ResponseModel responseModel = result.FromJson<ResponseModel>();
7272
if (responseModel.Code != (int)HttpStatusCode.OK)
7373
{
74-
throw new Exception($"请求出错,返回内容:{Encoding.UTF8.GetString(result)}");
74+
throw new Exception($"请求出错,返回内容:{responseModel.Message}");
7575
}
7676

7777
TypeInfo methodReturnType = methodInfo.ReturnType.GetTypeInfo();

src/DotNetCoreRpc.Client/RpcClient.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
using System;
1+
using Castle.DynamicProxy;
2+
using System;
3+
using System.Collections.Generic;
24
using System.Net.Http;
3-
using Castle.DynamicProxy;
4-
using System.Collections.Concurrent;
5+
using System.Text;
56

67
namespace DotNetCoreRpc.Client
78
{
8-
public class RpcClient
9+
internal class RpcClient
910
{
10-
private readonly ProxyGenerator _proxyGenerator;
11-
private readonly IHttpClientFactory _httpClientFactory;
11+
private HttpClient _httpClient;
12+
private ProxyGenerator _proxyGenerator;
1213

13-
public RpcClient(ProxyGenerator proxyGenerator, IHttpClientFactory httpClientFactory)
14+
public RpcClient(HttpClient httpClient, ProxyGenerator proxyGenerator)
1415
{
16+
_httpClient = httpClient;
17+
_httpClient.DefaultRequestHeaders.Add("req-source", "dncrpc");
1518
_proxyGenerator = proxyGenerator;
16-
_httpClientFactory = httpClientFactory;
1719
}
1820

19-
public T CreateClient<T>(string serviceName) where T : class
21+
internal T CreateClient<T>() where T : class
2022
{
21-
return CreateClient<T>(_httpClientFactory.CreateClient(serviceName));
22-
}
23-
24-
public T CreateClient<T>(HttpClient httpClient) where T : class
25-
{
26-
httpClient.DefaultRequestHeaders.Add("req-source", "dncrpc");
27-
HttpRequestInterceptor httpRequestInterceptor = new HttpRequestInterceptor(httpClient);
23+
HttpRequestInterceptor httpRequestInterceptor = new HttpRequestInterceptor(_httpClient);
2824
return _proxyGenerator.CreateInterfaceProxyWithoutTarget<T>(httpRequestInterceptor);
2925
}
3026
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using System;
22
using Castle.DynamicProxy;
33
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.DependencyInjection.Extensions;
45

56
namespace DotNetCoreRpc.Client
67
{
78
public static class ServiceCollectionExtensions
89
{
9-
public static IServiceCollection AddDotNetCoreRpcClient(this IServiceCollection services)
10+
public static IHttpClientBuilder AddDotNetCoreRpcClient(this IHttpClientBuilder httpClientBuilder, Action<ClientOptions> options)
1011
{
11-
services.AddSingleton<ProxyGenerator>();
12-
services.AddSingleton<RpcClient>();
13-
return services;
12+
httpClientBuilder.Services.TryAddSingleton<ProxyGenerator>();
13+
14+
ClientOptions clientOptions = new ClientOptions(httpClientBuilder.Services, httpClientBuilder.Name);
15+
options.Invoke(clientOptions);
16+
17+
return httpClientBuilder;
1418
}
1519
}
1620
}

0 commit comments

Comments
 (0)