代码改变世界

WCF学习总结2

2011-05-10 15:45  RyanXiang  阅读(486)  评论(0编辑  收藏  举报

我们来写一个WCF的HelloWorld来增加对WCF的认识。

、创建解决方案

首先创建如下的工程,建立一个解决方案,添加Client、Service、和Host的Console Application。如下图:

飞信截屏未命名

该实例主要实现1、WCF服务端。2、WCF客户端。3、WCF的宿主环境,如下图:

WCF HOST.mp3

二、编写WCF服务端程序

现在我们开始写WCF服务端程序了,首先添加System.ServiceModel引用。然后编写下面的代码:

IGetDataService.cs

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5: using System.ServiceModel;
   6:  
   7: namespace Service
   8: {
   9:     //定义契约
  10:     [ServiceContract]
  11:     public interface IGetDataService
  12:     {
  13:         [OperationContract]
  14:         string GetData();
  15:     }
  16: }

GetDataService.cs

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: namespace Service
   7: {
   8:     public class GetDataService : IGetDataService
   9:     {
  10:         public string GetData()
  11:         {
  12:             return "Hello WCF";
  13:         }
  14:     }
  15: }

OK,服务端代码已经编写完毕。但是WCF服务如果要想运行的话,必须有一个宿主环境(Host)如下图,现在我们来实现这个宿主环境。

三、实现WCF宿主环境,并启动WCF服务。

添加System.ServiceModel引用后,宿主环境代码如下:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5: using System.ServiceModel;
   6: using System.ServiceModel.Description;
   7: using Service;
   8:  
   9: namespace Host
  10: {
  11:     class Program
  12:     {
  13:         static void Main(string[] args)
  14:         {
  15:             //定义ServiceHost
  16:             ServiceHost host = new ServiceHost(typeof(GetDataService));
  17:             //添加终结点
  18:             host.AddServiceEndpoint(typeof(IGetDataService), new WSHttpBinding(), "http://127.0.0.1:6666/getData");
  19:             //查找元数据
  20:             ServiceMetadataBehavior behavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
  21:             if (behavior == null)
  22:             {
  23:                 behavior = new ServiceMetadataBehavior();
  24:                 behavior.HttpGetEnabled = true;
  25:                 behavior.HttpGetUrl = new Uri("http://127.0.0.1:6666/getData/metadata");
  26:                 host.Description.Behaviors.Add(behavior);
  27:             }
  28:             else 
  29:             {
  30:                 behavior.HttpGetEnabled = true;
  31:             }
  32:  
  33:             //注册一个事件,当服务开启的时候打印出服务开始
  34:             host.Opened += delegate
  35:             {
  36:                 Console.Write("WCF 服务已经开启");
  37:             };
  38:  
  39:             //开启服务
  40:             host.Open();
  41:             Console.ReadKey();
  42:         }
  43:     }
  44: }

运行宿主环境,启动服务。在浏览器中输入:http://localhost:8818/getData/metadata,如果获取到了元数据,则证明服务宿主编写成功。如下图:飞信截屏未命名

四、实现客户端,并访问WCF服务。

首先添加服务引用,Reference ----Add Service Reference 按下图添加:点击OK后VS会为我们生成客户端的访问代码,我们调用即可。

飞信截屏未命名

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: namespace Client
   7: {
   8:     class Program
   9:     {
  10:         static void Main(string[] args)
  11:         {
  12:             //调用客户端生成的代码
  13:             GetDataServiceClient client = new GetDataServiceClient();
  14:             Console.Write(client.GetData());
  15:             Console.ReadKey();
  16:         }
  17:     }
  18:     public partial class GetDataServiceClient : System.ServiceModel.ClientBase<Client.GetDataService.IGetDataService>, Client.GetDataService.IGetDataService
  19:     {
  20:  
  21:         public GetDataServiceClient()
  22:         {
  23:         }
  24:  
  25:         public GetDataServiceClient(string endpointConfigurationName) :
  26:             base(endpointConfigurationName)
  27:         {
  28:         }
  29:  
  30:         public GetDataServiceClient(string endpointConfigurationName, string remoteAddress) :
  31:             base(endpointConfigurationName, remoteAddress)
  32:         {
  33:         }
  34:  
  35:         public GetDataServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
  36:             base(endpointConfigurationName, remoteAddress)
  37:         {
  38:         }
  39:  
  40:         public GetDataServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
  41:             base(binding, remoteAddress)
  42:         {
  43:         }
  44:  
  45:         public string GetData()
  46:         {
  47:             return base.Channel.GetData();
  48:         }
  49:     }
  50: }