using System; using System.IO; using System.Linq; using System.Linq.Expressions; using System.ServiceModel; using System.ServiceModel.Channels; using System.Web.Script.Serialization; namespace GTech.Solution.Api { public abstract class ApiServicesBase { #region Properties And Feilds #endregion #region common method public dynamic GetPostData(Stream stream) { if (stream.Length != 0) { StreamReader sr = new StreamReader(stream); var json = sr.ReadToEnd(); JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); dynamic data = javaScriptSerializer.Deserialize(json); return data; } else { return null; } } public IQueryable Sort(IQueryable data, string orderByName, bool orderDirection) { try { if (string.IsNullOrEmpty(orderByName)) { return data; } var dataType = typeof(T); var property = dataType.GetProperty(orderByName); if (property == null) return data; var paramter = Expression.Parameter(dataType, string.Empty); var propertyAccess = Expression.MakeMemberAccess(paramter, property); var orderByExp = Expression.Lambda(propertyAccess, paramter); string command = orderDirection ? "OrderBy" : "OrderByDescending"; var resultExp = Expression.Call(typeof(Queryable), command, new Type[] { dataType, property.PropertyType }, data.Expression, Expression.Quote(orderByExp)); return data.Provider.CreateQuery(resultExp); } catch (Exception ex) { return null; } } public Binding ConfigBinding() { BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); binding.ReceiveTimeout = new TimeSpan(0, 1, 00); binding.SendTimeout = new TimeSpan(0, 1, 00); binding.CloseTimeout = new TimeSpan(0, 0, 20); binding.OpenTimeout = new TimeSpan(0, 0, 20); binding.MaxBufferPoolSize = 524288; binding.MaxReceivedMessageSize = 655360000; binding.ReaderQuotas.MaxDepth = 32; binding.ReaderQuotas.MaxStringContentLength = 8192; binding.ReaderQuotas.MaxArrayLength = 1638400; binding.ReaderQuotas.MaxBytesPerRead = 40960000; binding.ReaderQuotas.MaxNameTableCharCount = 1638400; return binding; } public static void CopyModel(object target, object source) { Type type1 = target.GetType(); Type type2 = source.GetType(); foreach (var mi in type1.GetProperties()) { var des = type1.GetProperty(mi.Name); if (des != null) { try { des.SetValue(target, mi.GetValue(source, null), null); } catch (Exception ex) { } } } } #endregion } }