WCF入门

来源:互联网 发布:户型图数据录入 编辑:程序博客网 时间:2024/05/21 13:38

Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程模型。
WCF的基本概念:
  地址:定义服务的地址
  绑定:定义服务的通讯方式(传输协议、编码方案)
  契约:定义服务的具体实现
  终结点:由地址、绑定和契约共同构成一个终结点,服务器通过终结点向客户端公开服务,客户端通过终结点调用服务。

下面通过一个简单的服务示例来认识WCF(只需让本例顺利运行即可,关于代码中的各种类型及WCF的相关概念我们将在后续介绍):
1.新建项目,名称 XfrogWCFService,解决方案名称 XfrogWCFStudy001,模板选择类库,选择.NET Framework 3.0版本
2.修改Class1.cs文件名称为 IFirstService.cs
3.添加引用 System.ServiceModel
4.修改IFirstService.cs代码如下:

隐藏行号 复制代码 IFirstService.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ServiceModel;
  5. namespace Xfrog.Study.WCF
  6. {
  7.     [ServiceContract]
  8.     public interface IFirstService
  9.     {
  10.         [OperationContract]
  11.         String GetData(String name);
  12.     }
  13. }

 

我们定义了一个IFirstService接口,注意在接口上申明了ServiceContract特性,即服务契约,表明该接口是一个服务。方法上声明了OperationContract特性,表示该方法是IFirstService的一个服务方法,客户端可远程调用该方法。

5.添加一个新类,文件名为FirstService.cs,代码如下:

隐藏行号 复制代码 FirstService.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Xfrog.Study.WCF
  5. {
  6.     public class FirstService : IFirstService
  7.     {
  8.         string IFirstService.GetData(String name)
  9.         {
  10.             return String.Format("Hello {0},Welcome To WCF!", name);
  11.         }
  12.     }
  13. }

 

OK,到此我们的服务代码已经编写完成,下面我们必须为服务提供一个运行的宿主,通过该宿主程序来启动我们的服务。
6.在同一解决方案下新建一个项目,名称为Host,类型为控制台应用程序
7.Host项目中添加引用,引用项目XfrogWCFService,然后再添加引用:System.ServiceModel
8.修改Program.cs代码如下:

隐藏行号 复制代码 Program.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ServiceModel;
  5. using Xfrog.Study.WCF;
  6. namespace Host
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             using (ServiceHost host = new ServiceHost(typeof(FirstService)))
  13.             {
  14.                 host.Open();
  15.                 Console.WriteLine("服务已启动,按任意键中止...");
  16.                 Console.ReadKey(true);
  17.                 host.Close();
  18.             }
  19.         }
  20.     }
  21. }

 


以上,我们已经实现了服务以及为服务提供了一个运行宿主,即契约部分已经完成,下面我们为服务指定地址及绑定,本步骤可通过WCF的管理工具,或直接编写配置文件来完成。我们先采用手工编写配置文件的方式:
9.新建项,选择应用程序配置文件,文件名App.config保持不变。
10.修改app.config内容如下:

隐藏行号 复制代码 App.config
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.   <system.serviceModel>
  4.     <services>
  5.       <service name="Xfrog.Study.WCF.FirstService" behaviorConfiguration="behaviorConfiguration">
  6.         <host>
  7.           <baseAddresses>
  8.             <add baseAddress="http://localhost:8000/"/>
  9.           </baseAddresses>
  10.         </host>
  11.         <endpoint address="" binding="basicHttpBinding" contract="Xfrog.Study.WCF.IFirstService"></endpoint>
  12.       </service>
  13.     </services>
  14.     <behaviors>
  15.       <serviceBehaviors>
  16.         <behavior name="behaviorConfiguration">
  17.           <serviceMetadata httpGetEnabled="true"/>
  18.         </behavior>
  19.       </serviceBehaviors>
  20.     </behaviors>
  21.   </system.serviceModel>
  22. </configuration>

 


11.设置Host项目为启动项目,启动调试。控制台上显示服务已启动后,打开浏览器输入服务地址:http://localhost:8000/ ,浏览器中会打开我们的服务页面,这表示我们的服务已经启动成功,客户端可通过该地址访问我们的服务了。
下面,我们将创建一个客户端来访问我们的服务
12.在同一解决方案下新建一个项目,名称为Client,类型为控制台应用程序
13.我们将使用微软的svcutil工具生成FirstService服务的客户端代理类,通过开始菜单/Microsoft Visual Studio 2008/Visual Studio Tools/Visual Studio 2008命令提示,启动命令环境。
14.确认FirstService服务已启动
15.切换当前路径到解决方案目录:
cd G:\Study\WCF\XfrogWCFStudy001
g:
16.输入命令:
svcutil http://localhost:8000/?wsdl /o:FirstServiceClient.cs
执行成功后,会在解决方案目录下生成两个文件:FirstServiceClient.cs 和output.config
17.中止Host项目的调试,回到Client项目,选择添加 现有项 ,然后选择这两个文件,添加后,将output.config重命名为App.config
18.Client项目中添加引用,选择System.ServiceModel
19.修改program.cs代码如下:

隐藏行号 复制代码 Program.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Client
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             String key = "";
  11.             while (String.Compare(key, "Q", true)!=0)
  12.             {
  13.                 FirstServiceClient client = new FirstServiceClient();
  14.                 Console.WriteLine(client.GetData(key));
  15.                 key = Console.ReadLine();
  16.             }
  17.         }
  18.     }
  19. }

 


20.Host项目上单击右键,选择调试—>启动新实例,待服务启动完成后,在Client项目上单击右键,选择调试—>启动新实例。输入任意字符回车,Client将调用FirstService服务GetData方法,返回一个字符串。输入q退出Client。