|
WCF實(shí)例(帶步驟) 復(fù)制代碼 代碼如下:
<xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" /> 本篇轉(zhuǎn)自百度文檔,自己試過(guò),確實(shí)可以用。 以訂票為例簡(jiǎn)單應(yīng)用wcf 新建一個(gè)wcf服務(wù)應(yīng)用程序 在IService1.cs定義服務(wù)契約 復(fù)制代碼 代碼如下:
namespace WcfDemo { // 注意: 如果更改此處的接口名稱 "IService1",也必須更新 Web.config 中對(duì) "IService1" 的引用。 [ServiceContract] // 服務(wù)合同 即提供服務(wù)的接口或類 public interface IService1 { [OperationContract] /* 增加車票的方法*/ void AddTicket(int count); [OperationContract] /*購(gòu)買車票的方法*/ int BuyTickets(int Num); [OperationContract] //服務(wù)契約 即提供服務(wù)的實(shí)現(xiàn)方法 /*查詢車票的方法*/ int GetRemainingNum(); // 任務(wù): 在此處添加服務(wù)操作 } // 使用下面示例中說(shuō)明的數(shù)據(jù)約定將復(fù)合類型添加到服務(wù)操作。 [DataContract] //數(shù)據(jù)契約 public class Ticket { bool boolCount = true;//判斷是否還有車票 int howmany = 10;//還有多少車票 [DataMember] /*判斷是否還有票*/ public bool BoolCalue { get { return boolCount; } set { if (HowMany > 0) { boolCount = false; } else { boolCount = true; } } } [DataMember] /*返回票數(shù)*/ public int HowMany { get { return howmany; } set { howmany = value;} } } } 在Service1.svc中實(shí)現(xiàn)契約服務(wù) 復(fù)制代碼 代碼如下:
namespace WcfDemo { // 注意: 如果更改此處的類名“Service1”,也必須更新 Web.config 和關(guān)聯(lián)的 .svc 文件中對(duì)“Service1”的引用。 public class Service1 : IService1 { Ticket T=new Ticket(); /*實(shí)現(xiàn)添加票數(shù)的方法*/ public void AddTicket(int count) { T.HowMany=T.HowMany+count; } /*實(shí)現(xiàn)返回票數(shù)的方法*/ public int GetRemainingNum() { return T.HowMany; } /*實(shí)現(xiàn)購(gòu)買車票的方法*/ public int BuyTickets(int Num) { if (T.BoolCalue) { T.HowMany = T.HowMany - Num; return 1; } else { return 0; } } } } 添加宿主程序用于監(jiān)測(cè)服務(wù) 添加WinForm項(xiàng)目加入解決方案 界面如下圖: ![]() 界面上兩個(gè)按鈕: 啟動(dòng)服務(wù)按鈕: 用于啟動(dòng)wcf服務(wù) 停止服務(wù)按鈕: 用于停止wcf服務(wù) Label: 用于顯示服務(wù)相關(guān)信息 后臺(tái)代碼為: 應(yīng)用命名空間 using System.ServiceModel; 添加引用 wcf服務(wù)生成的dll文件 復(fù)制代碼 代碼如下:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } ServiceHost host = null;//定義 ServiceHost private void button1_Click(object sender, EventArgs e) { host = new ServiceHost(typeof(WcfDemo.Service1));//WcfDemo.Service1 為引用的dll中的服務(wù) host.Open();//啟動(dòng)服務(wù) this.label1.Text = "服務(wù)已啟動(dòng)"; } private void button2_Click(object sender, EventArgs e) { if (host.State != CommunicationState.Closed)//判斷服務(wù)是否關(guān)閉 { host.Close();//關(guān)閉服務(wù) } this.label1.Text = "服務(wù)已關(guān)閉"; } } 接下來(lái)配置app.config 復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services><!--添加服務(wù)--> <service name="WcfDemo.Service1" behaviorConfiguration="CalculatorServiceBehavior"> <!--name 必須與代碼中的host實(shí)例初始化的服務(wù)一樣 behaviorConfiguration 行為配置 --> <host> <baseAddresses> <!--添加調(diào)用服務(wù)地址--> <add baseAddress="http://localhost:8000/"/> </baseAddresses> </host> <!--添加契約接口 contract="WcfDemo.IService1" WcfDemo.IService1為契約接口 binding="wsHttpBinding" wsHttpBinding為通過(guò)Http調(diào)用--> <endpoint address="" binding="wsHttpBinding" contract="WcfDemo.IService1"></endpoint> </service> </services> <!--定義CalculatorServiceBehavior的行為--> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> 程序運(yùn)行結(jié)果: ![]()
![]()
![]() 復(fù)制代碼 代碼如下:
public partial class Form2 : Form { public Form2() { InitializeComponent(); } ServiceReference1.Service1Client TClient = new WinFormsClient.ServiceReference1.Service1Client(); //聲明客戶端調(diào)用 private void button1_Click(object sender, EventArgs e) { int i = TClient.BuyTickets(2); //調(diào)用WCF中的方法 if (i == 1) { this.label1.Text = "購(gòu)買成功"; } this.label1.Text += "剩余車票還有" + TClient.GetRemainingNum().ToString(); } private void button2_Click(object sender, EventArgs e) { this.label1.Text = ""; this.label1.Text = TClient.GetRemainingNum().ToString();//調(diào)用WCF中的方法 } } 點(diǎn)擊購(gòu)買車票時(shí)調(diào)用wcf的BuyTicket()方法并返回剩余車票的信息 點(diǎn)擊查看車票時(shí)調(diào)用wcf的GetRemainingNum()得到剩余車票信息 運(yùn)行結(jié)果如下: 點(diǎn)擊購(gòu)買車票:
您可能感興趣的文章:
|
|
|
來(lái)自: ThinkTank_引擎 > 《WCF》