Skip to content

Commit ec38221

Browse files
author
liguoliang
committed
优化接口注册逻辑
1 parent 7d98e26 commit ec38221

3 files changed

Lines changed: 53 additions & 37 deletions

File tree

src/DotNetCoreRpc.Server/DotNetCoreRpcMiddleware.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,42 @@ namespace DotNetCoreRpc.Server
1414
{
1515
public class DotNetCoreRpcMiddleware
1616
{
17-
private readonly IDictionary<string,Type> _types;
17+
private readonly IDictionary<string, Type> _types;
1818
private readonly IEnumerable<Type> _filterTypes;
1919
private readonly ConcurrentDictionary<string, List<RpcFilterAttribute>> _methodFilters = new ConcurrentDictionary<string, List<RpcFilterAttribute>>();
2020

2121
public DotNetCoreRpcMiddleware(RequestDelegate next, RpcServerOptions rpcServerOptions)
2222
{
23-
_types = rpcServerOptions.GetTypes();
23+
_types = rpcServerOptions.GetRegisterTypes();
2424
_filterTypes = rpcServerOptions.GetFilterTypes();
2525
}
2626

2727
public async Task InvokeAsync(HttpContext context)
2828
{
29-
//context.Request.EnableBuffering();
30-
//context.Request.Body.Seek(0, SeekOrigin.Begin);
31-
//var requestReader = new StreamReader(context.Request.Body);
32-
//var requestContent = await requestReader.ReadToEndAsync();
33-
//context.Request.Body.Seek(0, SeekOrigin.Begin);
34-
3529
var requestContent = await context.Request.ReadStringAsync();
36-
ResponseModel responseModel = new ResponseModel
37-
{
38-
Code = 500
39-
};
30+
ResponseModel responseModel = new ResponseModel{ Code = 500 };
4031
if (string.IsNullOrEmpty(requestContent))
4132
{
4233
responseModel.Message = "未读取到请求信息";
4334
await context.Response.WriteAsync(responseModel.ToJson());
4435
return;
4536
}
37+
4638
RequestModel requestModel = requestContent.FromJson<RequestModel>();
4739
if (requestModel == null)
4840
{
4941
responseModel.Message = "读取请求数据失败";
5042
await context.Response.WriteAsync(responseModel.ToJson());
5143
return;
5244
}
45+
5346
if (!_types.ContainsKey(requestModel.TypeFullName))
5447
{
5548
responseModel.Message = $"{requestModel.TypeFullName}未注册";
5649
await context.Response.WriteAsync(responseModel.ToJson());
5750
return;
5851
}
52+
5953
await HandleRequest(context, requestModel);
6054
return;
6155
}
@@ -79,6 +73,7 @@ private async Task HandleRequest(HttpContext context, RequestModel requestModel)
7973
paramters[i] = paramters[i].ToJson().FromJson(methodParamters[i].ParameterType);
8074
}
8175
}
76+
8277
RpcContext aspectContext = new RpcContext
8378
{
8479
Parameters = paramters,
@@ -110,6 +105,7 @@ private AspectPiplineBuilder CreatPipleline(RpcContext aspectContext)
110105
};
111106
await aspectContext.HttpContext.Response.WriteAsync(responseModel.ToJson());
112107
});
108+
113109
List<RpcFilterAttribute> interceptorAttributes = GetFilterAttributes(aspectContext);
114110
if (interceptorAttributes.Any())
115111
{

src/DotNetCoreRpc.Server/RpcServerOptions.cs

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ namespace DotNetCoreRpc.Server
88
{
99
public class RpcServerOptions
1010
{
11-
private readonly IDictionary<string, Type> _types = new Dictionary<string, Type>();
11+
private readonly IDictionary<string, Type> _serviceTypes = new Dictionary<string, Type>();
12+
private readonly HashSet<Type> _serviceTypeSet = new HashSet<Type>();
13+
private readonly HashSet<string> _serviceNameSet = new HashSet<string>();
14+
private readonly HashSet<string> _serviceNameSpaceSet = new HashSet<string>();
1215
private readonly IList<Type> _filterTypes = new List<Type>();
16+
1317
private readonly IServiceCollection _services;
1418

1519
public RpcServerOptions(IServiceCollection services)
@@ -26,44 +30,59 @@ public RpcServerOptions AddFilter<RpcFilterAttribute>()
2630
public RpcServerOptions AddService<TService>()
2731
where TService:class
2832
{
29-
Type serviceType = typeof(TService);
30-
_types.TryAdd(serviceType.FullName, serviceType);
33+
_serviceTypeSet.Add(typeof(TService));
3134
return this;
3235
}
3336

3437
public RpcServerOptions AddService(string serviceName)
3538
{
36-
foreach (var service in _services)
37-
{
38-
if (serviceName.StartsWith("*")
39-
&& service.ServiceType.Name.EndsWith(serviceName.Substring(1)))
40-
{
41-
_types.TryAdd(service.ServiceType.FullName, service.ServiceType);
42-
continue;
43-
}
44-
if (service.ServiceType.Name == serviceName)
45-
{
46-
_types.TryAdd(service.ServiceType.FullName, service.ServiceType);
47-
}
48-
}
39+
_serviceNameSet.Add(serviceName);
4940
return this;
5041
}
5142

5243
public RpcServerOptions AddNameSpace(string nameSpace)
5344
{
54-
foreach (var service in _services)
45+
_serviceNameSpaceSet.Add(nameSpace);
46+
return this;
47+
}
48+
49+
public IDictionary<string, Type> GetRegisterTypes()
50+
{
51+
foreach (var serviceType in _serviceTypeSet)
5552
{
56-
if (service.ServiceType.Namespace == nameSpace)
53+
_serviceTypes.TryAdd(serviceType.FullName, serviceType);
54+
}
55+
56+
foreach (var serviceName in _serviceNameSet)
57+
{
58+
string subServiceName = serviceName[1..];
59+
foreach (var service in _services)
5760
{
58-
_types.TryAdd(service.ServiceType.FullName, service.ServiceType);
61+
if (serviceName.StartsWith("*") && service.ServiceType.Name.EndsWith(subServiceName))
62+
{
63+
_serviceTypes.TryAdd(service.ServiceType.FullName, service.ServiceType);
64+
continue;
65+
}
66+
67+
if (service.ServiceType.Name == serviceName)
68+
{
69+
_serviceTypes.TryAdd(service.ServiceType.FullName, service.ServiceType);
70+
}
5971
}
6072
}
61-
return this;
62-
}
6373

64-
public IDictionary<string, Type> GetTypes()
65-
{
66-
return _types.ToDictionary(i=>i.Key,i=>i.Value);
74+
foreach (var nameSpace in _serviceNameSpaceSet)
75+
{
76+
foreach (var service in _services)
77+
{
78+
if (service.ServiceType.Namespace == nameSpace)
79+
{
80+
_serviceTypes.TryAdd(service.ServiceType.FullName, service.ServiceType);
81+
}
82+
}
83+
}
84+
85+
return _serviceTypes.ToDictionary(i => i.Key, i => i.Value);
6786
}
6887

6988
public IEnumerable<Type> GetFilterTypes()

src/DotNetCoreRpc.Server/ServerServiceCollectionExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.DependencyInjection.Extensions;
34

45
namespace DotNetCoreRpc.Server
56
{
@@ -9,7 +10,7 @@ public static IServiceCollection AddDotNetCoreRpcServer(this IServiceCollection
910
{
1011
RpcServerOptions rpcServerOptions = new RpcServerOptions(services);
1112
options.Invoke(rpcServerOptions);
12-
services.AddSingleton(rpcServerOptions);
13+
services.TryAddSingleton(rpcServerOptions);
1314
return services;
1415
}
1516
}

0 commit comments

Comments
 (0)