【WCF 2】理解WCF框架的简单小实例
2016-02-13 11:00
435 查看
导读:上篇博客介绍了WCF框架的整体情况,然后,闲着没事儿,自己做了一个及其简单的WCF框架的例子帮助自己理解。从简单的入手,一步一步深入!本篇博客是介绍怎么用VS2012从头创建一个WCF项目,是一个流程化的介绍,有清楚了解的,建议路过即可!
在解决方案中,建立控制台应用程序,并添加发布好的服务引用
一、建立WCF服务应用程序
1.1,编写IService类接口
<span style="font-family:KaiTi_GB2312;font-size:18px;">namespace TestWcf { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。 [ServiceContract] public interface IService1 { [OperationContract] string GetMessage(string strMessage); // TODO: 在此添加您的服务操作 } }</span>
1.2,编写Service1接口实现
<span style="font-family:KaiTi_GB2312;font-size:18px;">namespace TestWcf { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。 // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。 public class Service1 : IService1 { public string GetMessage(string strMessage) { string strTest = string.Format("My message is:{0}",strMessage); return strTest; } } }</span>
1.3,编写配置文件
<span style="font-family:KaiTi_GB2312;font-size:18px;"> <services> <service name="TestWcf.Service1"> <endpoint address="" binding="wsHttpBinding" contract="TestWcf.IService1"> </endpoint> </service> </services> </span>
二、建立控制台应用程序
在解决方案中,建立控制台应用程序,并添加发布好的服务引用
2.1,编写主程序代码
<span style="font-family:KaiTi_GB2312;font-size:18px;">namespace TestWcfLibrary { class Program { static void Main(string[] args) { Console.WriteLine("-------------Angel,TestWcf Begin--------------"); TestWcfLibrary.ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(); Console.WriteLine("The message my client sent is:Angel,you are so beautiful!"); Console.WriteLine(client.GetMessage("Angel,you are so beautiful!")); client.Close(); Console.WriteLine("---------------Angel,TestWcf End---------------"); Console.ReadLine(); } } } </span>
2.2,运行效果
三、学习总结
这是对于WCF的一个简单的实例学习,而对于目前项目中所用的WCF,包括在配置文件中引入Spring容器进行开发,这些都将在后续的博客中进行分析总结。相关文章推荐
- 超长字符串换行处理
- 母牛问题
- <climits>头文件
- 加油小白
- URL结构
- leetcode:Largest Number
- HDU【3038】How Many Answers Are Wrong
- 最优布线问题
- 图(邻接矩阵)
- pig: ERROR 1070
- 2776 寻找代表元(匈牙利算法)
- Android Fragment完全解析,关于碎片你所需知道的一切
- C#里面Attribute的使用方法
- dx11学习笔记-1.创建一个空窗口
- 《笨办法学Python》 第38课手记
- PHP操作Mysql疑问?
- Nmap
- Codeforces Round #342 (Div. 2) 625C K-special Tables(脑洞)
- 【翻译自mos文章】怎么startup/shutdown PDB?
- UITableView如何撤销移动操作