WCF客戶端配置以及代理-----基於DDD領域驅動設計的WCF+EF+WPF分層框架(4)


寫在最前面:轉載請注明出處

目錄置頂:

前兩天出差,所以這一系列的博客寫到3就耽誤了幾天。今天回來了,接着寫。

WCF客戶端配置以及代理

 這一部分需要講的內容不多,因為我沒有使用引用服務來添加WCF服務,所以App.Config里面也沒有自動生成配置信息以及Binding和Endpoint。如果需要引用的WCF服務很多的話,這個App.config看起來就很龐大,看着很不舒服。所以我在ACS.OA.Base項目中添加了一個幫助類(架構搭建--------------------基於DDD領域驅動設計的WCF+EF+WPF分層框架(2) 中也有簡單介紹)WCFHandler 類,下面是代碼:

  1     /// <summary>
  2     /// 艾克仕網絡雲OA WCF配置
  3     /// </summary>
  4     public class WCFHandler
  5     {
  6         public static T CreateHttpServiceClient<T>(string webAddress, string serviceName)
  7         {
  8             T t = default(T);
  9             object obj = Activator.CreateInstance(typeof(T), new object[]
 10             {
 11                 GetHttpBinding(),
 12                 GetEndpoint(webAddress, serviceName)
 13             });
 14             t = (T)(obj);
 15             return t;
 16         }
 17 
 18         public static T CreateTcpServiceClient<T>(string webAddress, string serviceName, bool isStream=false)
 19         {
 20             T t = default(T);
 21             object obj;
 22             if (isStream) //流傳輸
 23             {
 24                 obj = Activator.CreateInstance(typeof(T), new object[]
 25                 {
 26                     GetFileTcpBinding(),
 27                     GetEndpoint(webAddress, serviceName)
 28                 });
 29             }
 30             else         //緩存傳輸
 31             {
 32                 obj = Activator.CreateInstance(typeof(T), new object[]
 33                 {
 34                     GetTcpBinding(),
 35                     GetEndpoint(webAddress, serviceName)
 36                 });
 37             }
 38             t = (T)(obj);
 39             return t;
 40         }
 41         
 42 
 43         public static NetTcpBinding GetTcpBinding()
 44         {
 45             return new NetTcpBinding
 46             {
 47                 MaxBufferPoolSize = 2147483646L,
 48                 MaxReceivedMessageSize = 2147483646L,
 49                 CloseTimeout = new TimeSpan(0, 0, 10),
 50                 OpenTimeout = new TimeSpan(0, 0, 10),
 51                 ReceiveTimeout = TimeSpan.MaxValue,
 52                 SendTimeout = new TimeSpan(0, 20, 0),
 53                 ReaderQuotas =
 54                 {
 55                     MaxDepth=64,
 56                     MaxStringContentLength=2147483646,
 57                     MaxArrayLength=2147483646,
 58                     MaxBytesPerRead=2147483646,
 59                     MaxNameTableCharCount=2147483646
 60                 },
 61                 ReliableSession =
 62                 {
 63                     Enabled = true,
 64                     Ordered = true,
 65                     InactivityTimeout = new TimeSpan(0, 0, 10)
 66                 },
 67                 Security =
 68                 {
 69                     Mode=SecurityMode.None,
 70                     Message =
 71                     {
 72                         ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows
 73                     },
 74                     Transport =
 75                     {
 76                         ClientCredentialType =  TcpClientCredentialType.Windows
 77                     }
 78                 },
 79 
 80             };
 81         }
 82 
 83         /// <summary>
 84         /// TCP大文件斷點續傳
 85         /// </summary>
 86         /// <returns></returns>
 87         public static NetTcpBinding GetFileTcpBinding()
 88         {
 89             return new NetTcpBinding
 90             {
 91                 MaxBufferPoolSize = 2147483646L,
 92                 MaxReceivedMessageSize = 2147483646L,
 93                 CloseTimeout = new TimeSpan(0, 0, 10),
 94                 OpenTimeout = new TimeSpan(0, 0, 10),
 95                 ReceiveTimeout = TimeSpan.MaxValue,
 96                 SendTimeout = new TimeSpan(0, 20, 0),
 97                 TransferMode=TransferMode.Streamed,
 98                 ReaderQuotas =
 99                 {
100                     MaxDepth=64,
101                     MaxStringContentLength=2147483646,
102                     MaxArrayLength=2147483646,
103                     MaxBytesPerRead=2147483646,
104                     MaxNameTableCharCount=2147483646
105                 },
106                 //ReliableSession =
107                 //{
108                 //    Enabled = true,
109                 //    Ordered = true,
110                 //    InactivityTimeout = new TimeSpan(1, 0, 0)
111                 //},
112                 //Security =
113                 //{
114                 //    Mode=SecurityMode.None,
115                 //    Message =
116                 //    {
117                 //        ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows
118                 //    },
119                 //    Transport =
120                 //    {
121                 //        ClientCredentialType =  TcpClientCredentialType.Windows
122                 //    }
123                 //},
124 
125             };
126         }
127 
128         public static WSHttpBinding GetHttpBinding()
129         {
130             return new WSHttpBinding
131             {
132                 MaxBufferPoolSize = 2147483646L,
133                 MaxReceivedMessageSize = 2147483646L,
134                 CloseTimeout = new TimeSpan(0, 0, 10),
135                 OpenTimeout = new TimeSpan(0, 0, 10),
136                 ReceiveTimeout = new TimeSpan(0, 20, 0),
137                 SendTimeout = new TimeSpan(0, 20, 0),
138                 ReliableSession =
139                 {
140                     Enabled = true,
141                     Ordered = true,
142                     InactivityTimeout = new TimeSpan(0, 0, 10)
143                 },
144                 UseDefaultWebProxy = false,
145                 Security =
146                 {
147                     Mode = SecurityMode.None,
148                     Message =
149                     {
150                         ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows
151                     },
152                     Transport =
153                     {
154                         ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows
155                     }
156                 },
157             };
158         }
159         public static EndpointAddress GetEndpoint(string webAddress, string serviceName)
160         {
161             Uri uri = new Uri(webAddress + "/" + serviceName + ".svc");
162             return new EndpointAddress(uri, new AddressHeader[0]);
163         }
164 
165         public static EndpointAddress GetIMEndpoint(string webAddress, string serviceName)
166         {
167             Uri uri = new Uri(webAddress + "/" + serviceName + ".svc");
168             return new EndpointAddress(uri, new AddressHeader[0]);
169         }
170     }
WCFHandler View Code

