所有與 Windows Communication Foundation (WCF) 服務的通訊都會透過服務的 端點 進行。 端點可讓用戶端存取 WCF 服務所提供的功能。 本節描述端點的結構,並概述如何在組態和程式代碼中定義端點。
端點的結構
每個端點都包含一個位址,指出要在哪裡尋找端點、指定用戶端如何與端點通訊的系結,以及識別可用方法的合約。
位址。 位址可唯一識別端點,並告知潛在取用者服務所在的位置。 它是由 EndpointAddress 位址在 WCF 物件模型中表示,該位址包含統一資源識別元 (URI) 和地址屬性,其中包含身分識別、某些 Web 服務描述語言 (WSDL) 元素,以及選擇性標頭的集合。 選擇性標頭會提供額外的詳細尋址資訊,以識別或與端點互動。 如需詳細資訊,請參閱 指定端點位址。
綁定。 系結會指定如何與端點通訊。 系結會指定端點如何與世界通訊,包括要使用的傳輸通訊協定(例如 TCP 或 HTTP),要用於訊息的編碼方式(例如文字或二進位),以及需要哪些安全性需求(例如安全套接字層 [SSL] 或 SOAP 訊息安全性)。 如需詳細資訊,請參閱 使用系結設定服務和用戶端。
服務合約。 服務合約概述端點向客戶端公開的功能。 合約會指定用戶端可以呼叫的作業、訊息的形式,以及呼叫作業所需的輸入參數或數據類型,以及用戶端可以預期的處理或回應訊息類型。 三種基本類型的合約對應至基本訊息交換模式(MEP):數據報(單向)、要求/回復和雙工(雙向)。 服務合約也可以在存取時採用數據和訊息合約,以要求特定數據類型和訊息格式。 如需如何定義服務合約的詳細資訊,請參閱 設計服務合約。 請注意,客戶端可能也需要實作稱為回呼合約的服務定義合約,以接收雙向消息交換模式 (MEP) 中服務的訊息。 如需詳細資訊,請參閱 雙工服務。
您可以使用程式碼具體指定服務的端點,或者透過組態以宣告方式指定服務的端點。 如果未指定任何端點,則運行環境會為服務所實作的每個服務合約,在每個基位址新增一個預設端點。 在程式代碼中定義端點通常並不實用,因為已部署服務的系結和位址通常與開發服務時所使用的端點不同。 一般而言,使用組態而非程式代碼來定義服務端點會比較實用。 將系結和尋址資訊保留在程序代碼中,可讓它們變更,而不需要重新編譯和重新部署應用程式。
備註
當新增一個實行冒用的服務端點時,您必須使用AddServiceEndpoint方法之一或GetContract(Type, Type)方法,將合約正確載入至新的ServiceDescription物件中。
在程式代碼中定義端點
下列範例說明如何使用下列程式代碼指定端點:
定義一種類型為
IEcho的服務合約,它接受某人的名稱並回應「Hello <name>!」。根據
Echo合約定義的類型,實作IEcho服務。請指定服務的端點位址
http://localhost:8000/Echo。使用
Echo結構來配置WSHttpBinding服務。
namespace Echo
{
// Define the contract for the IEcho service
[ServiceContract]
public interface IEcho
{
[OperationContract]
String Hello(string name)
}
// Create an Echo service that implements IEcho contract
class Echo : IEcho
{
public string Hello(string name)
{
return "Hello" + name + "!";
}
public static void Main ()
{
//Specify the base address for Echo service.
Uri echoUri = new Uri("http://localhost:8000/");
//Create a ServiceHost for the Echo service.
ServiceHost serviceHost = new ServiceHost(typeof(Echo),echoUri);
// Use a predefined WSHttpBinding to configure the service.
WSHttpBinding binding = new WSHttpBinding();
// Add the endpoint for this service to the service host.
serviceHost.AddServiceEndpoint(
typeof(IEcho),
binding,
echoUri
);
// Open the service host to run it.
serviceHost.Open();
}
}
}
' Define the contract for the IEcho service
<ServiceContract()> _
Public Interface IEcho
<OperationContract()> _
Function Hello(ByVal name As String) As String
End Interface
' Create an Echo service that implements IEcho contract
Public Class Echo
Implements IEcho
Public Function Hello(ByVal name As String) As String _
Implements ICalculator.Hello
Dim result As String = "Hello" + name + "!"
Return result
End Function
' Specify the base address for Echo service.
Dim echoUri As Uri = New Uri("http://localhost:8000/")
' Create a ServiceHost for the Echo service.
Dim svcHost As ServiceHost = New ServiceHost(GetType(HelloWorld), echoUri)
' Use a predefined WSHttpBinding to configure the service.
Dim binding As New WSHttpBinding()
' Add the endpoint for this service to the service host.
serviceHost.AddServiceEndpoint(GetType(IEcho), binding, echoUri)
' Open the service host to run it.
serviceHost.Open()
備註
服務主機以基地址建立,然後相對於基地址的其餘地址被指定為端點的一部分。 地址的劃分使得為主機上的服務定義多個端點更加方便。
備註
在服務應用程式中,ServiceDescription的屬性不得在OnOpening上的ServiceHostBase方法之後進行修改。 某些成員,例如 Credentials 屬性和 AddServiceEndpoint 和 ServiceHostBase 上的 ServiceHost 方法,如果在該點之後進行修改,則會拋出例外。 其他人則允許您修改它們,但結果未定義。
同樣地,在用戶端上,在對ServiceEndpoint呼叫OnOpening之後,不得修改ChannelFactory的值。 Credentials屬性會在修改超過該點時擲回例外狀況。 其他用戶端描述值可以修改而不發生錯誤,但結果未定義。
無論是針對服務還是客戶端,建議您先修改描述,再呼叫 Open。
在組態中定義端點
建立應用程式時,您通常會想要將決策延遲給正在部署應用程式的系統管理員。 例如,通常無法事先知道服務位址 (URI) 會是什麼。 建立服務之後,最好允許系統管理員在建立服務之後執行此動作,而不是將位址硬式編碼。 這項彈性是透過設定來完成。 如需詳細資訊,請參閱 設定服務。
備註
使用 ServiceModel 元數據公用程式工具 (Svcutil.exe) 搭配/config:檔名[,檔名]開關來快速建立組態檔。
使用預設端點
如果未在程式代碼或組態中指定任何端點,則運行時間會針對服務所實作的每個服務合約,為每個基位址新增一個默認端點,以提供預設端點。 基位址可以在程式碼或組態中指定,當在 Open() 上呼叫 ServiceHost 時會新增預設端點。 此範例與上一節中的範例相同,但由於未指定任何端點,因此會新增預設端點。
namespace Echo
{
// Define the contract for the IEcho service
[ServiceContract]
public interface IEcho
{
[OperationContract]
String Hello(string name)
}
// Create an Echo service that implements IEcho contract
public class Echo : IEcho
{
public string Hello(string name)
{
return "Hello" + name + "!";
}
public static void Main ()
{
//Specify the base address for Echo service.
Uri echoUri = new Uri("http://localhost:8000/");
//Create a ServiceHost for the Echo service.
ServiceHost serviceHost = new ServiceHost(typeof(Echo),echoUri);
// Open the service host to run it. Default endpoints
// are added when the service is opened.
serviceHost.Open();
}
}
}
' Define the contract for the IEcho service
<ServiceContract()> _
Public Interface IEcho
<OperationContract()> _
Function Hello(ByVal name As String) As String
End Interface
' Create an Echo service that implements IEcho contract
Public Class Echo
Implements IEcho
Public Function Hello(ByVal name As String) As String _
Implements ICalculator.Hello
Dim result As String = "Hello" + name + "!"
Return result
End Function
' Specify the base address for Echo service.
Dim echoUri As Uri = New Uri("http://localhost:8000/")
' Open the service host to run it. Default endpoints
' are added when the service is opened.
serviceHost.Open()
如果明確提供端點,在呼叫 AddDefaultEndpoints 之前,仍然可以先在 ServiceHost 上呼叫 Open 來新增預設端點。 如需預設端點的詳細資訊,請參閱 簡化的組態 和 WCF 服務的簡化組態。