Ejemplo n.º 1
0
        private T PrivateExecute <T>(string url, IWMSRequest <T> request, Dictionary <string, string> customHeaders = null) where T : WMSResponse
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (string.IsNullOrWhiteSpace(url))
            {
                T response = ToObject <T>(GetJson(new ErrorWMSResponse {
                    Code = ErrorCodeMessage.ServiceAddressNotFound, Message = ErrorCodeMessage.ServiceAddressNotFoundMessage
                }));
                return(response);
            }
            APIExecuteStatistics data = new APIExecuteStatistics();

            data.StartTime      = DateTime.Now /*仅用于统计耗时,可以使用DateTime.Now*/;
            data.ServiceAddress = url;
            data.APIPath        = request.GetAPIPath();
            url               = url.TrimEnd('/') + "/" + request.GetAPIPath().TrimStart('/');
            data.FullURL      = url;
            data.RequestType  = request == null ? null : request.GetType();
            data.ResponseType = typeof(T);
            T resp = null;

            try
            {
                if (customHeaders == null)
                {
                    customHeaders = new Dictionary <string, string>();
                }
                foreach (var item in Properties)
                {
                    customHeaders.Add(item.Key, item.Value);
                }
                int currentMinute = DateTime.Now.Minute /*仅记录当前分钟数,可以使用DateTime.Now*/;
                if (lastMinute != currentMinute)
                {
                    //lastMinute = currentMinute;
                    //customHeaders["statistics-IP"] = ComputerHelper.GetIPAddress();
                }
                //lock (_bytesLocker)
                //{
                //    customHeaders["statistics-LastBytesSent"] = LastSentBytes.ToString("F0");
                //    customHeaders["statistics-LastBytesReceived"] = LastReceivedBytes.ToString("F0");
                //    LastSentBytes = LastReceivedBytes = 0;
                //}
                //customHeaders["statistics-BytesSent"] = SentBytes.ToString("F0");
                //customHeaders["statistics-BytesReceived"] = ReceivedBytes.ToString("F0");
                string json = GetJson(request);
                data.RequestContent = json;
                string response = PostData(url, json, ref data, customHeaders);
                data.ResponseContent = response;
                if (string.IsNullOrEmpty(response) || response.Trim().Length < 1)
                {
                    resp = ToObject <T>(GetJson(new ErrorWMSResponse {
                        Code = ErrorCodeMessage.EmptyResponse, Message = ErrorCodeMessage.EmptyResponseMessage
                    }));
                }
                else
                {
                    resp = ToObject <T>(response);
                }
                resp.Body        = response;
                resp.RequestJson = json;
                data.EndTime     = DateTime.Now /*仅用于统计耗时,可以使用DateTime.Now*/;
                data.ElapseTime  = (long)(data.EndTime - data.StartTime).TotalMilliseconds;
                data.Code        = resp.Code;
            }
            catch (Exception ex)
            {
                data.Exception = ex;
                throw ex;
            }
            finally
            {
                OnAPIExecuted(new APIExecutedEventArgs {
                    Data = data
                });
            }
            return(resp);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 调用指定服务
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="url"></param>
 /// <param name="request"></param>
 /// <returns></returns>
 public T Execute <T>(string url, IWMSRequest <T> request) where T : WMSResponse
 {
     return(PrivateExecute(url, request));
 }
Ejemplo n.º 3
0
 internal T PrivateExecute <T>(IWMSRequest <T> request, Dictionary <string, string> customHeaders = null) where T : WMSResponse
 {
     return(PrivateExecute(DefaultServiceAddress, request, customHeaders));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 调用默认服务
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="request"></param>
 /// <returns></returns>
 public T Execute <T>(IWMSRequest <T> request) where T : WMSResponse
 {
     return(Execute(DefaultServiceAddress, request));
 }