WCF學習筆記(二),服務契約

一、服務契約介紹
       
我個人理解服務契約是一組公開的操作,其中公開的操作(OperationContract)只能定義在方法(Method)上。對於我們要公開的服務我們可以在接口或者類上加上標識ServiceContract。但是我們一般情況下,會把ServiceContract定義在接口上而不是類上,這樣有幾個好處:
       1.方便契約的繼承,不同的類型可以去實現相同的契約,重用性高。
       2.同一服務可以去實現多個契約。
       3.可以隨時去修改服務類型,而不需去修改接口。
       下面定義一個服務的契約:   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    [ServiceContract(Name
="Service_Calucator",Namespace="Henllyee")]
    
public interface ICalucator
    
{
        [OperationContract]
        
int Add(int x, int y);

    }

}

我們在上面首先定義了一個接口名稱爲:ICalucator,然後我們要將這個接口公開爲服務契約在上面加上屬性標識[ServiceContract],其中Name可以爲契約指定別名,這樣的話,如果我們在客戶端遇到相同的接口時可以通過Name來制定別名區別開來。公開的操作爲Add方法,在上面標識[OperationContract]即可。

二、方法的重載
    按照我們正常的方法去重載的話,只需要方法的參數不同(個數、類型)就可以實現方法的重載。但是我們在服務契約的定義的時候是不能怎樣的,wdsl是編譯同不過的如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    [ServiceContract(Name
="Service_Calucator",Namespace="Henllyee")]
    
public interface ICalucator
    
{
        [OperationContract]
        
int Add(int x, int y);

        [OperationContract]
        
double Add(double x, double y);
    }

}

上面的方法是編譯不能通過的。但是我們有一種解決的辦法可以去解決這樣的問題,就是通過OperationContract的Name屬性來設定方法的別名是實現方法的重載。如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    [ServiceContract(Name
="Service_Calucator",Namespace="Henllyee")]
    
public interface ICalucator
    
{
        [OperationContract(Name
="IntAdd")]
        
int Add(int x, int y);

        [OperationContract(Name
="DoubleAdd")]
        
double Add(double x, double y);
    }

}


下面我們通過數據元的方式來配置一個宿主主機,我們添加一個控制檯的程序,通過配置App.Config來實現。
在App.Config中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.serviceModel>
    
<services>
      
<service name="Service.Calucator" behaviorConfiguration="mex">
        
<host>
          
<baseAddresses>
            
<add baseAddress="http://localhost:8888"/>
          
baseAddresses>
        
host>
        
<endpoint address="Calucator" binding="basicHttpBinding" contract="Service.ICalucator">endpoint>
      
service>
    
services>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="mex">
          
<serviceMetadata httpGetEnabled="true"/>
        
behavior>
      
serviceBehaviors>
    
behaviors>
  
system.serviceModel>
configuration>

在主程序中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Service;

namespace Host
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            ServiceHost host 
= null;
            
try
            
{
                host 
= new ServiceHost(typeof(Calucator));
                host.Open();

                Console.Write(
"Host is opening now! Press any key to stop");
                Console.Read();
            }

            
finally
            
{
                host.Close();
            }

        }

    }

}

然後我們啓動主機,在瀏覽器中輸入地址:http://www.systhinker.com/?wsdl 。我們可以看到:




我們可以看到wsdl編譯時已經將名稱編譯成爲了我們Name中定義的別名。

WCF學習筆記系列鏈接:

WCF學習筆記(一),WCF預覽

WCF學習筆記(二),服務契約

WCF學習筆記(三),數據契約

WCF學習筆記(四),數據契約的事件

WCF學習筆記(五),數據契約的已知類型

WCF學習筆記(六),實例類型

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章