在IService1.cs定义服务契约
namespace WcfDemo
{
}
在Service1.svc中实现契约服务
1 |
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; } } } } |
1 |
<wbr></wbr> |
1 |
<br />添加宿主程序用于监测服务<br />添加WinForm项目加入解决方案<br />界面如下图:<br /><img src="//www.qiandabao.com/upload/201008310021340361.jpg" real_src="//www.qiandabao.com/upload/201008310021340361.jpg" width="490" height="367" alt="WCF实例" title="WCF实例" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; " /><br /><br />界面上两个按钮:<br />启动服务按钮: 用于启动wcf服务<br />停止服务按钮: 用于停止wcf服务<br />Label: <wbr></wbr> <wbr></wbr> 用于显示服务相关信息<br />后台代码为:<br />应用命名空间 <wbr></wbr> using System.ServiceModel;<br />添加引用 wcf服务生成的dll文件<br /> |
1 |
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 = "服务已关闭"; } } |
1 |
<br /><br />接下来配置app.config<br /> |
1 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services><!--添加服务--> <service name="WcfDemo.Service1" behaviorConfiguration="CalculatorServiceBehavio<wbr></wbr>r"> <!--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> <!--定义CalculatorServiceBehavio<wbr></wbr>r的行为--> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehavio<wbr></wbr>r"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailIn<wbr></wbr>Faults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> |
1 |
<br /><br />在服务启动后可通过appConfig中baseAddress节点中的baseAddress地址查看Wcf服务<br /> |
1 |
<wbr></wbr> |
为项目添加服务引用地址输入服务主机appconfig中baseAddress地址点击前往(添加服务引用时一点是在服务启动状态下的)
1 |
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) { this.label1.Text = "购买成功"; } this.label1.Text += "剩余车票还有" + TClient.GetRemainingNum().ToString(); } private void button2_Click(object sender, EventArgs e) { this.label1.Text = ""; this.label1.Text = TClient.GetRemainingNum().ToString();//调用WCF中的方法 } } |