xuruiqian
2021-05-25 c0bbcd97fa2f130a7a60d02873fbf9038bdd4c15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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<dynamic>(json);
                return data;
            }
            else
            {
                return null;
            }
        }
 
        public IQueryable<T> Sort<T>(IQueryable<T> 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<T>(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
    }
}