WCF入門教程2——建立第一個WCF程序

本節目標編程

  • 掌握接口
  • 理解契約式編程
  • 建立宿主程序
  • 建立客戶端程序訪問服務

什麼是接口

認識一下接口

編碼

必須知道的接口特性
spa

  • 接口不能夠被實例化(常做爲類型使用)
  • 實現類必須實現接口的全部方法(抽象類除外)
  • 實現類能夠實現多個接口(Java,C#中的多繼承)
  • 接口中的變量都是靜態常量

理解接口.net

定義一個接口是爲了遵循同一種規範,便於程序的擴展。
接口是一種能力
接口是一種約定
關鍵字
Interface
public
abstract
代理

理解契約式編程

契約合同能保障雙方的利益,對客戶來講,合同規定了供應者要作的工做;對供應者來講,合同說明了若是約定的條件不知足,供應者沒有義務必定要完成規定的任務。該道理一樣也適用於軟件. 因此,契約式編程是編程的一種方法。code

引入契約觀念以後,這種Client 與 Server 關係被打破,你們都是平等的,你須要我正確提供服務,那麼你必須知足我提出的條件,不然我沒有義務「排除萬難」地保證完成任務。對象

WCF服務契約
blog

服務契約描述了暴露給外部的類型(接口或類)、服務所支持的操做、使用的消息交換模式和消息的格式。每一個WCF服務必須實現至少一個服務契約。使用服務契約必需要引用命名空間System.ServiceModel 。繼承

ServiceContractAttribute:該特性可被用來做用於子類或者接口之上,並容許重複聲明。接口

OperationContractAttribute:只有定義了該特性的方法纔會被放入服務之中。

一、新建服務程序

新建項目——類庫,這裏咱們先不直接新建一個WCF服務,而是新建一個類庫,命名爲HelloService


添加引用

刪除Class1.cs,而後新建一個接口IHelloService.cs

[csharp] view plain copy 在CODE上查看代碼片派生到個人代碼片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel; //添加命名空間,這是WCF的核心庫  
  6.   
  7. namespace HelloService  
  8.   
  9. {  
  10.    [ServiceContract]  
  11.    public interface IHelloService  
  12.    {  
  13.        [OperationContract]  
  14.        string SayHello(string name);  
  15.    }  
  16. }  

添加HelloService類:

[csharp] view plain copy 在CODE上查看代碼片派生到個人代碼片
  1. public class HelloService:IHelloService  
  2.    {  
  3.        public string SayHello(string name)  
  4.        {  
  5.             return "你好,我是:" + name;  
  6.        }  
  7.    }  

ServiceHost類型:當IISWAS做爲宿主程序時,IISWAS會自動建立ServiceHost類型。

手動建立的基本語法:public ServiceHost(Type serviceType,params Uri[] baseAddresses);

二、新建宿主

新建項目——控制檯應用程序


而後添加System.ServiceModel引用,和項目引用HelloService,引用以前的類庫項目。

HelloServiceHost 項目中Program.cs代碼以下:

[csharp] view plain copy 在CODE上查看代碼片派生到個人代碼片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;   
  5.   
  6. using System.ServiceModel;  
  7. using System.ServiceModel.Channels; //使用到了綁定   
  8.   
  9. namespace HelloServiceHost  
  10. {  
  11.    class Program  
  12.    {  
  13.        static void Main(string[] args)  
  14.        {  
  15.             using (MyHelloHost host=new MyHelloHost())  
  16.             {  
  17.                 host.Open();  
  18.                 Console. Console.ReadLine();  
  19.             }  
  20.        }  
  21.    }  
  22.   
  23.    public class MyHelloHost:IDisposable  
  24.    {  
  25.        /// <summary>  
  26.        /// 定義一個服務對象  
  27.        /// </summary>  
  28.        private ServiceHost _myHelloHost;   
  29.        public const string BaseAddress = "net.pipe://localhost"; //基地址  
  30.        public const string HelloServiceAddress = "Hello"; //可選地址  
  31.        public static readonly Type ServiceType =typeof(HelloService.HelloService);  //服務契約實現類型  
  32.        public static readonly Type ContractType =typeof(HelloService.IHelloService);  //服務契約接口  
  33.        public static readonly Binding HelloBinding = new NetNamedPipeBinding(); //服務定義一個綁定  
  34.    
  35.        /// <summary>  
  36.        /// 構造方法  
  37.        /// </summary>  
  38.        public MyHelloHost()  
  39.        {  
  40.             CreateHelloServiceHost();  
  41.        }  
  42.   
  43.        /// <summary>  
  44.        /// 構造服務對象  
  45.        /// </summary>  
  46.        protected void CreateHelloServiceHost()  
  47.        {  
  48.             _myHelloHost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress) });//建立服務對象  
  49.            _myHelloHost.AddServiceEndpoint(ContractType, HelloBinding,HelloServiceAddress); //添加終結點  
  50.        }  
  51.   
  52.        /// <summary>  
  53.        /// 打開服務方法  
  54.        /// </summary>  
  55.        public void Open()  
  56.        {  
  57.             Console.WriteLine("開始啓動服務...");  
  58.             _myHelloHost.Open();  
  59.             Console.WriteLine("服務已啓動");  
  60.        }  
  61.   
  62.        /// <summary>  
  63.        /// 銷燬服務宿主對象實例  
  64.        /// </summary>  
  65.        public void Dispose()  
  66.        {  
  67.             if (_myHelloHost != null)  
  68.                 (_myHelloHost asIDisposable).Dispose();  
  69.        }  
  70.    }  
  71.   
  72. }  

三、新建客戶端調用程序

新建項目——控制檯應用程序

HelloClient項目中Program.cs代碼以下:

[csharp] view plain copy 在CODE上查看代碼片派生到個人代碼片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;   
  5.   
  6. using System.ServiceModel;  
  7. using System.ServiceModel.Channels;  
  8. using HelloService;  
  9.    
  10. namespace HelloClient  
  11. {  
  12.    class Program  
  13.    {  
  14.        static void Main(string[] args)  
  15.        {  
  16.           using(HelloProxy proxy=new HelloProxy())  
  17.             {  
  18.                 //利用代理調用方法  
  19.                Console.WriteLine(proxy.Say("鄭少秋"));  
  20.                Console.ReadLine();  
  21.             }  
  22.        }  
  23.    }  
  24.   
  25.    [ServiceContract]  
  26.    interface IService  
  27.    {  
  28.        [OperationContract]  
  29.        string Say(string name);  
  30.    }  
  31.   
  32.    class HelloProxy:ClientBase<IHelloService>,IService  
  33.    {  
  34.        public static readonly Binding HelloBinding = new NetNamedPipeBinding();  //硬編碼定義綁定  
  35.        //硬編碼定義地址 注意:這裏要和以前服務定義的地址保持一直  
  36.        public static readonly EndpointAddress HelloAddress =new EndpointAddress(new Uri("net.pipe://localhost/Hello"));  
  37.        public HelloProxy() : base(HelloBinding,HelloAddress) { } //構造方法  
  38.   
  39.        public string Say(string name)  
  40.        {  
  41.             //使用Channel屬性對服務進行調用  
  42.             return Channel.SayHello(name);  
  43.        }  
  44.    }  
  45. }  

先運行HelloServiceHost

而後運行HelloClient

相關文章
相關標籤/搜索