99using System . Text . Json ;
1010using System . Threading . Tasks ;
1111using DotNetCoreRpc . Core ;
12- using DotNetCoreRpc . Core . RpcBuilder ;
12+ using DotNetCoreRpc . Server . RpcBuilder ;
1313using Microsoft . AspNetCore . Http ;
14+ using Microsoft . Extensions . DependencyInjection ;
1415
1516namespace 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 )
0 commit comments