Skip to content

Commit 82a5ba7

Browse files
author
liguoliang
committed
重构服务端
1 parent 276e8eb commit 82a5ba7

13 files changed

Lines changed: 67 additions & 111 deletions

src/DotNetCoreRpc.Core/DotNetCoreRpc.Core.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,4 @@
99
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
1010
<PackageReference Include="System.Text.Json" Version="6.0.7" />
1111
</ItemGroup>
12-
<ItemGroup>
13-
<Folder Include="RpcBuilder\" />
14-
</ItemGroup>
1512
</Project>

src/DotNetCoreRpc.Core/FromServicesAttribute.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/DotNetCoreRpc.Server/DotNetCoreRpc.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
9+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1010
</ItemGroup>
1111
<ItemGroup>
1212
<ProjectReference Include="..\DotNetCoreRpc.Core\DotNetCoreRpc.Core.csproj" />

src/DotNetCoreRpc.Server/DotNetCoreRpcMiddleware.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@
99
using System.Text.Json;
1010
using System.Threading.Tasks;
1111
using DotNetCoreRpc.Core;
12-
using DotNetCoreRpc.Core.RpcBuilder;
12+
using DotNetCoreRpc.Server.RpcBuilder;
1313
using Microsoft.AspNetCore.Http;
14+
using Microsoft.Extensions.DependencyInjection;
1415

1516
namespace DotNetCoreRpc.Server
1617
{
1718
public class DotNetCoreRpcMiddleware
1819
{
19-
private readonly IDictionary<string, Type> _serviceTypes;
20-
private readonly IEnumerable<Type> _filterTypes;
20+
private readonly RpcServerOptions _rpcServerOptions;
2121

2222
public DotNetCoreRpcMiddleware(RequestDelegate next, RpcServerOptions rpcServerOptions)
2323
{
24-
_serviceTypes = rpcServerOptions.GetRegisterTypes();
25-
_filterTypes = rpcServerOptions.GetFilterTypes();
24+
_rpcServerOptions = rpcServerOptions;
2625
}
2726

2827
public async Task InvokeAsync(HttpContext context)
2928
{
3029
var requestContent = await context.Request.ReadStringAsync();
31-
ResponseModel responseModel = new ResponseModel{ Code = (int)HttpStatusCode.InternalServerError };
30+
ResponseModel responseModel = new ResponseModel { Code = (int)HttpStatusCode.InternalServerError };
3231
if (string.IsNullOrEmpty(requestContent))
3332
{
3433
responseModel.Message = "未读取到请求信息";
@@ -44,13 +43,6 @@ public async Task InvokeAsync(HttpContext context)
4443
return;
4544
}
4645

47-
if (!_serviceTypes.ContainsKey(requestModel.TypeFullName))
48-
{
49-
responseModel.Message = $"{requestModel.TypeFullName}未注册";
50-
await context.Response.WriteAsync(responseModel.ToJson());
51-
return;
52-
}
53-
5446
await HandleRequest(context, requestModel);
5547
return;
5648
}
@@ -61,8 +53,8 @@ public async Task InvokeAsync(HttpContext context)
6153
/// <returns></returns>
6254
private async Task HandleRequest(HttpContext context, RequestModel requestModel)
6355
{
64-
Type serviceType = _serviceTypes[requestModel.TypeFullName];
65-
var instance = context.RequestServices.GetService(serviceType);
56+
var serviceType = _rpcServerOptions.GetServiceType(requestModel.TypeFullName);
57+
var instance = context.RequestServices.GetRequiredService(serviceType);
6658
var instanceType = instance.GetType();
6759
var method = instanceType.GetMethod(requestModel.MethodName);
6860
var methodParamters = method.GetParameters();
@@ -113,7 +105,7 @@ private AspectPiplineBuilder CreatPipleline(RpcContext aspectContext)
113105
await aspectContext.HttpContext.Response.WriteAsync(responseModel.ToJson());
114106
});
115107

116-
List<RpcFilterAttribute> interceptorAttributes = RpcFilterUtils.GetFilterAttributes(aspectContext, _filterTypes);
108+
List<RpcFilterAttribute> interceptorAttributes = RpcFilterUtils.GetFilterAttributes(aspectContext, _rpcServerOptions.GetFilterTypes());
117109
if (interceptorAttributes.Any())
118110
{
119111
foreach (var item in interceptorAttributes)

src/DotNetCoreRpc.Core/RpcBuilder/AspectPiplineBuilder.cs renamed to src/DotNetCoreRpc.Server/RpcBuilder/AspectPiplineBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55

6-
namespace DotNetCoreRpc.Core.RpcBuilder
6+
namespace DotNetCoreRpc.Server.RpcBuilder
77
{
88
public class AspectPiplineBuilder
99
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace DotNetCoreRpc.Server.RpcBuilder
4+
{
5+
[AttributeUsage(AttributeTargets.Property)]
6+
public class FromServicesAttribute : Attribute
7+
{
8+
9+
}
10+
}

src/DotNetCoreRpc.Core/RpcBuilder/RpcContext.cs renamed to src/DotNetCoreRpc.Server/RpcBuilder/RpcContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Reflection;
33
using Microsoft.AspNetCore.Http;
44

5-
namespace DotNetCoreRpc.Core.RpcBuilder
5+
namespace DotNetCoreRpc.Server.RpcBuilder
66
{
77
public class RpcContext
88
{

src/DotNetCoreRpc.Core/RpcBuilder/RpcFilterAttribute.cs renamed to src/DotNetCoreRpc.Server/RpcBuilder/RpcFilterAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33

4-
namespace DotNetCoreRpc.Core.RpcBuilder
4+
namespace DotNetCoreRpc.Server.RpcBuilder
55
{
66
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
77
public abstract class RpcFilterAttribute : Attribute

src/DotNetCoreRpc.Core/RpcFilterUtils.cs renamed to src/DotNetCoreRpc.Server/RpcBuilder/RpcFilterUtils.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Reflection;
6-
using DotNetCoreRpc.Core.RpcBuilder;
6+
using DotNetCoreRpc.Core;
77
using Microsoft.Extensions.DependencyInjection;
88

9-
namespace DotNetCoreRpc.Core
9+
namespace DotNetCoreRpc.Server.RpcBuilder
1010
{
1111
public static class RpcFilterUtils
1212
{
@@ -22,7 +22,8 @@ public static List<RpcFilterAttribute> GetFilterAttributes(RpcContext aspectCont
2222
var methondInfo = aspectContext.Method;
2323

2424
var methondInterceptorAttributes = _methodFilters.GetOrAdd($"{methondInfo.DeclaringType.FullName}#{methondInfo.Name}",
25-
key => {
25+
key =>
26+
{
2627
var methondAttributes = methondInfo.GetCustomAttributes(true)
2728
.Where(i => typeof(RpcFilterAttribute).IsAssignableFrom(i.GetType()))
2829
.Cast<RpcFilterAttribute>().ToList();

src/DotNetCoreRpc.Core/RpcBuilder/RpcRequestDelegate.cs renamed to src/DotNetCoreRpc.Server/RpcBuilder/RpcRequestDelegate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33

4-
namespace DotNetCoreRpc.Core.RpcBuilder
4+
namespace DotNetCoreRpc.Server.RpcBuilder
55
{
66
public delegate Task RpcRequestDelegate(RpcContext context);
77
}

0 commit comments

Comments
 (0)