WCF實(shí)現(xiàn)的計(jì)算器功能實(shí)例
本文實(shí)例講述了WCF實(shí)現(xiàn)的計(jì)算器功能。分享給大家供大家參考,具體如下:
對(duì)于WCF,我們有了前面的理論基礎(chǔ),今天通過一個(gè)計(jì)算器的實(shí)例主要給大家講解怎么一步一步地創(chuàng)建一個(gè)完整的WCF應(yīng)用。
一、創(chuàng)建整個(gè)解決方案
Calculator.Service:一個(gè)類庫項(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ù)契約
一般,我們通過接口的形式定義服務(wù)契約。通過下面的代碼,將一個(gè)接口ICalculator定義成服務(wù)契約。我們通過在接口上應(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)建成功后,我們需要通過實(shí)現(xiàn)服務(wù)契約來創(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;
}
}
}
四、通過自我寄宿的方式寄宿服務(wù)
服務(wù)寄宿的目的就是開啟一個(gè)進(jìn)程,為WCF服務(wù)提供一個(gè)運(yùn)行的環(huán)境。通過為服務(wù)添加一個(gè)或多個(gè)中級(jí)誒單,使之暴露給潛在的服務(wù)消費(fèi)者。服務(wù)消費(fèi)者最終通過相匹配的終結(jié)點(diǎn)對(duì)該服務(wù)進(jìn)行調(diào)用。我們完全可以通過代碼的方式完成所有的服務(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ù)端便開始了服務(wù)調(diào)用請(qǐng)求的監(jiān)聽工作。此外,服務(wù)寄宿將服務(wù)描述通過元數(shù)據(jù)的形式發(fā)布出來,相應(yīng)的客戶端就可以獲取這些元數(shù)據(jù),創(chuàng)建愛你客戶端程序進(jìn)行服務(wù)的消費(fèi)。在VS下,當(dāng)我們添加服務(wù)引用的時(shí)候,VS在內(nèi)部幫我們實(shí)現(xiàn)元數(shù)據(jù)的獲取,并借組這些元數(shù)據(jù)通過代碼生成工具自動(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ù)通過運(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#常見控件用法教程》、《C#窗體操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#操作SQLite實(shí)現(xiàn)數(shù)據(jù)的增刪改查
SQLite是一個(gè)輕量級(jí)、跨平臺(tái)的關(guān)系型數(shù)據(jù)庫,在小型項(xiàng)目中,方便,易用,同時(shí)支持多種開發(fā)語言。本文將用C#語言對(duì)SQLite 的一個(gè)封裝,實(shí)現(xiàn)數(shù)據(jù)的增刪改查。需要的可以參考一下2022-01-01
WCF基礎(chǔ)介紹并創(chuàng)建簡(jiǎn)單應(yīng)用程序
這篇文章介紹了WCF基礎(chǔ)并創(chuàng)建簡(jiǎn)單WCF應(yīng)用程序,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
C#獲取ListView鼠標(biāo)下的Item實(shí)例
下面小編就為大家?guī)硪黄狢#獲取ListView鼠標(biāo)下的Item實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
C#從windows剪貼板獲取并顯示文本內(nèi)容的方法
這篇文章主要介紹了C#從windows剪貼板獲取并顯示文本內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04

