1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
99
100
101
102
103
104
105
#region
 
using System;
using System.ServiceModel;
 
#endregion
 
namespace CSFrameworkV5.WebRef
{
    /// <summary>
    ///     WCF客户端代理层-基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class WCF_Base<T> where T : class
    {
        private void CloseService(ICommunicationObject svc)
        {
            try
            {
                //查看连接状态,关闭WCF                
                var state = svc.State; //创建WCF通道,默认State=Opened;
 
                svc.Close();
 
                //svc.Close();//测试关闭状态下,调用Close
            }
            catch
            {
            }
        }
 
        /// <summary>
        ///     执行WCF方法
        /// </summary>
        /// <param name="action">调用WCF接口核心逻辑</param>
        protected void Excute(Action<T> action)
        {
            T svc = null;
 
            try
            {
                svc = WCFFactory.Create<T>();
                action(svc);
                CloseService(svc as ICommunicationObject); //关闭WCF服务
            }
            catch (Exception ex)
            {
                if (svc != null) CloseService(svc as ICommunicationObject);
 
                throw ex;
            }
        }
 
        /// <summary>
        ///     执行WCF方法
        /// </summary>
        /// <typeparam name="R">数据类型</typeparam>
        /// <param name="action">调用WCF接口核心逻辑</param>
        /// <returns></returns>
        protected R Excute<R>(Func<T, R> action)
        {
            T svc = null;
 
            try
            {
                svc = WCFFactory.Create<T>(); //创建WCF接口实例                
                var result = action(svc); //调用接口逻辑
                CloseService(svc as ICommunicationObject); //关闭WCF服务
                return result;
            }
            catch (Exception ex)
            {
                if (svc != null) CloseService(svc as ICommunicationObject);
 
                throw ex;
            }
        }
 
        /// <summary>
        ///     执行指定WCF接口的方法
        /// </summary>
        /// <typeparam name="M">WCF接口</typeparam>
        /// <typeparam name="R">数据类型</typeparam>
        /// <param name="action">调用WCF接口核心逻辑</param>
        /// <returns></returns>
        protected R Excute<R, M>(Func<M, R> action) where M : class
        {
            M svc = null;
 
            try
            {
                svc = WCFFactory.Create<M>(); //创建WCF接口实例                
                var result = action(svc); //调用接口逻辑
                CloseService(svc as ICommunicationObject); //关闭WCF服务
                return result;
            }
            catch (Exception ex)
            {
                if (svc != null) CloseService(svc as ICommunicationObject);
 
                throw ex;
            }
        }
    }
}