WCF实例
以订票为例简单应用wcf
1、新建一个wcf服务应用程序,命名为WcfDemo
1、新建一个wcf服务应用程序,命名为WcfDemo

在IService1.cs定义服务契约
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
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 { return boolCount; }
set {
if (HowMany > 0)
{
boolCount = true;
}
else
{
boolCount = false;
}
}
}
[DataMember]
/*返回票数*/
public int HowMany
{
get { return howmany; }
set { howmany = value;}
}
}
}
在Service1.svc.cs中实现契约服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
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)
{
T.BoolCalue = false;
if (T.BoolCalue)
{
T.HowMany = T.HowMany - Num;
return 1;
}
else
{
return 0;
}
}
}
}
2、添加宿主程序用于监测服务
添加WinForm项目加入解决方案(WcfHost)
界面如下图:

界面上两个按钮:
启动服务按钮: 用于启动wcf服务
停止服务按钮: 用于停止wcf服务
Label: 用于显示服务相关信息
后台代码为:
引用命名空间 using System.ServiceModel;
添加引用 wcf服务生成的dll文件(引用WcfDemo项目)
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;
using System.ServiceModel;
namespace WcfHost
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ServiceHost host = null;//定义 ServiceHost
private void btn_Open_Click(object sender, EventArgs e)
{
host = new ServiceHost(typeof(WcfDemo.Service1));//WcfDemo.Service1 为引用的dll中的服务
host.Open();//启动服务
this.lbl_Show.Text = "服务已启动";
}
private void btn_Close_Click(object sender, EventArgs e)
{
if (host.State != CommunicationState.Closed)//判断服务是否关闭
{
host.Close();//关闭服务
}
this.lbl_Show.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>
程序运行结果:

在服务启动后可通过App.config中baseAddress节点中的baseAddress地址查看Wcf服务(Ctrl+鼠标左键单击)
到这里,服务以及服务主机都已经创建好了。下面该创建测试客户机了!
3、重新建立一个解决方案,新建个WinForm程序做为我们的测试客户机
界面中有两个按钮一个label,如下图:

到这里,服务以及服务主机都已经创建好了。下面该创建测试客户机了!
3、重新建立一个解决方案,新建个WinForm程序做为我们的测试客户机
界面中有两个按钮一个label,如下图:

购买车票:调用wcf服务的BuyTickets()方法
查询车票:调用wcf服务的GetRemainingNum()方法
label用于显示运行信息(剩余票数什么的)
查询车票:调用wcf服务的GetRemainingNum()方法
label用于显示运行信息(剩余票数什么的)
为项目添加(引用,右键,添加服务引用),地址输入服务主机App.config中baseAddress地址点击前往(
添加服务引用时一点是在服务启动状态下的)

后台代码为:
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;
using System.ServiceModel;
namespace WcfClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ServiceReference1.Service1Client TClient = new WcfClient.ServiceReference1.Service1Client();
//声明客户端调用
private void btn_Buy_Click(object sender, EventArgs e)
{
int i = TClient.BuyTickets(2); //调用WCF中的方法
if (i == 1)
{
this.lbl_Show.Text = "购买成功,剩余车票还有" + TClient.GetRemainingNum().ToString() + "张";
}
else
{
this.lbl_Show.Text = "没有剩余车票";
}
}
private void btn_Query_Click(object sender, EventArgs e)
{
this.lbl_Show.Text = "";
this.lbl_Show.Text = "剩余车票还有" + TClient.GetRemainingNum().ToString() + "张";//调用WCF中的方法
}
}
}
点击购买车票时调用wcf的BuyTicket()方法并返回剩余车票的信息
点击查看车票时调用wcf的GetRemainingNum()得到剩余车票信息
运行结果如下:
点击购买车票按钮:
点击查看车票时调用wcf的GetRemainingNum()得到剩余车票信息
运行结果如下:
点击购买车票按钮:

到此结束!
