以訂票為例簡單應用wcf程序,需要的朋友可以參考下 本篇轉自百度文檔,自己試過,確實可以用。
以訂票為例簡單應用wcf
新建一個wcf服務應用程序

在IService1.cs定義服務契約
- namespace WcfDemo
- {
- // 注意: 如果更改此處的接口名稱 "IService1",也必須更新 Web.config 中對 "IService1" 的引用。
- [ServiceContract] // 服務合同 即提供服務的接口或類
- public interface IService1
- {
- [OperationContract]
- /* 增加車票的方法*/
- void AddTicket(int count);
- [OperationContract]
- /*購買車票的方法*/
- int BuyTickets(int Num);
- [OperationContract] //服務契約 即提供服務的實現方法
- /*查詢車票的方法*/
- int GetRemainingNum();
- // 任務: 在此處添加服務操作
- }
- // 使用下面示例中說明的數據約定將復合類型添加到服務操作。
- [DataContract] //數據契約
- public class Ticket
- {
- bool boolCount = true;//判斷是否還有車票
- int howmany = 10;//還有多少車票
- [DataMember]
- /*判斷是否還有票*/
- public bool BoolCalue
- {
- get
- {
- if (HowMany > 0)
- {
- boolCount = true;
- }
- else
- {
- boolCount = false;
- }
- return boolCount;
- }
- }
- [DataMember]
- /*返回票數*/
- public int HowMany
- {
- get { return howmany; }
- set { howmany = value;}
- }
- }
- }
namespace WcfDemo
{
// 注意: 如果更改此處的接口名稱 "IService1",也必須更新 Web.config 中對 "IService1" 的引用。
[ServiceContract] // 服務合同 即提供服務的接口或類
public interface IService1
{
[OperationContract]
/* 增加車票的方法*/
void AddTicket(int count);
[OperationContract]
/*購買車票的方法*/
int BuyTickets(int Num);
[OperationContract] //服務契約 即提供服務的實現方法
/*查詢車票的方法*/
int GetRemainingNum();
// 任務: 在此處添加服務操作
}
// 使用下面示例中說明的數據約定將復合類型添加到服務操作。
[DataContract] //數據契約
public class Ticket
{
bool boolCount = true;//判斷是否還有車票
int howmany = 10;//還有多少車票
[DataMember]
/*判斷是否還有票*/
public bool BoolCalue
{
get
{
if (HowMany > 0)
{
boolCount = true;
}
else
{
boolCount = false;
}
return boolCount;
}
}
[DataMember]
/*返回票數*/
public int HowMany
{
get { return howmany; }
set { howmany = value;}
}
}
}
在Service1.svc中實現契約服務
- namespace WcfDemo
- {
- // 注意: 如果更改此處的類名“Service1”,也必須更新 Web.config 和關聯的 .svc 文件中對“Service1”的引用。
- public class Service1 : IService1
- {
- Ticket T=new Ticket();
- /*實現添加票數的方法*/
- public void AddTicket(int count)
- {
- T.HowMany=T.HowMany+count;
- }
- /*實現返回票數的方法*/
- public int GetRemainingNum()
- {
- return T.HowMany;
- }
- /*實現購買車票的方法*/
- public int BuyTickets(int Num)
- {
- if (T.BoolCalue)
- {
- T.HowMany = T.HowMany - Num;
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- }
namespace WcfDemo
{
// 注意: 如果更改此處的類名“Service1”,也必須更新 Web.config 和關聯的 .svc 文件中對“Service1”的引用。
public class Service1 : IService1
{
Ticket T=new Ticket();
/*實現添加票數的方法*/
public void AddTicket(int count)
{
T.HowMany=T.HowMany+count;
}
/*實現返回票數的方法*/
public int GetRemainingNum()
{
return T.HowMany;
}
/*實現購買車票的方法*/
public int BuyTickets(int Num)
{
if (T.BoolCalue)
{
T.HowMany = T.HowMany - Num;
return 1;
}
else
{
return 0;
}
}
}
}
編譯后生成WcfDemo.dll
添加宿主程序用於監測服務
添加WinForm項目加入解決方案 界面如下圖: 
界面上兩個按鈕: 啟動服務按鈕: 用於啟動wcf服務 停止服務按鈕: 用於停止wcf服務 Label: 用於顯示服務相關信息 后台代碼為: 應用命名空間 using System.ServiceModel; 添加引用 wcf服務生成的dll文件 WcfDemo.dll
- 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中的服務
- host.Open();//啟動服務
- this.label1.Text = "服務已啟動";
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if (host.State != CommunicationState.Closed)//判斷服務是否關閉
- {
- host.Close();//關閉服務
- }
- this.label1.Text = "服務已關閉";
- }
- }
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中的服務
host.Open();//啟動服務
this.label1.Text = "服務已啟動";
}
private void button2_Click(object sender, EventArgs e)
{
if (host.State != CommunicationState.Closed)//判斷服務是否關閉
{
host.Close();//關閉服務
}
this.label1.Text = "服務已關閉";
}
}
接下來配置app.config
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <system.serviceModel>
- <services><!--添加服務-->
- <service name="WcfDemo.Service1" behaviorConfiguration="CalculatorServiceBehavior">
- <!--name 必須與代碼中的host實例初始化的服務一樣
- behaviorConfiguration 行為配置 -->
- <host>
- <baseAddresses>
- <!--添加調用服務地址-->
- <add baseAddress="http://localhost:8000/"/>
- </baseAddresses>
- </host>
- <!--添加契約接口 contract="WcfDemo.IService1" WcfDemo.IService1為契約接口 binding="wsHttpBinding" wsHttpBinding為通過Http調用-->
- <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>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services><!--添加服務--> <service name="WcfDemo.Service1" behaviorConfiguration="CalculatorServiceBehavior"> <!--name 必須與代碼中的host實例初始化的服務一樣 behaviorConfiguration 行為配置 --> <host> <baseAddresses> <!--添加調用服務地址--> <add baseAddress="http://localhost:8000/"/> </baseAddresses> </host> <!--添加契約接口 contract="WcfDemo.IService1" WcfDemo.IService1為契約接口 binding="wsHttpBinding" wsHttpBinding為通過Http調用--> <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>
程序運行結果: 
在服務啟動后可通過appConfig中baseAddress節點中的baseAddress地址查看Wcf服務 
到這服務以及服務主機都已經創建好了下面該創建測試客戶機了! 新建個WinForm程序做為我們的測試客戶機 界面兩個按鈕一個label 
購買車票:調用wcf服務的BuyTickets()方法 查詢車票:調用wcf服務的GetRemainingNum()方法 label用於顯示運行信息 為項目添加服務引用 地址輸入服務主機appconfig中baseAddress地址點擊前往(添加服務引用時一點是在服務啟動狀態下的)
后台代碼為:
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- ServiceReference1.Service1Client TClient = new WinFormsClient.ServiceReference1.Service1Client();
- //聲明客戶端調用
- private void button1_Click(object sender, EventArgs e)
- {
- int i = TClient.BuyTickets(2); //調用WCF中的方法
- if (i == 1)
- {
- lblResult.Text = "購買成功.";
- lblResult.Text += "剩余車票還有" + TClient.GetRemainingNum().ToString();
- }
- else
- {
- lblResult.Text = "購買Fail.";
- lblResult.Text += "剩余車票還有" + TClient.GetRemainingNum().ToString();
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- this.label1.Text = "";
- this.label1.Text = TClient.GetRemainingNum().ToString();//調用WCF中的方法
- }
- }
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
ServiceReference1.Service1Client TClient = new WinFormsClient.ServiceReference1.Service1Client();
//聲明客戶端調用
private void button1_Click(object sender, EventArgs e)
{
int i = TClient.BuyTickets(2); //調用WCF中的方法
if (i == 1)
{
lblResult.Text = "購買成功.";
lblResult.Text += "剩余車票還有" + TClient.GetRemainingNum().ToString();
}
else
{
lblResult.Text = "購買Fail.";
lblResult.Text += "剩余車票還有" + TClient.GetRemainingNum().ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.label1.Text = "";
this.label1.Text = TClient.GetRemainingNum().ToString();//調用WCF中的方法
}
}
點擊購買車票時調用wcf的BuyTicket()方法並返回剩余車票的信息 點擊查看車票時調用wcf的GetRemainingNum()得到剩余車票信息 運行結果如下: 點擊購買車票: 
點擊查詢票數時: 