我的WPF客戶端使用的是NET.TCP,Web端使用的是WSHTTP,所以兩個配置信息都有,大家可以做一個參考。

我再把客戶端的App.config配置文件部分代碼貼出來

1  <appSettings>
2     <add key="HTTPAddress" value="http://10.32.100.251/ACS.OA.Test;http://10.32.100.251/ACS.OA.Test;http://lineoa.shijian365.cn/ACS.OA" />
3     <add key="WCFAddress" value="net.tcp://localhost/DDD/ACS.CloudOA.WCFService;net.tcp://10.32.100.251/ACS.OA.Test;net.tcp://oa.acssoft.cn/CloudOA" />
4     <add key="ACS_WebTCPAddress" value="net.tcp://localhost/DDD/ACS_WEB/ACS.WCFService;net.tcp://10.32.100.251/ACS.OA.Test;net.tcp://oa.acssoft.cn/Web" />
5     <!--部署環境 0:開發;1:測試;2:正式-->
6     <add key="DeployType" value="2" />
7   </appSettings>

這里我講一下我的用法:(也希望大家說說你們是如何處理這個事情的,大家可以相互討論一下)

一般我們項目開發都需要經歷 開發、內部測試、正式部署 三個階段 我在config中只需要修改 DeployType 的值就可以將項目修改為 開發、內部測試、正式部署 階段,之后生成項目打包就OK了,是不是很方便???

