以下都采用VS2013,且创建的项目的名称和生成代码都不做修改
一、新建一个WCF服务库项目WcfServiceLibrary1
注:
1、WCF服务应用程序项目和WCF服务库项目的区别
->WCF服务应用程序在VS2013中可以直接运行,类似WebService,可以部署到IIS;
二、创建一个WPF应用程序项目WpfApplication1,用作WCF服务的宿主
1、添加对WcfServiceLibrary1项目的引用;
2、添加对程序集System.ServiceModel的引用
->添加引用-》程序集-》System.ServiceModel;
3、在MainWindow.xaml.cs中添加如下引用:
using System.ServiceModel;
using WcfServiceLibrary1;
4、添加一个按钮(btnTest)和一个文本框(txtTest);
5、双击按钮添加按钮事件,并添加如下代码:
6、在App.config中添加如下配置:
注:用工具-》WCF服务配置编辑器,没有找到生成baseAddresses、serviceMetadata节点的方法。
三、重新生成解决方案,双击启动WpfApplication1.exe,点击按钮,结果如下图:
四、创建一个WPF应用程序项目WpfApplication2,当作客户端调用WCF服务
1、添加WcfServiceLibrary1项目的引用;
2、添加服务引用,输入http://localhost:6868/wcf1?wsdl并转到,点确定,如下图:
3、在MainWindow.xaml.cs中添加如下引用:
4、添加一个按钮(btnTest)和一个文本框(txtTest),并添加如下事件:
五、双击启动WpfApplication2.exe,点击按钮,结果如下: