本節目標
- 掌握接口
- 理解契約式編程
- 創建宿主程序
- 創建客戶端程序訪問服務
什么是接口
認識一下接口
必須知道的接口特性
- 接口不可以被實例化(常作為類型使用)
- 實現類必須實現接口的所有方法(抽象類除外)
- 實現類可以實現多個接口(Java,C#中的多繼承)
- 接口中的變量都是靜態常量
理解接口
定義一個接口是為了遵循同一種規范,便於程序的擴展。
接口是一種能力
接口是一種約定
關鍵字
Interface
public
abstract
理解契約式編程
契約合同能保障雙方的利益,對客戶來說,合同規定了供應者要做的工作;對供應者來說,合同說明了如果約定的條件不滿足,供應者沒有義務一定要完成規定的任務。該道理同樣也適用於軟件. 所以,契約式編程是編程的一種方法。
引入契約觀念之后,這種Client 與 Server 關系被打破,大家都是平等的,你需要我正確提供服務,那么你必須滿足我提出的條件,否則我沒有義務“排除萬難”地保證完成任務。
WCF服務契約
服務契約描述了暴露給外部的類型(接口或類)、服務所支持的操作、使用的消息交換模式和消息的格式。每個WCF服務必須實現至少一個服務契約。使用服務契約必須要引用命名空間System.ServiceModel 。
ServiceContractAttribute:該特性可被用來作用於子類或者接口之上,並允許重復聲明。
OperationContractAttribute:只有定義了該特性的方法才會被放入服務之中。
1、新建服務程序
新建項目——類庫,這里我們先不直接新建一個WCF服務,而是新建一個類庫,命名為HelloService
添加引用
刪除Class1.cs,然后新建一個接口IHelloService.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel; //添加命名空間,這是WCF的核心庫
- namespace HelloService
- {
- [ServiceContract]
- public interface IHelloService
- {
- [OperationContract]
- string SayHello(string name);
- }
- }
添加HelloService類:
- public class HelloService:IHelloService
- {
- public string SayHello(string name)
- {
- return "你好,我是:" + name;
- }
- }
ServiceHost類型:當IIS活WAS作為宿主程序時,IIS和WAS會自動創建ServiceHost類型。
手動創建的基本語法:public ServiceHost(Type serviceType,params Uri[] baseAddresses);
2、新建宿主
新建項目——控制台應用程序
然后添加System.ServiceModel引用,和項目引用HelloService,引用之前的類庫項目。
HelloServiceHost 項目中Program.cs代碼如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using System.ServiceModel.Channels; //使用到了綁定
- namespace HelloServiceHost
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (MyHelloHost host=new MyHelloHost())
- {
- host.Open();
- Console. Console.ReadLine();
- }
- }
- }
- public class MyHelloHost:IDisposable
- {
- /// <summary>
- /// 定義一個服務對象
- /// </summary>
- private ServiceHost _myHelloHost;
- public const string BaseAddress = "net.pipe://localhost"; //基地址
- public const string HelloServiceAddress = "Hello"; //可選地址
- public static readonly Type ServiceType =typeof(HelloService.HelloService); //服務契約實現類型
- public static readonly Type ContractType =typeof(HelloService.IHelloService); //服務契約接口
- public static readonly Binding HelloBinding = new NetNamedPipeBinding(); //服務定義一個綁定
- /// <summary>
- /// 構造方法
- /// </summary>
- public MyHelloHost()
- {
- CreateHelloServiceHost();
- }
- /// <summary>
- /// 構造服務對象
- /// </summary>
- protected void CreateHelloServiceHost()
- {
- _myHelloHost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress) });//創建服務對象
- _myHelloHost.AddServiceEndpoint(ContractType, HelloBinding,HelloServiceAddress); //添加終結點
- }
- /// <summary>
- /// 打開服務方法
- /// </summary>
- public void Open()
- {
- Console.WriteLine("開始啟動服務...");
- _myHelloHost.Open();
- Console.WriteLine("服務已啟動");
- }
- /// <summary>
- /// 銷毀服務宿主對象實例
- /// </summary>
- public void Dispose()
- {
- if (_myHelloHost != null)
- (_myHelloHost asIDisposable).Dispose();
- }
- }
- }
3、新建客戶端調用程序
新建項目——控制台應用程序
HelloClient項目中Program.cs代碼如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using System.ServiceModel.Channels;
- using HelloService;
- namespace HelloClient
- {
- class Program
- {
- static void Main(string[] args)
- {
- using(HelloProxy proxy=new HelloProxy())
- {
- //利用代理調用方法
- Console.WriteLine(proxy.Say("鄭少秋"));
- Console.ReadLine();
- }
- }
- }
- [ServiceContract]
- interface IService
- {
- [OperationContract]
- string Say(string name);
- }
- class HelloProxy:ClientBase<IHelloService>,IService
- {
- public static readonly Binding HelloBinding = new NetNamedPipeBinding(); //硬編碼定義綁定
- //硬編碼定義地址 注意:這里要和之前服務定義的地址保持一直
- public static readonly EndpointAddress HelloAddress =new EndpointAddress(new Uri("net.pipe://localhost/Hello"));
- public HelloProxy() : base(HelloBinding,HelloAddress) { } //構造方法
- public string Say(string name)
- {
- //使用Channel屬性對服務進行調用
- return Channel.SayHello(name);
- }
- }
- }
先運行HelloServiceHost
然后運行HelloClient