我們接著上一篇文章進行講解 http://www.cnblogs.com/songjianhui/p/7060698.html 一:客戶端通過添加引用調用服務 WCF應用服務被成功寄宿後,WCF服務應用便開始了服務調用請求的監聽工作。此外,服務寄宿將服務描述通過元數據的形式發佈出來,相應的客戶端就可 ...
我們接著上一篇文章進行講解 http://www.cnblogs.com/songjianhui/p/7060698.html
一:客戶端通過添加引用調用服務
WCF應用服務被成功寄宿後,WCF服務應用便開始了服務調用請求的監聽工作。此外,服務寄宿將服務描述通過元數據的形式發佈出來,相應的客戶端就可以獲取這些元數據。接下來我們來創建客戶端程式進行服務的調用。
1)先運行服務寄宿程式(Hosting.exe)
2) 在Visual Studio 2013的“解決方案資源管理器”中,把Client項目展開,左鍵選中“引用”,點擊滑鼠右鍵,彈出菜單,在彈出的上下文菜單中選擇“添加服務引用(Add Service References)”。如下圖。
3) 此時會彈出一個對話框,如下圖所示。在對話框中的“地址”輸入框中輸入服務元數據發佈的源地址:http://127.0.0.1:3721/calculatorService/metadata,併在“命名空間”輸入框中輸入一個命名空間,然後點擊“確定”按鈕(如下圖)。Visual studio 2013會自動生成一系列用於服務調用的代碼和配置。
4) 在Visual Studio 2013自動生成的類中,包含一個服務協定介面、一個服務代理對象和其他相關的類。
5) 我們可以實例化CalculatorServiceClient對象,執行相應方法調用WCF服務操作。客戶端進行WCF服務調用的代碼如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Client 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 MyWcfService.CalculatorServiceClient client = new MyWcfService.CalculatorServiceClient(); 14 Console.WriteLine("x+y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2)); 15 Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2)); 16 Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2)); 17 Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2)); 18 Console.ReadLine(); 19 } 20 } 21 }
6)結果
二:客戶端通過ChannelFactory<T>方式調用WCF服務
1) WCF採用基於契約的服務調用方法。從上面的例子也可以看到,Visual Studio2013 在進行服務引用添加的過程中會在客戶端創建一個與服務端等效的服務契 約介面。由於服務端和客戶端在同一個解決方案中。因此完全可以讓服務端和客戶端引用相同的契約
2) 為了演示這種場景,我們將添加的服務引用移除,併為Client項目添加Service.Interface項目的引用。在客戶端程式中基於地址和綁定對象創建一個 ChannelFactory<ICalculator>,然後調用它的CreateChannel方法 創建的服務代理對象完成服務調用(這裡我們就直接創建一個控制台來進行演示)
3)創建一個控制台應用程式 引用Service.Interface和System.ServiceModel;
4) 編寫ChanelClient的代碼
1.通過代碼的方式配置終結點
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Service.Interface; 7 using System.ServiceModel; 8 9 namespace ChannelClient 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //基於地址和綁定對象創建一個ChannelFactory<ICalculator> 通過代碼 16 using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3721/calculatorService")) 17 { 18 //創建服務代理對象 19 ICalculator proxy = channelFactory.CreateChannel(); 20 //調用計算器方法 21 Console.WriteLine("x+y={2} when x={0} and y={1}",1,2,proxy.Add(1,2)); 22 Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 23 Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 24 Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 25 26 Console.ReadLine(); 27 } 28 } 29 } 30 }
2.通過配置文件的方式來配置終結點(在app.config中進行配置)
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <client> <endpoint name="calculatorService" address="http://127.0.0.1:3721/CalculatorService" binding="wsHttpBinding" contract="Service.Interface.ICalculator"/> </client> </system.serviceModel> </configuration>
ChannelClient中的代碼就要進行更改
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Service.Interface; 7 using System.ServiceModel; 8 9 namespace ChannelClient 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //基於地址和綁定對象創建一個ChannelFactory<ICalculator> 通過代碼 16 //using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3721/CalculatorService")) 17 //{ 18 // //創建服務代理對象 19 // ICalculator proxy = channelFactory.CreateChannel(); 20 // //調用計算器方法 21 // Console.WriteLine("x+y={2} when x={0} and y={1}",1,2,proxy.Add(1,2)); 22 // Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 23 // Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 24 // Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 25 26 // Console.ReadLine(); 27 //} 28 29 //基於地址和綁定對象創建一個ChannelFactory<ICalculator> 通過配置文件 30 using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorService")) 31 { 32 //創建服務代理對象 33 ICalculator proxy = channelFactory.CreateChannel(); 34 //調用計算器方法 35 Console.WriteLine("x+y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 36 Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 37 Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 38 Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); 39 40 Console.ReadLine(); 41 } 42 } 43 } 44 }
5) 執行hosting.exe應用程式
6)運行ChanelClient
持續更新中.................................
需要源碼的發送郵件到 [email protected] 每周星期天統一發送