WCF入门到精通系列教程第1篇:Hello WCF

本文为第一篇:Hello WCF。
      WCF全称为Windows Communication Foundation,是Microsoft平台上的SOA架构,用于构建分布式和可交互操作的应用程序。它统一ASMX, .NET Remoting, 与Enterprise Services的开发模型,为各种应用提供单一的编程模型,基于配置驱动的协议选择,消息格式,进程分配等。
开发环境:Visual Studio 2010 + NET Framework 4.0。
本章我们通过一个简单的DEMO来创建一个WCF程序。
1、打开VS2010,选择C#语言下的创建WCF程序,选中WCF Service Library,修改解决方案名称为HelloWCF与项目名称为HelloServiceLibrary,点击确定。
2、删除HelloServiceLibrary项目中生成的IService1.cs与Services1.cs文件。
3、新建IHelloWCF接口文件,代码如下:
 //OperationContract为服务契约[ServiceContract] public interface IHelloWCF{ //OperationContract为方法契约[OperationContract] string GetMessage(string msg);}
4、新建HelloWCF文件,代码如下:
 public class HelloWCF : IHelloWCF{ public string GetMessage(string msg){ return string.Format("The server received message is : {0}", msg);}}
5、修改HelloServiceLibrary中的App.config文件:
修改服务名称为:<service name="HelloServiceLibrary.HelloWCF">
修改端契约为:<endpoint address="" binding="wsHttpBinding" contract="HelloServiceLibrary.IHelloWCF">
修改服务地址为:<add baseAddress="http://localhost:8732/Design_Time_Addresses/HelloServiceLibrary/HelloWCF/" />
配置如下:
 Code [http://www.xueit.com]<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <!-- When deploying the service library project, the content of the config file must be added to the host's app.config file. System.Configuration does not support config files for libraries. --> <system.serviceModel> <services> <service name="HelloServiceLibrary.HelloWCF"> <endpoint address="" binding="wsHttpBinding" contract="HelloServiceLibrary.IHelloWCF"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8732/Design_Time_Addresses/HelloServiceLibrary/HelloWCF/" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="True"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
6、新建控制台应用程序Client,添加Service Reference,修改名称空间为HelloServiceLibrary。
7、在Program类中的Main函数中添加代码。
 Code [http://www.xueit.com] static void Main(string[] args) { Console.WriteLine("------------------HelloWCFClient Begin------------------"); HelloServiceLibrary.HelloWCFClient client = new HelloServiceLibrary.HelloWCFClient(); Console.WriteLine("The client sent message is :Hello WCF"); Console.WriteLine(client.GetMessage("Hello WCF")); client.Close(); Console.WriteLine("------------------HelloWCFClient End------------------"); Console.ReadLine(); }8、F5运行调试程序,在控制台上我们将看到客户端调用WCF服务端返回的结果。
------------------HelloWCFClient Begin------------------The client sent message is :Hello WCFThe server received message is : Hello WCF------------------HelloWCFClient End------------------
    至此,一个简单的WCF应用程序创建完成了,下章将详细介绍WCF的契约设计

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值