Skip to content

Commit 425bb07

Browse files
authored
Merge pull request #44 from softlgl/dev
优化代码
2 parents fc2fdad + 6758692 commit 425bb07

3 files changed

Lines changed: 15 additions & 11 deletions

File tree

src/DotNetCoreRpc.Client/ClientOptions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.Extensions.DependencyInjection.Extensions;
44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Net.Http;
78
using System.Text;
89

@@ -30,13 +31,14 @@ public ClientOptions(IServiceCollection services, string serviceName)
3031

3132
public ClientOptions AddRpcClient<T>() where T : class
3233
{
33-
Services.TryAddScoped(provider => AddRpcClient(provider));
34+
Services.TryAddScoped(provider => CreateRpcClient(provider));
3435

35-
T AddRpcClient(IServiceProvider serviceProvider)
36+
T CreateRpcClient(IServiceProvider serviceProvider)
3637
{
3738
var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
3839
var proxyGenerator = serviceProvider.GetRequiredService<ProxyGenerator>();
3940
var httpClient = httpClientFactory.CreateClient(ServiceName);
41+
4042
RpcClient rpcClient = new RpcClient(this, httpClient, proxyGenerator);
4143
return rpcClient.CreateClient<T>();
4244
}

src/DotNetCoreRpc.Client/RpcClient.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ public RpcClient(ClientOptions clientOptions, HttpClient httpClient, ProxyGenera
2222

2323
internal T CreateClient<T>() where T : class
2424
{
25-
InitalHttpClient();
25+
InitialHttpClient();
2626

27-
HttpRequestInterceptor httpRequestInterceptor = new HttpRequestInterceptor(_httpClient);
28-
return _proxyGenerator.CreateInterfaceProxyWithoutTarget<T>(httpRequestInterceptor);
27+
return _proxyGenerator.CreateInterfaceProxyWithoutTarget<T>(new HttpRequestInterceptor(_httpClient));
2928
}
3029

31-
private void InitalHttpClient()
30+
private void InitialHttpClient()
3231
{
3332
_httpClient.DefaultRequestHeaders.Add("req-source", "dncrpc");
3433
if (!string.IsNullOrWhiteSpace(_path))

src/DotNetCoreRpc.Server/RpcBuilder/RpcFilterUtils.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ public static class RpcFilterUtils
1919
/// <returns></returns>
2020
public static List<RpcFilterAttribute> GetFilterAttributes(RpcContext aspectContext, IServiceProvider serviceProvider, IEnumerable<Type> filterTypes)
2121
{
22-
var methondInfo = aspectContext.Method;
22+
var methodInfo = aspectContext.Method;
23+
var reflectedTypeHandle = methodInfo.ReflectedType!.TypeHandle.Value;
24+
var methodHandle = methodInfo.MethodHandle.Value;
25+
var methodKey = $"{reflectedTypeHandle}_{methodHandle}";
2326

24-
var methondInterceptorAttributes = _methodFilters.GetOrAdd($"{methondInfo.DeclaringType.FullName}#{methondInfo.Name}",
27+
var methondInterceptorAttributes = _methodFilters.GetOrAdd(methodKey,
2528
key =>
2629
{
27-
var methondAttributes = methondInfo.GetCustomAttributes(true)
30+
var methondAttributes = methodInfo.GetCustomAttributes(true)
2831
.Where(i => typeof(RpcFilterAttribute).IsAssignableFrom(i.GetType()))
2932
.Cast<RpcFilterAttribute>().ToList();
30-
var classAttributes = methondInfo.DeclaringType.GetCustomAttributes(true)
33+
var classAttributes = methodInfo.DeclaringType.GetCustomAttributes(true)
3134
.Where(i => typeof(RpcFilterAttribute).IsAssignableFrom(i.GetType()))
3235
.Cast<RpcFilterAttribute>();
3336
//获取方法filter
@@ -68,7 +71,7 @@ private static void PropertiesInject(IServiceProvider serviceProvider, IEnumerab
6871

6972
private static void PropertieInject(IServiceProvider serviceProvider, RpcFilterAttribute rpcFilterAttribute)
7073
{
71-
var properties = _filterFromServices.GetOrAdd($"{rpcFilterAttribute.GetType().FullName}", key => rpcFilterAttribute.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(i => i.GetCustomAttribute<FromServicesAttribute>() != null));
74+
var properties = _filterFromServices.GetOrAdd($"{rpcFilterAttribute.GetType().TypeHandle.Value}", key => rpcFilterAttribute.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(i => i.GetCustomAttribute<FromServicesAttribute>() != null));
7275
if (properties.Any())
7376
{
7477
foreach (var propertyInfo in properties)

0 commit comments

Comments
 (0)