/// <summary> /// 响应消息 /// </summary> /// <param name="proxy"></param> /// <param name="method"></param> /// <param name="parameters"></param> /// <returns></returns> public object Invoke(object proxy, System.Reflection.MethodInfo method, object[] parameters) { if (client.CommunicationState == CommunicationStates.Connected) { //定义回调的消息 var message = new CallbackMessage { ServiceName = callType.FullName, MethodName = method.ToString(), Parameters = parameters }; //加入队列 lock (queue.SyncRoot) { queue.Enqueue(new CallbackInfo { Client = client, Message = message }); } //返回null return(null); } else { throw new SocketException((int)SocketError.ConnectionAborted); } }
private string ParsCommand(System.Reflection.MethodInfo command) { string pattern = @"\<(.*?)\>"; var value = Regex.Match(command.ToString(), pattern).Groups[1].Value; return(value); }
/// <summary> /// 响应委托 /// </summary> /// <param name="proxy"></param> /// <param name="method"></param> /// <param name="parameters"></param> /// <returns></returns> public object Invoke(object proxy, System.Reflection.MethodInfo method, object[] parameters) { object returnValue = null; var collection = IoCHelper.CreateParameters(method, parameters); string cacheKey = IoCHelper.GetCacheKey(serviceType, method, collection); var cacheValue = cache.GetCache <CacheObject>(cacheKey); //缓存无值 if (cacheValue == null) { //调用方法 var resMsg = InvokeMethod(method, collection); if (resMsg != null) { returnValue = resMsg.Value; //处理参数 IoCHelper.SetRefParameterValues(method, resMsg.Parameters, parameters); //如果需要缓存,则存入本地缓存 if (returnValue != null && cacheTimes.ContainsKey(method.ToString())) { int cacheTime = cacheTimes[method.ToString()]; cacheValue = new CacheObject { Value = resMsg.Value, Parameters = resMsg.Parameters }; cache.InsertCache(cacheKey, cacheValue, cacheTime); } } } else { //处理返回值 returnValue = cacheValue.Value; //处理参数 IoCHelper.SetRefParameterValues(method, cacheValue.Parameters, parameters); } //返回结果 return(returnValue); }
public static string FullNameWithArgTypes(this System.Reflection.MethodInfo methodInfo) { // Is there a better way of getting a fully qualified signature that includes the parameter types? var signatureWithReturnType = methodInfo.ToString(); var positionOfSpaceAfterReturnType = signatureWithReturnType.IndexOf(' '); var signatureWithoutReturnType = signatureWithReturnType.Substring(positionOfSpaceAfterReturnType + 1); var methodName = String.Format("{0}.{1}", methodInfo.DeclaringType.FullName, signatureWithoutReturnType); return(methodName); }
/// <summary> /// 调用方法返回 /// </summary> /// <param name="method"></param> /// <param name="collection"></param> /// <returns></returns> private ResponseMessage InvokeMethod(System.Reflection.MethodInfo method, ParameterCollection collection) { #region 设置请求信息 var reqMsg = new RequestMessage { AppName = config.AppName, //应用名称 HostName = hostName, //客户端名称 IPAddress = ipAddress, //客户端IP地址 ReturnType = method.ReturnType, //返回类型 ServiceName = serviceType.FullName, //服务名称 MethodName = method.ToString(), //方法名称 TransactionId = Guid.NewGuid(), //传输ID号 MethodInfo = method, //设置调用方法 Parameters = collection //设置参数 }; #endregion //调用服务 return(CallService(reqMsg)); }
/// <summary> /// 获取缓存Key值 /// </summary> /// <param name="serviceType"></param> /// <param name="method"></param> /// <param name="collection"></param> /// <returns></returns> public static string GetCacheKey(Type serviceType, System.Reflection.MethodInfo method, ParameterCollection collection) { var opContract = CoreHelper.GetMemberAttribute <OperationContractAttribute>(method); if (opContract != null && !string.IsNullOrEmpty(opContract.CacheKey)) { string cacheKey = opContract.CacheKey; var index = 0; foreach (var key in collection.Keys) { string name = "{" + key + "}"; if (cacheKey.Contains(name)) { var parameter = collection[key]; if (parameter != null) { cacheKey = cacheKey.Replace(name, "|" + parameter.ToString() + "|"); } else { cacheKey = cacheKey.Replace(name, "|"); } } index++; } return(string.Format("{0}_{1}", serviceType.FullName, cacheKey)); } else { //返回默认的缓存key var cacheKey = string.Format("{0}${1}${2}", serviceType.FullName, method.ToString(), collection.ToString()); return(string.Format("CastleCache_{0}", GetMD5String(cacheKey))); } }