{
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;
}
}
}
}
添加宿主程序用于监测服务
添加WinForm项目加入解决方案
界面如下图:
界面上两个按钮:
启动服务按钮: 用于启动wcf服务
停止服务按钮: 用于停止wcf服务
Label: 用于显示服务相关信息
后台代码为:
应用命名空间 using System.ServiceModel;
添加引用 wcf服务生成的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 = "服务已关闭";
}
}
接下来配置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://www.easck.com/> <endpoint address="" binding="wsHttpBinding" contract="WcfDemo.IService1"></endpoint>










