示例#1
0
        /// <summary>
        /// 异步调用方法
        /// </summary>
        /// <param name="reqMsg"></param>
        public ResponseMessage CallService(RequestMessage reqMsg)
        {
            var context = OperationContext.Current;

            //实例化异步调用器
            var worker = smart.QueueWorkItem <OperationContext, RequestMessage, ResponseMessage>
                             (GetResponse, context, reqMsg);

            //等待响应
            ResponseMessage resMsg = null;

            try
            {
                resMsg = worker.GetResult(elapsedTime, true);
            }
            catch (Exception ex)
            {
                //结束当前线程
                if (!worker.IsCompleted)
                {
                    worker.Cancel(true);
                }

                var body = string.Format("Call service ({0}, {1}) timeout ({2}) ms. Error: {4}\r\nParameters => {3}"
                                         , reqMsg.ServiceName, reqMsg.MethodName, (int)elapsedTime.TotalMilliseconds, reqMsg.Parameters.ToString(), ex.Message);

                //获取异常
                var error = IoCHelper.GetException(OperationContext.Current, reqMsg, body);

                //将异常信息写出
                logger.Write(error);

                //处理异常
                resMsg = new ResponseMessage
                {
                    TransactionId = reqMsg.TransactionId,
                    ReturnType    = reqMsg.ReturnType,
                    ServiceName   = reqMsg.ServiceName,
                    MethodName    = reqMsg.MethodName,
                    Parameters    = reqMsg.Parameters,
                    Error         = error
                };
            }

            //返回响应的消息
            return(resMsg);
        }
示例#2
0
        /// <summary>
        /// Calls the service.
        /// </summary>
        /// <param name="reqMsg">The MSG.</param>
        /// <returns>The msg.</returns>
        public ResponseMessage CallService(RequestMessage reqMsg)
        {
            //运行服务返回值
            var resMsg = Run(reqMsg);

            //如果出错,通知客户端
            if (resMsg.IsError)
            {
                string body = string.Format("Remote client【{0}】call service ({1},{2}) error.\r\nParameters => {3}\r\nMessage => {4}",
                                            reqMsg.Message, reqMsg.ServiceName, reqMsg.MethodName, reqMsg.Parameters.ToString(), resMsg.Message);

                //获取异常
                var exception = IoCHelper.GetException(OperationContext.Current, reqMsg, body, resMsg.Error);

                logger.Write(exception);
            }

            return(resMsg);
        }