songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
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
using System;
using System.Linq;
using System.ServiceModel;
using System.Collections.Generic;
using System.Xml;
using System.ServiceModel.Description;
 
namespace BatchService.Framework.Utility
{
    /// <summary>
    /// Wcf°ïÖúÀà
    /// </summary>
    public class WcfServiceProxy
    {        
        /// <summary>
        /// ¶¯Ì¬´´½¨Wcf¿Í»§¶Ë´úÀíʵÀý
        /// </summary>
        /// <typeparam name="T">Contract/½Ó¿Ú</typeparam>
        /// <param name="uri">Wcf·þÎñµØÖ·</param>
        /// <returns>´úÀíʵÀý</returns>
        public static T CreateServiceProxy<T>(string uri)
        {
            var key = string.Format("{0} - {1}", typeof(T), uri);
 
            if (Caching.Get(key) == null)
            {
                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = maxReceivedMessageSize;
                binding.ReaderQuotas = new XmlDictionaryReaderQuotas();
                binding.ReaderQuotas.MaxStringContentLength = maxReceivedMessageSize;
                binding.ReaderQuotas.MaxArrayLength = maxReceivedMessageSize;
                binding.ReaderQuotas.MaxBytesPerRead = maxReceivedMessageSize;
                binding.OpenTimeout = timeout;
                binding.ReceiveTimeout = timeout;
                binding.SendTimeout = timeout;
 
                var chan = new ChannelFactory<T>(binding, new EndpointAddress(uri));
 
                foreach (OperationDescription op in chan.Endpoint.Contract.Operations)
                {
                    var dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>();
                    if (dataContractBehavior != null)
                        dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
                }
 
 
                chan.Open();
 
                var service = chan.CreateChannel();
                Caching.Set(key, service);
 
                return service;
            }
            else
            {
                return (T)Caching.Get(key);
            }
        }
 
        private const int maxReceivedMessageSize = 2147483647;
        private static TimeSpan timeout = TimeSpan.FromMinutes(10);
    }
}