WCF實(shí)現(xiàn)的計(jì)算器功能實(shí)例
本文實(shí)例講述了WCF實(shí)現(xiàn)的計(jì)算器功能。分享給大家供大家參考,具體如下:
對(duì)于WCF,我們有了前面的理論基礎(chǔ),今天通過(guò)一個(gè)計(jì)算器的實(shí)例主要給大家講解怎么一步一步地創(chuàng)建一個(gè)完整的WCF應(yīng)用。
一、創(chuàng)建整個(gè)解決方案
Calculator.Service:一個(gè)類庫(kù)項(xiàng)目,定義服務(wù)契約(Service Contract),應(yīng)用System.ServiceModel程序集;提供對(duì)WCF服務(wù)的實(shí)現(xiàn)。
Calculator.Host:一個(gè)Windows窗體應(yīng)用程序,實(shí)現(xiàn)對(duì)定義在Calculator.Service項(xiàng)目中的服務(wù)的寄宿,該項(xiàng)目需要引用Calculator.Service項(xiàng)目和System.ServiceModel程序集。
Calculator.Client:一個(gè)Windows窗體應(yīng)用程序模擬服務(wù)的客戶端,該項(xiàng)目應(yīng)用System.ServiceModel程序集。
二、創(chuàng)建服務(wù)契約
一般,我們通過(guò)接口的形式定義服務(wù)契約。通過(guò)下面的代碼,將一個(gè)接口ICalculator定義成服務(wù)契約。我們通過(guò)在接口上應(yīng)用System.ServiceModel.ServiceContractAttribute特性將一個(gè)接口定義成服務(wù)契約。
將接口定義成服務(wù)契約后,接口的方法成員并不能自動(dòng)成為服務(wù)的操作。我們需要在相應(yīng)的操作方法上面顯式地應(yīng)用OperationContractAttribute特性。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Calculator.Service { [ServiceContract] public interface ICalculator { [OperationContract] double Add(double x, double y); [OperationContract] double Subtract(double x, double y); [OperationContract] double Multiply(double x, double y); [OperationContract] double Divide(double x, double y); } }
三、創(chuàng)建服務(wù)
當(dāng)服務(wù)契約創(chuàng)建成功后,我們需要通過(guò)實(shí)現(xiàn)服務(wù)契約來(lái)創(chuàng)建具體的WCF服務(wù),WCF服務(wù)CalculatorService實(shí)現(xiàn)了服務(wù)契約的接口ICalculator,實(shí)現(xiàn)了所有的服務(wù)操作。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Calculator.Service { public class CalculatorService:ICalculator { public double Add(double x, double y) { return x + y; } public double Subtract(double x, double y) { return x - y; } public double Multiply(double x, double y) { return x * y; } public double Divide(double x, double y) { return x / y; } } }
四、通過(guò)自我寄宿的方式寄宿服務(wù)
服務(wù)寄宿的目的就是開(kāi)啟一個(gè)進(jìn)程,為WCF服務(wù)提供一個(gè)運(yùn)行的環(huán)境。通過(guò)為服務(wù)添加一個(gè)或多個(gè)中級(jí)誒單,使之暴露給潛在的服務(wù)消費(fèi)者。服務(wù)消費(fèi)者最終通過(guò)相匹配的終結(jié)點(diǎn)對(duì)該服務(wù)進(jìn)行調(diào)用。我們完全可以通過(guò)代碼的方式完成所有的服務(wù)寄宿工作。
using Calculator.Service; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Windows.Forms; namespace Calculator.Host { public partial class Form1 : Form { public Form1() { InitializeComponent(); } ServiceHost host = null; private void btnOpen_Click(object sender, EventArgs e) { host = new ServiceHost(typeof(CalculatorService)); host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://localhost:8008/Calculator"); if (host.Description.Behaviors.Find<ServiceMetadataBehavior>()==null) { ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; behavior.HttpGetUrl = new Uri("http://localhost:8008/Calculator/metadata"); host.Description.Behaviors.Add(behavior); } host.Opened += delegate { label1.Text = "服務(wù)已經(jīng)啟動(dòng)!"; }; host.Open(); } private void btnClose_Click(object sender, EventArgs e) { if (host.State != CommunicationState.Closed) { host.Closed += delegate { label1.Text = "服務(wù)已經(jīng)停止!"; }; host.Close(); } } } }
五、創(chuàng)建客戶端調(diào)用服務(wù)
服務(wù)被成功寄宿后,服務(wù)端便開(kāi)始了服務(wù)調(diào)用請(qǐng)求的監(jiān)聽(tīng)工作。此外,服務(wù)寄宿將服務(wù)描述通過(guò)元數(shù)據(jù)的形式發(fā)布出來(lái),相應(yīng)的客戶端就可以獲取這些元數(shù)據(jù),創(chuàng)建愛(ài)你客戶端程序進(jìn)行服務(wù)的消費(fèi)。在VS下,當(dāng)我們添加服務(wù)引用的時(shí)候,VS在內(nèi)部幫我們實(shí)現(xiàn)元數(shù)據(jù)的獲取,并借組這些元數(shù)據(jù)通過(guò)代碼生成工具自動(dòng)生成用于服務(wù)調(diào)用的服務(wù)代理相關(guān)代碼和相應(yīng)的配置。
我們可以創(chuàng)建CalculatorClient對(duì)象,執(zhí)行相應(yīng)方法調(diào)用服務(wù)操作。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Calculator.Client { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { comboBox1.Text = "+"; } private void button1_Click(object sender, EventArgs e) { CalculatorService.CalculatorClient client = new CalculatorService.CalculatorClient(); double x = Convert.ToDouble (textBox1.Text); double y = Convert.ToDouble(textBox2.Text); double result=0; string operater = comboBox1.Text; switch (operater ) { case "+": result = client.Add(x, y); break; case "-": result = client.Subtract(x, y); break; case "*": result = client.Multiply(x, y); break; case "/": if (y==0) { MessageBox.Show("除數(shù)不能為0!"); return; } result = client.Divide(x, y); break; } label1.Text = textBox1.Text + comboBox1.Text + textBox2.Text + " = " + Convert.ToString(result); } } }
在這個(gè)計(jì)算器實(shí)例中,我們實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的計(jì)算服務(wù)(CalculatorService),提供基本的加、減、乘、除的運(yùn)算??蛻舳撕头?wù)通過(guò)運(yùn)行在不同進(jìn)程模擬,體現(xiàn)了客戶端和服務(wù)端進(jìn)程互相調(diào)用的關(guān)系。
PS:這里再為大家推薦幾款在線計(jì)算工具供大家參考使用:
在線一元函數(shù)(方程)求解計(jì)算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科學(xué)計(jì)算器在線使用_高級(jí)計(jì)算器在線計(jì)算:
http://tools.jb51.net/jisuanqi/jsqkexue
在線計(jì)算器_標(biāo)準(zhǔn)計(jì)算器:
http://tools.jb51.net/jisuanqi/jsq
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見(jiàn)控件用法教程》、《C#窗體操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
上一篇:C# networkcomms 3.0實(shí)現(xiàn)模擬登陸總結(jié)
欄 目:C#教程
下一篇:C#中各種計(jì)時(shí)器用法小結(jié)
本文標(biāo)題:WCF實(shí)現(xiàn)的計(jì)算器功能實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5672.html
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開(kāi)的方法
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)