wcf服务端
1,新建一个“windows窗体程序”名称为WCFServer1。
2,然后添加一个“WCF服务”名称为Service1。具体步骤为:解决方案试图中,选中“WCFServer1”项目,右键,在弹出菜单中选择“添加->新建项”。
3,双击主窗体,在它的Load事件中编写代码启动wcf服务:BasicHttpBinding方式启动wcf服务。此文件代码如下:
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; using System.ServiceModel.Description; namespace WCFServer1 { public partial class Form1 : Form { ServiceHost m_ServiceHost; publicForm1() { InitializeComponent(); } privatevoid Form1_Load(objectsender, EventArgs e) { m_ServiceHost = new ServiceHost(typeof(Service1));//Service1是wcf服务的类名称 //BasicHttpBinding方式启动wcf服务 ServiceMetadataBehaviormetadataBehavior; metadataBehavior =m_ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>(); if(metadataBehavior == null) { metadataBehavior = new ServiceMetadataBehavior(); metadataBehavior.HttpGetEnabled= true; metadataBehavior.HttpGetUrl = new Uri(string.Format("http://localhost:10085/WCFHostServer/Service1")); m_ServiceHost.Description.Behaviors.Add(metadataBehavior); } else { metadataBehavior.HttpGetEnabled= true; metadataBehavior.HttpGetUrl = new Uri(string.Format("http://localhost:10085/WCFHostServer/Service1")); } m_ServiceHost.Open(); } privatevoid FormService_FormClosing(object sender, FormClosingEventArgse) { if(m_ServiceHost != null) { m_ServiceHost.Close(); m_ServiceHost = null; } } } }
4,在 IService1.cs中增加一个方法GetSvrTime
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFServer1 { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。 [ServiceContract] public interface IService1 { [OperationContract] voidDoWork(); [OperationContract] stringGetSvrTime(); } }
5, 在Service1.cs中实现接口的方法GetSvrTime,获取服务器时间
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFServer1 { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。 public class Service1 : IService1 { public void DoWork() { } public string GetSvrTime() { returnDateTime.Now.ToString(); } } }
第二步:WCF客户端
1,新建一个“windows窗体程序”命名为WCFClient1。
2,在Debug中运行WCFHostServer窗体(这一步很重要),然后在项目WCFHostClient中右键--》“添加服务引用”,在地址填入以BasicHttpBinding绑定方式启动wcf服务的地址http://localhost:10085/WCFHostServer/Service1,点击“前往”,然后连接上后点击“确定”,在WCFHostClient项目中就会增加一个Service References(ServiceReference1)
3,主窗体上增加一个按钮,双击此按钮添加响应事件。
using WCFClient1.ServiceReference1; using System.ServiceModel; //... private voidbutton1_Click(object sender, EventArgs e) { //basicHttpBinding绑定方式的通讯单工 Service1Clienthost = new ServiceReference1.Service1Client(); stringhtime = host.GetSvrTime();//调用GetSvrTime获取到wcf服务器上的时间 MessageBox.Show("basicHttpBinding" + "@" + htime); //NetTcpBinding绑定方式的通讯双工 IService1m_Innerclient; ChannelFactory<IService1> m_ChannelFactory; NetTcpBindingbinding = new NetTcpBinding(); UribaseAddress = new Uri("net.tcp://localhost:10085/WCFHostServer/Service1"); m_ChannelFactory = new ChannelFactory<IService1>(binding, newEndpointAddress(baseAddress)); m_Innerclient =m_ChannelFactory.CreateChannel(); stringhtime1 = m_Innerclient.GetSvrTime();//调用GetSvrTime获取到wcf服务器上的时间 MessageBox.Show("NetTcpBinding" + "@" + htime1); }