其實大家可以看到我在 HTTPAddress WCFAddress ACS_WebTCPAddress 三個的value值都用【 ;】分割為三部分,然后我會在ACS.OA.Golbal項目中SystemConfig.cs文件中獲取這里的值,代碼如下:

 1 public static int DeployType = StringHelper.ToInteger(ConfigHelper.GetAppSettingsValue("DeployType"));
 2 
 3 
 4         private static string _wcfaddress = ConfigHelper.GetAppSettingsValue("WCFAddress");
 5         /// <summary>
 6         /// 艾克仕網絡ACS.CloudOA NET.TCP地址
 7         /// </summary>
 8         public static string WCFAddress
 9         {
10             get
11             {
12                 string address = "";
13                 if (string.IsNullOrEmpty(_wcfaddress) || !_wcfaddress.Contains(";"))
14                 {
15                     return address;
16                 }
17                 string[] wcfarr = _wcfaddress.Split(';');
18                 address =  wcfarr.Length > DeployType ? wcfarr[StringHelper.ToInteger(DeployType)] : "";
19                 return address;
20             }
21         }
22 
23         private static string _httpAddress = ConfigHelper.GetAppSettingsValue("HTTPAddress");
24         /// <summary>
25         /// 艾克仕網絡ACS.CloudOA HTTP地址
26         /// </summary>
27         public static string HTTPAddress
28         {
29             get
30             {
31                 string address = "";
32                 if(string.IsNullOrEmpty(_httpAddress) || !_httpAddress.Contains(";"))
33                 {
34                     return address;
35                 }
36                 string[] httparr = _httpAddress.Split(';');
37                 address =  httparr.Length > DeployType ? httparr[StringHelper.ToInteger(DeployType)] : "";
38                 return address;
39             }
40         }
41 
42         private static string _ACS_WebTCPAddress = ConfigHelper.GetAppSettingsValue("ACS_WebTCPAddress");
43         /// <summary>
44         /// 艾克仕網絡ACS.Web服務NET.TCP地址
45         /// </summary>
46         public static string ACS_WebTCPAddress
47         {
48             get
49             {
50                 string address = "";
51                 if (string.IsNullOrEmpty(_ACS_WebTCPAddress) || !_ACS_WebTCPAddress.Contains(";"))
52                 {
53                     return address;
54                 }
55                 string[] wcfarr = _ACS_WebTCPAddress.Split(';');
56                 address = wcfarr.Length > DeployType ? wcfarr[StringHelper.ToInteger(DeployType)] : "";
57                 return address;
58             }
59         }

這樣值就拿到了,這樣做的話就不用每次老板催着要發包測試或者發布新版本的時候手忙腳亂的了。當然我不建議在每次用的時候直接調用,我建議在客戶端本地緩存中保存一下,再使用緩存,要不然每次調用都需要去讀一下config。搭建架構,只要能想到可以優化就要毫不猶豫的去優化,盡管這個地方對整體性能影響很小。

WCF客戶端配置就寫這些了。下面我再簡單講一下WCF代理類:

WCF代理類

 代理類我是單獨建了一個項目ACS.OA.WCFClient,然后每一個需要引用WCF代理類的項目我都會在ACS.OA.WCFClient項目中建立一個文件夾,例如我這里三個項目用到這個代理,ACS.OA.WCFClient項目中就有三個項目對應的文件夾

代理類使用SvcUtil生成,我在這里貼一個代理實例:

 1     [System.Diagnostics.DebuggerStepThroughAttribute()]
 2     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
 3     public partial class SettingServiceClient : System.ServiceModel.ClientBase<ISettingService>, ISettingService
 4     {
 5 
 6         public SettingServiceClient()
 7         {
 8         }
 9 
10         public SettingServiceClient(string endpointConfigurationName) :
11                 base(endpointConfigurationName)
12         {
13         }
14 
15         public SettingServiceClient(string endpointConfigurationName, string remoteAddress) :
16                 base(endpointConfigurationName, remoteAddress)
17         {
18         }
19 
20         public SettingServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
21                 base(endpointConfigurationName, remoteAddress)
22         {
23         }
24 
25         public SettingServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
26                 base(binding, remoteAddress)
27         {
28         }
29 
30         #region 艾克仕網絡ACS.CloudOA企業名片
31         
32         /// <summary>
33         /// 企業名片
34         /// </summary>
35         public byte[] GetFirmCard(byte[] bytData)
36         {
37             return base.Channel.GetFirmCard(bytData);
38         }
39 
40         /// <summary>
41         /// 保存企業信息
42         /// </summary>
43         public byte[] AddFirmCard(byte[] bytData)
44         {
45             return base.Channel.AddFirmCard(bytData);
46         }
47 
48         /// <summary>
49         /// 保存企業認證信息
50         /// </summary>
51         public byte[] AddFirmCardCertified(byte[] bytData)
52         {
53             return base.Channel.AddFirmCardCertified(bytData);
54         }
55         #endregion
56     }
SettingService View Code

 WCF客戶端配置和代理類就這些了,至於如何使用我在后面的實例部分會講到。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM