示例#1
0
 public void RegisterServerType(params Type[] serverType)
 {
     foreach (var item in serverType)
     {
         ServerAssemblyUtil.InstallType(item);
     }
 }
示例#2
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.BeginLeftScope())
            {
                var        obj = scope.Resolve(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)
                {
                    _rpcMonitor.AddError(new RpcMonitorRequestErrorInfo(requestMethodInfo, remotingRequest, e));
                    HandleException(executeMethodInfo, e);
                    return(remotingRequest.CreateDealErrorResponse());
                }
            }
        }
示例#3
0
 public void RegisterServerAssembly(Assembly assembly)
 {
     ServerAssemblyUtil.InstallAssembly(assembly);
 }