Esempio n. 1
0
 public void RegisterServerType(params Type[] serverType)
 {
     foreach (var item in serverType)
     {
         ServerAssemblyUtil.InstallType(item);
     }
 }
Esempio n. 2
0
 public void Start(params string[] assemblyNameList)
 {
     ServerAssemblyUtil.AddAssemblyList(assemblyNameList);
     _socketRemotingServer = new SocketRemotingServer(_iPEndPoint).RegisterRequestHandler(100, new NRpcHandle(_gloabFilterList));
     _socketRemotingServer.Start();
     _routeCoordinator.RegisterAsync(_iPEndPoint).Wait();
 }
Esempio n. 3
0
        /// <summary>
        /// 请求处理
        /// </summary>
        /// <param name="remotingRequest"></param>
        /// <returns></returns>
        public RemotingResponse HandleRequest(RemotingRequest remotingRequest)
        {
            var requestMethodInfo = DependencyManage.Resolve <IMethodCallSerializer>().Deserialize <RpcMethodCallInfo>(remotingRequest.Body);
            var classType         = ServerAssemblyUtil.GetType(requestMethodInfo.TypeName);

            using (var scope = DependencyManage.BeginScope())
            {
                var        obj = scope.ServiceProvider.GetService(classType);
                MethodInfo executeMethodInfo = null;
                try
                {
                    LogUtil.InfoFormat("Begin Deal Rpc Request:{0}-{1}", requestMethodInfo.TypeName, requestMethodInfo.MethodName);
                    executeMethodInfo = ServerAssemblyUtil.GetMethod(requestMethodInfo.MethodName, classType) as MethodInfo;
                    if (executeMethodInfo == null)
                    {
                        _rpcMonitor.AddError(new RpcMonitorRequestErrorInfo(requestMethodInfo, remotingRequest, "Not Found Method"));
                        return(remotingRequest.CreateNotFoundResponse($"{requestMethodInfo.TypeName},{requestMethodInfo.MethodName}"));
                    }
                    else
                    {
                        var delegateType = executeMethodInfo.GetMethodReturnType();
                        OnActionExecuting(executeMethodInfo, requestMethodInfo.Parameters);
                        var executeResult = executeMethodInfo.Invoke(obj, requestMethodInfo.Parameters);
                        RemotingResponse remotingResponse;
                        if (delegateType == MethodType.SyncAction)
                        {
                            remotingResponse = remotingRequest.CreateSuccessResponse(ResponseUtil.NoneBodyResponse);
                        }
                        else if (delegateType == MethodType.SyncFunction)
                        {
                            remotingResponse = remotingRequest.CreateSuccessResponse(GetBody(executeResult, executeMethodInfo));
                        }
                        else if (delegateType == MethodType.AsyncAction)
                        {
                            var task = (Task)executeResult;
                            task.Wait();
                            remotingResponse = remotingRequest.CreateSuccessResponse(ResponseUtil.NoneBodyResponse);
                        }
                        else
                        {
                            var resultType = executeMethodInfo.ReturnType.GetGenericArguments()[0];
                            var mi         = HandleAsyncMethodInfo.MakeGenericMethod(resultType);
                            var result     = mi.Invoke(this, new[] { executeResult });
                            remotingResponse = remotingRequest.CreateSuccessResponse(GetBody(result, executeMethodInfo));
                        }
                        LogUtil.InfoFormat("Rpc Method Dealed:{0}-{1}", requestMethodInfo.TypeName, requestMethodInfo.MethodName);
                        OnActionExecuted(executeMethodInfo);
                        return(remotingResponse);
                    }
                }
                catch (Exception e)
                {
                    HandleException(executeMethodInfo, e);
                    _rpcMonitor.AddError(new RpcMonitorRequestErrorInfo(requestMethodInfo, remotingRequest, e));
                    return(remotingRequest.CreateDealErrorResponse());
                }
            }
        }