WCF入门

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.  
  6. namespace Xfrog.Study.WCF
    
  7. {
    
  8.     [ServiceContract]
    
  9.     public interface IFirstService
    
  10.     {
    
  11.         [OperationContract]
    
  12.         String GetData(String name);
    
  13.     }
    
  14. }
    
  15.  

 

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

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

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

 

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.  
  7. namespace Host
    
  8. {
    
  9.     class Program
    
  10.     {
    
  11.         static void Main(string[] args)
    
  12.         {
    
  13.             using (ServiceHost host = new ServiceHost(typeof(FirstService)))
    
  14.             {
    
  15.                 host.Open();
    
  16.                 Console.WriteLine("服务已启动,按任意键中止...");
    
  17.                 Console.ReadKey(true);
    
  18.                 host.Close();
    
  19.             }
    
  20.         }
    
  21.     }
    
  22. }
    
  23.  

 


以上,我们已经实现了服务以及为服务提供了一个运行宿主,即契约部分已经完成,下面我们为服务指定地址及绑定,本步骤可通过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.  
  5. namespace Client
    
  6. {
    
  7.     class Program
    
  8.     {
    
  9.         static void Main(string[] args)
    
  10.         {
    
  11.             String key = "";
    
  12.             while (String.Compare(key, "Q", true)!=0)
    
  13.             {
    
  14.                 FirstServiceClient client = new FirstServiceClient();
    
  15.                 Console.WriteLine(client.GetData(key));
    
  16.  
  17.                 key = Console.ReadLine();
    
  18.             }
    
  19.         }
    
  20.     }
    
  21. }
    
  22.  

 


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值