1 新建空解决方案WCFExample
在解决方案WCFExample下新建以下项目:
项目结构如下图所示。
2 实现Business项目
项目下新建BubbleSortBusiness类(类文件BubbleSortBusiness.cs),类中添加BubbleSort方法实现冒泡排序功能,客户端程序通过WCF服务调用该方法来完成冒泡排序操作。
详细代码如下所示。
using System; namespace Business { public class BubbleSortBusiness { public int[] BubbleSort(int[] listNumber) { try { if (listNumber != null && listNumber.Length != 0) { bool flag; int temp; for (int i = 0; i < listNumber.Length - 1; i++) { flag = false; for (int j = 0; j < listNumber.Length - i - 1; j++) { if (listNumber[j] > listNumber[j + 1]) { temp = listNumber[j]; listNumber[j] = listNumber[j + 1]; listNumber[j + 1] = temp; flag = true; } } if (!flag) { break; } } return listNumber; } else { return null; } } catch(Exception ex) { //将异常写入log(代码省略) return null; } } } }
3 实现Service项目
首先,需要添加对Business项目的引用,因为Service项目需要调用Business下的BubbleSort方法来完成冒泡排序操作。
我们这里直接添加项目Business编译后的dll即可,另外还需要添加对System.ServiceModel的引用。
添加引用后引用文件夹包含下图所示内容。
接着,在项目下新建IBubbleSortService接口(类文件IBubbleSortService.cs),详细代码如下所示。
using System.ServiceModel; namespace Service { [ServiceContract] public interface IBubbleSortService { [OperationContract] int[] BubbleSort(int[] listNumber); } }
紧接着,添加IBubbleSortService接口的实现类BubbleSortService (类文件BubbleSortService.cs),详细代码如下所示。
using Business; namespace Service { public class BubbleSortService : IBubbleSortService { public int[] BubbleSort(int[] listNumber) { return new BubbleSortBusiness().BubbleSort(listNumber); } } }
4 实现Host项目
WCF不能独立运行,需要一个载体来承载才能运行,此载体被称作宿主。
宿主程序可以是Windows应用程序,控制台应用程序,ASP.NET应用程序等。
本文新建一个控制台应用程序Host作为WCF服务的宿主程序。
一开始,需要添加对Service项目的引用,添加方法同样是添加Service项目编译后的dll即可。另外,同样需要添加对System.ServiceModel的引用。
添加引用后引用文件夹包含下图所示内容。
接着,修改项目下类文件Program,cs的代码如下所示。
using System; using System.ServiceModel; using Service; namespace Host { class Program { static void Main(string[] args) { Type ServiceType = typeof(BubbleSortService); using (ServiceHost host = new ServiceHost(ServiceType)) { host.Open(); Console.WriteLine("服务已经启动!"); Console.ReadKey(true); host.Close(); } } } }
随后,添加配置文件App.config,配置文件的详细内容如下所示。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="Service.BubbleSortService" behaviorConfiguration="BubbleSortService"> <host> <baseAddresses> <add baseAddress="http://localhost:8888/Service/"/> </baseAddresses> </host> <endpoint address="BubbleSortService" binding="basicHttpBinding" contract="Service.IBubbleSortService"> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="BubbleSortService"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
编译项目Host后,运行Host.exe,效果如下图所示。
在浏览器地址栏输入服务访问地址http://localhost:8888/Service/,测试服务是否能够访问。如果显示下图结果则说明服务可以被访问。
5 实现Client项目
首先,需要添加对服务的引用。
添加方法:在“引用”文件夹上右键点击菜单项“添加服务引用”打开“添加服务引用”窗口。
在“添加服务引用”窗口的地址栏输入服务地址后,点击“前往”按钮,选中需要添加的服务,点击“确定”按钮即可完成服务引用的添加。
(注意:添加服务引用时,Host.exe程序需要运行起来,这样才可以找到需要被添加的服务。)
添加服务引用成功后,在Client项目下会出现Service References目录,同时生成了配置文件app.config(如下图所示)。
配置文件app.config的详细内容如下所示。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IBubbleSortService" /> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:8888/Service/BubbleSortService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IBubbleSortService" contract="ServiceReference1.IBubbleSortService" name="BasicHttpBinding_IBubbleSortService" /> </client> </system.serviceModel> </configuration>
注意:这个app.config文件是自动生成的,不需要手动添加,但是也不能被删除。
随后,制作窗体程序的界面,如下图所示。
同时,编写窗体的后端代码,如下所示。
using System; using System.Windows.Forms; namespace Client { public partial class frmSorter : Form { ServiceReference1.BubbleSortServiceClient bubbleSortClient = new ServiceReference1.BubbleSortServiceClient(); public frmSorter() { InitializeComponent(); } private void btnSort_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtSrcData.Text.Trim())) { //省略数据校验代码 //必须是由逗号分隔的整型数据 string srcData = txtSrcData.Text.Trim(); string[] arrSrcData = srcData.Split(‘,‘); int len = arrSrcData.Length; int[] intSrcData = new int[len]; for (int i = 0; i < len; i++) { intSrcData[i] = Convert.ToInt32(arrSrcData[i]); } intSrcData = bubbleSortClient.BubbleSort(intSrcData); string strResult=string.Empty; for (int i = 0; i < len; i++) { strResult += Convert.ToString(intSrcData[i])+" "; } txtSortedData.Text = strResult; } else { MessageBox.Show("请输入待排序的整型数据!"); } } } }
6 运行程序
运行Client.exe与Host.exe程序,得到以下结果。
至此,一个简单的WCF程序完成了。
原文:http://blog.csdn.net/yl2isoft/article/details/20800299