Пример #1
0
 /// <summary>
 /// 调用服务器端一个函数,并返回一个值
 /// </summary>
 /// <typeparam name="T">返回值类型</typeparam>
 /// <param name="invoke">参数</param>
 /// <returns>返回值</returns>
 protected T Invoke <T>(WcfInvokeInfo invoke)
 {
     try
     {
         byte[] bs = CommunicationSerializer.SerializeDataContract(invoke, "Communication", "www.qcw.com", KnownTypeRegister.GetKnownTypes());
         bs = Process(bs, (buffer) =>
         {
             var service = GetService();
             return(service.Process(buffer));
         });
         var result = CommunicationSerializer.DeserializeDataContract(typeof(WcfInvokeResult), "Communication", "www.qcw.com", KnownTypeRegister.GetKnownTypes(), bs) as WcfInvokeResult;
         if (result != null)
         {
             if (!result.IsSuccess)
             {
                 throw new WcfException(result.ErrorMessage);
             }
             return((T)result.Data);
         }
     }
     catch (Exception ex)
     {
         if (ex.Message.IndexOf("http:") > 0)
         {
             var writer = Common.Log.LogWriterGetter.GetLogWriter();
             writer.Write("WCF连接异常", "Invoke_WCF连接异常", Common.Log.LogType.Error, "WCF连接异常", ex.ToString());
             throw new Exception("您好,目前服务器繁忙,请稍后再试!");
         }
         else
         {
             throw new Exception(ex.Message);
         }
     }
     return(default(T));
 }
Пример #2
0
        /// <summary>
        /// 调用服务器端一个无返回值的函数
        /// </summary>
        /// <param name="invoke">参数</param>
        protected void InvokeViaOneWay(WcfInvokeInfo invoke)
        {
            var bs = CommunicationSerializer.SerializeDataContract(invoke, "Communication", "www.qcw.com", KnownTypeRegister.GetKnownTypes());

            Process(bs, buffer =>
            {
                var service = GetService();
                service.ProcessViaOneWay(buffer);
                return(null);
            });
        }
Пример #3
0
        private static void WriteException(WcfInvokeInfo invokeInfo, Exception ex)
        {
            var logWriter = LogWriterGetter.GetLogWriter();

            if (logWriter != null)
            {
                for (int i = 0; i < invokeInfo.Parameters.Length; i++)
                {
                    if (invokeInfo.Parameters[i] != null)
                    {
                        ex.Data.Add("[" + i + "]" + invokeInfo.Parameters[i].GetType().Name, "值:" + invokeInfo.Parameters[i]);
                    }
                }
                logWriter.Write("Error", invokeInfo.MethodName, ex);
            }
        }
Пример #4
0
        private static void WriteLog(WcfInvokeInfo invokeInfo, int time)
        {
            var logWriter = LogWriterGetter.GetLogWriter();

            if (logWriter != null)
            {
                var sb = new StringBuilder();
                if (invokeInfo != null)
                {
                    sb.AppendLine("执行函数:" + invokeInfo.MethodName);
                    foreach (var p in invokeInfo.Parameters)
                    {
                        if (p != null)
                        {
                            sb.AppendLine(string.Format("  参数类型:{0};值:{1}", p.GetType().Name, p));
                        }
                    }
                }
                logWriter.Write("Timeout\\Info-", invokeInfo.MethodName + "_" + time, LogType.Information, invokeInfo.MethodName + "_" + time, sb.ToString());
            }
        }
Пример #5
0
        private WcfInvokeResult DoWork(WcfInvokeInfo invokeInfo)
        {
            var rst = new WcfInvokeResult();

            try
            {
                try
                {
                    var start = DateTime.Now;
                    if (string.IsNullOrWhiteSpace(invokeInfo.MethodName))
                    {
                        throw new LogicException("传入函数名为空");
                    }
                    var mi = GetType().GetMethod(invokeInfo.MethodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
                    if (mi == null)
                    {
                        throw new LogicException("传入函数名不存在 - " + invokeInfo.MethodName);
                    }
                    var result = mi.Invoke(this, invokeInfo.Parameters);  //所有被调用方法,必须返回WcfInvokeResult对象
                    var end    = DateTime.Now;
                    var span   = end - start;
                    if (span.Seconds >= 5)
                    {
                        WriteLog(invokeInfo, (int)span.TotalMilliseconds);
                    }
                    rst.IsSuccess = true;
                    rst.Data      = result;
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
                catch (Exception ex)
                {
                    WriteException(invokeInfo, ex);

                    rst.ErrorMessage = ex.Message;
                    rst.ErrorDetail  = "调用函数异常 - " + invokeInfo.MethodName + "  " + ex;
                }
            }
            catch (WcfException ex)
            {
                WriteException(invokeInfo, ex);

                rst.ErrorCategory = "WCF";
                rst.ErrorMessage  = ex.Message;
                rst.ErrorDetail   = ex.ToString();
            }
            catch (LogicException ex)   // 逻辑错误,不需要写日志
            {
                rst.ErrorCategory = "LOGIC";
                rst.ErrorMessage  = ex.Message;
                rst.ErrorDetail   = ex.ToString();
            }
            catch (AuthException ex)    // 身份验证失败,不需要写日志
            {
                rst.ErrorCategory = "AUTH";
                rst.ErrorMessage  = ex.Message;
                rst.ErrorDetail   = ex.ToString();
            }
            catch (Exception ex)
            {
                WriteException(invokeInfo, ex);

                rst.ErrorCategory = ex.GetType().FullName;
                rst.ErrorMessage  = ex.Message;
                rst.ErrorDetail   = "调用函数异常 - " + invokeInfo.MethodName + "  " + ex;
            }
            return(rst);
        }