我的WCF之旅(2):Endpoint Overview
WCF實(shí)際上是構(gòu)建了一個(gè)框架,這個(gè)框架實(shí)現(xiàn)了在互聯(lián)系統(tǒng)中各個(gè)Application之間如何通信。使得Developers和Architect在構(gòu)建分布式系統(tǒng)中,無(wú)需在考慮如何去實(shí)現(xiàn)通信相關(guān)的問(wèn)題,更加關(guān)注與系統(tǒng)的業(yè)務(wù)邏輯本身。而在WCF Infrastructure中,各個(gè)Application之間的通信是由Endpoint來(lái)實(shí)現(xiàn)的。
Endpoint的結(jié)構(gòu)
Endpoint包含以下4個(gè)對(duì)象:
-
Address: Address通過(guò)一個(gè)URI唯一地標(biāo)識(shí)一個(gè)Endpoint,并告訴潛在的WCF service的調(diào)用者如何找到這個(gè)Endpoint。所以Address解決了Where to locate the WCF Service?
-
Binding: Binding實(shí)現(xiàn)在Client和Service通信的所有底層細(xì)節(jié)。比如Client與Service之間傳遞的Message是如何編碼的——text/XML, binary,MTOM;這種Message的傳遞是采用的哪種Transport——TCP, Http, Named Pipe, MSMQ; 以及采用怎樣的機(jī)制解決Secure Messaging的問(wèn)題——SSL,Message Level Security。所以Binding解決的是How to communicate with service?
-
Contract: Contract的主要的作用是暴露某個(gè)WCF Service所提供的所有有效的Functionality。從Message Exchange的層面上講,Contract實(shí)際上是抱每個(gè)Operation轉(zhuǎn)化成為相對(duì)應(yīng)的Message Exchange Pattern——MEP(Request/Response; One-way; Duplex)。所以Contract解決的是What functionalities do the Service provide?
-
Behavior: Behavior的主要作用是定制Endpoint在運(yùn)行時(shí)的一些必要的Behavior。比如Service 回調(diào)Client的Timeout;Client采用的Credential type;以及是否支持Transaction等。
當(dāng)我們Host一個(gè)WCF Service的時(shí)候,我們必須給他定義一個(gè)或多個(gè)Endpoint,然后service通過(guò)這個(gè)定義的Endpoint進(jìn)行監(jiān)聽(tīng)來(lái)自Client端的請(qǐng)求。當(dāng)我們的Application需要調(diào)用這個(gè)Service的時(shí)候,因?yàn)镃lient 和Service是通過(guò)Endpoint的進(jìn)行通信的, 所以我們必須為我們的Application定義Client端的Endpoint。只有當(dāng)Client的Endpoint和Service端某個(gè)Endpoint相互匹配(Service端可以為一個(gè)Service定義多個(gè)Endpoint),Client端的請(qǐng)求才能被Service端監(jiān)聽(tīng)到。也就是說(shuō),我們只有在Client具有一個(gè)與Service端完全匹配的Endpoint,我們才能調(diào)用這個(gè)Service。而這種匹配是比較嚴(yán)格的,比如從匹配Address方面,Client端和Service端的Endpoint Address不僅僅在URI上要完全匹配Service, 他們的Headers也需要相互匹配。對(duì)于Binding, 一般地,Client需要有一個(gè)與Service端完全一樣的Binding,他們之間才能通信。
Sample
首先給一個(gè)Sample,以便我們對(duì)在WCF Service Aplication中如何定義Endpoint有一個(gè)感性的認(rèn)識(shí)。整個(gè)Solution的結(jié)構(gòu)參照下圖,我的上一篇Blog([原創(chuàng)]我的WCF之旅(1):創(chuàng)建一個(gè)簡(jiǎn)單的WCF程序 )中有詳細(xì)的介紹。你也可以通過(guò)后面的Link下載相應(yīng)的Source Code(https://files.cnblogs.com/artech/Artech.WCFService.zip )
1. Service Contract:Artech..WCfService.Contract/ServiceContract/IGeneralCalculator.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace Artech.WCFService.Contract
{
[ServiceContract]
public interface IGeneralCalculator
{
[OperationContract]
double Add(double x, double y);
}
}
2. Service: Artech.WCFSerice.Service/GeneralCalculatorService.cs
using System;
using System.Collections.Generic;
using System.Text;
using Artech.WCFService.Contract;
namespace Artech.WCFService.Service
{
public class GeneralCalculatorService:IGeneralCalculator
{
IGeneralCalculator Members
}
}
3. Hosting: Artech.WCFService.Hosting/Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Artech.WCFService.Contract;
using Artech.WCFService.Service;
using System.ServiceModel.Description;
namespace Artech.WCFService.Hosting
{
class Program
{
static void Main(string[] args)
{
//HostCalculatorServiceViaCode();
HostCalculatorSerivceViaConfiguration();
}
/// <summary>
/// Hosting a service using managed code without any configuraiton information.
/// Please note that the related configuration data should be removed before calling the method.
/// </summary>
static void HostCalculatorServiceViaCode()
{
Uri httpBaseAddress = new Uri("http://localhost:8888/generalCalculator");
Uri tcpBaseAddress = new Uri("net.tcp://localhost:9999/generalCalculator");
using (ServiceHost calculatorSerivceHost = new ServiceHost(typeof(GeneralCalculatorService), httpBaseAddress, tcpBaseAddress))
{
BasicHttpBinding httpBinding = new BasicHttpBinding();
NetTcpBinding tcpBinding = new NetTcpBinding();
calculatorSerivceHost.AddServiceEndpoint(typeof(IGeneralCalculator), httpBinding, string.Empty);
calculatorSerivceHost.AddServiceEndpoint(typeof(IGeneralCalculator), tcpBinding, string.Empty);
ServiceMetadataBehavior behavior = calculatorSerivceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
{
if(behavior == null)
{
behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
calculatorSerivceHost.Description.Behaviors.Add(behavior);
}
else
{
behavior.HttpGetEnabled = true;
}
}
calculatorSerivceHost.Opened += delegate
{
Console.WriteLine("Calculator Service has begun to listen
");
};
calculatorSerivceHost.Open();
Console.Read();
}
}
static void HostCalculatorSerivceViaConfiguration()
{
using (ServiceHost calculatorSerivceHost = new ServiceHost(typeof(GeneralCalculatorService)))
{
calculatorSerivceHost.Opened += delegate
{
Console.WriteLine("Calculator Service has begun to listen
");
};
calculatorSerivceHost.Open();
Console.Read();
}
}
}
}
4. Service.svc: http://localhost/WCFService/ GeneralCalculatorService.svc
<%@ ServiceHost Language="C#" Debug="true" Service="Artech.WCFService.Service.GeneralCalculatorService" %>5. Client: Artech.WCFService.Client/ GeneralCalculatorClient.cs & Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Artech.WCFService.Contract;
namespace Artech.WCFService.Client
{
class GeneralCalculatorClient:ClientBase<IGeneralCalculator>,IGeneralCalculator
{
public GeneralCalculatorClient()
: base()
{ }
public GeneralCalculatorClient(string endpointConfigurationName)
: base(endpointConfigurationName)
{ }
public GeneralCalculatorClient(Binding binding, EndpointAddress address)
: base(binding, address)
{ }
IGeneralCalculator Members
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Artech.WCFService.Contract;
namespace Artech.WCFService.Client
{
class Program
{
static void Main()
{
try
{
//InvocateCalclatorServiceViaCode();
InvocateCalclatorServiceViaConfiguration();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
static void InvocateCalclatorServiceViaCode()
{
Binding httpBinding = new BasicHttpBinding();
Binding tcpBinding = new NetTcpBinding();
EndpointAddress httpAddress = new EndpointAddress("http://localhost:8888/generalCalculator");
EndpointAddress tcpAddress = new EndpointAddress("net.tcp://localhost:9999/generalCalculator");
EndpointAddress httpAddress_iisHost = new EndpointAddress("http://localhost/wcfservice/GeneralCalculatorService.svc");
Console.WriteLine("Invocate self-host calculator service ");
Invocate Self-host service
Console.WriteLine("\n\nInvocate IIS-host calculator service ");
Invocate IIS-host service
}
static void InvocateCalclatorServiceViaConfiguration()
{
Console.WriteLine("Invocate self-host calculator service ");
Invocate Self-host service
Console.WriteLine("\n\nInvocate IIS-host calculator service ");
Invocate IIS-host service
}
}
}
6. Self-Hosting Configuration: Artech.WCFService.Hosting/App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="calculatorServieBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="calculatorServieBehavior" name="Artech.WCFService.Service.GeneralCalculatorService">
<endpoint address="" binding="basicHttpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator">
</endpoint>
<endpoint address="" binding="netTcpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8888/generalcalculator" />
<add baseAddress="net.tcp://localhost:9999/generalCalculator" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
7. IIS-Host Configuration:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="calculatorServiceBehavior">
<serviceMetadata httpGetEnabled ="true"></serviceMetadata>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Artech.WCFService.Service.GeneralCalculatorService" behaviorConfiguration="calculatorServiceBehavior">
<endpoint binding="basicHttpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator"></endpoint>
</service>
</services>
</system.serviceModel>
<system.web>
<compilation debug="true">
<assemblies>
<add assembly="System.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="Microsoft.Transactions.Bridge, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="SMDiagnostics, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.IdentityModel.Selectors, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Web.RegularExpressions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Messaging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
</system.web>
</configuration>
8. Client configuration: Artech.WCFService.Client/App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:8888/generalCalculator" binding="basicHttpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" name="selfHostEndpoint_http"/>
<endpoint address="net.tcp://localhost:9999/generalCalculator" binding="netTcpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" name="selfHostEndpoint_tcp"/>
<endpoint address="http://localhost/wcfservice/GeneralCalculatorService.svc" binding="basicHttpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" name="iisHostEndpoint"/>
</client>
</system.serviceModel>
</configuration>
如何在Application中定義Endpoint
對(duì)于Self-Host的Service,絕大部分的Endpoint相關(guān)的信息都具有兩種定義方式——Managed Code 和Configuration。而對(duì)于把Service Host到IIS中的情況, Endpoint的信息一般虛擬根目錄下的Web.Config中定義。一般的我們我們不推薦使用代碼的方式Host和調(diào)用Service,這主要是基于以下的理由。首先我們開(kāi)發(fā)的環(huán)境往往與部署的環(huán)境不盡相同,才用configuration的方式是的我們可以在部署的時(shí)候通過(guò)修改配置文件以適應(yīng)新的需要。其次,對(duì)于不要出現(xiàn)的新的需要,比如對(duì)于一個(gè)原來(lái)只供Internet內(nèi)部使用的Service,我們一般會(huì)定義一個(gè)基于TCP的Endpoint,現(xiàn)在出現(xiàn)來(lái)自于Internet的潛在用戶(hù),我們只需要通過(guò)修改Config文件的方式為這個(gè)Service添加一個(gè)新的基于Http的Endpoint就可以了。 把Endpoint的信息寫(xiě)在config文件中的優(yōu)勢(shì)在于,修改config文件的內(nèi)容是不需要重新編譯和重新部署的。相應(yīng)的定義方式清參照以上的Sample。
下面我們來(lái)看看在host 一個(gè)Service的時(shí)候,置于配置文件的信息是如何起作用的。在上面的例子中我們通過(guò)下面一段代碼Host一個(gè)Serivice(對(duì)應(yīng)得Service Type是GeneralCalculatorService)。
using (ServiceHost calculatorSerivceHost = new ServiceHost(typeof(GeneralCalculatorService)))
{
calculatorSerivceHost.Opened += delegate
{
Console.WriteLine("Calculator Service has begun to listen
");
};
calculatorSerivceHost.Open();
Console.Read();
}
下面是Service相關(guān)的配置信息:
<services>
<service behaviorConfiguration="calculatorServieBehavior" name="Artech.WCFService.Service.GeneralCalculatorService">
<endpoint address="" binding="basicHttpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator">
</endpoint>
<endpoint address="" binding="netTcpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8888/generalcalculator" />
<add baseAddress="net.tcp://localhost:9999/generalCalculator" />
</baseAddresses>
</host>
</service>
</services>
首先我們創(chuàng)建一個(gè)ServiceHost對(duì)象calculatorSerivceHost,同時(shí)指定對(duì)用的Service Type 信息(typeof(GeneralCalculatorService))。WCF Infrastructure為在配置文件在Services Section尋找是否有相對(duì)用的service定義。在這個(gè)例子中,他會(huì)找到一個(gè)name屬性為Artech.WCFService.Service.GeneralCalculatorService的Service。然后他會(huì)根據(jù)定義在Service中的Endpoint定義為calculatorSerivceHost添加相應(yīng)的Endpoint。 如果有對(duì)應(yīng)的Endpoint Behavior設(shè)置存在于配置文件中,這些Behavior也會(huì)設(shè)置到改Endpoint中。最后調(diào)用Open方法,calculatorSerivceHost開(kāi)始監(jiān)聽(tīng)來(lái)自Client端的請(qǐng)求。
Address
每一個(gè)Endpoint都必須有一個(gè)Address,Address定位和唯一標(biāo)志一個(gè)Endpoint。在Managed code 中,Address由System.ServiceModel.EndpointAddress對(duì)象來(lái)表示。下面是一個(gè)Adress的結(jié)構(gòu):
-
URI:指定的Endpoint的Location。URI對(duì)于Endpoint是必須的。
-
Identity:當(dāng)另一個(gè)Endpoint與此Endpoint進(jìn)行消息交互時(shí),可以獲取該Identity來(lái)Authenticate正在與之進(jìn)行消息交互的Endpoint是否是它所希望的。Identity對(duì)于endpoint是可選的。
-
Headers:Address可以包含一些可選的Headers, 這些header最終會(huì)加到相應(yīng)的Soap Message的Header中。Header存放的多為Address相關(guān)的信息,用于進(jìn)行Addressing Filter。
Address的主要作用就是同過(guò)Uri為Service提供一個(gè)監(jiān)聽(tīng)Address。但在某些特殊的場(chǎng)景中,我們可以借助Address的Headers提供一些擴(kuò)展的功能。在大多數(shù)的情況下Client可以直接訪問(wèn)Service,換句話(huà)說(shuō),如果我們把Message 傳遞的路徑看成是以系列連續(xù)的節(jié)點(diǎn)(Node)的話(huà),Message直接從Client所在的節(jié)點(diǎn)(Node)傳遞到最終的Service的節(jié)點(diǎn)。但在某些情況下,考慮的實(shí)現(xiàn)負(fù)載平衡,安全驗(yàn)證等因素,我們需要在Client和最終的Service之間加入一些中間節(jié)點(diǎn)(Intermediaries),這些中間節(jié)點(diǎn)可以在Message到達(dá)最終Service Node之前作一些工作,比如為了實(shí)現(xiàn)負(fù)載平衡,它可以把Message Request分流到不同的節(jié)點(diǎn)——Routing;為了在Message達(dá)到最終Node之前,驗(yàn)證Client是否是一個(gè)合法的請(qǐng)求,他可以根據(jù)Message存儲(chǔ)的Credential的信息驗(yàn)證該請(qǐng)求——Authentication。
這些Intermediaries操作的一般不會(huì)是Message Body的內(nèi)容(大多數(shù)情況下他們已經(jīng)被加密),而是Message Header內(nèi)容。他們可以修改Header的內(nèi)容,也可以加入一些新的Header。所以為了實(shí)現(xiàn)Routing,我們需要在Message加入一些Addressing相關(guān)的內(nèi)容,為了實(shí)現(xiàn)Authentication我們需要加入Client Credential的信息, 而這些信息都放在Header中。實(shí)際上你可以把很多內(nèi)容加到Header中。
我們可以通過(guò)config文件加入這些Header:
<service behaviorConfiguration="calculatorServieBehavior" name="Artech.WCFService.Service.DuplexCalculatorService">
<endpoint binding="wsDualHttpBinding" contract="Artech.WCFService.Contract.IDuplexCalculator">
<headers>
<role>admin</role>
</headers>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:7777/DuplexCalculator" />
</baseAddresses>
</host>
</service>
<client>
<endpoint address="http://localhost/WCFService/SessionfulCalculatorService.svc"
binding="wsHttpBinding" bindingConfiguration="" contract="Artech.WCFService.Contract.ISessionfulCalculator" >
<headers>
<role>admin</role>
</headers>
</endpoint>
</client>
Binding
WCF,顧名思義就是實(shí)現(xiàn)了分布式系統(tǒng)中各Application之間的Communication的問(wèn)題。上面我們說(shuō)過(guò), Client和Service之間的通信完全有他們各自的Endpoint的擔(dān)當(dāng)。Address解決了尋址的問(wèn)題,通過(guò)Address,Client知道在哪里可以找到它所希望的Service。但是知道了地址,只是實(shí)現(xiàn)的通信的第一步。
對(duì)于一個(gè)基于SOA的分布式系統(tǒng)來(lái)說(shuō),各Application之間的通信是通過(guò)Message Exchange來(lái)實(shí)現(xiàn)的。如何實(shí)現(xiàn)在各個(gè)Application之間 進(jìn)行Message的交互,首先需要考慮的是采用怎樣的Transport,是采用Http呢,還是采用TCP或是其他,比如Named Pipe、MSMQ。其次需要考慮的是Message應(yīng)該采取怎樣的編碼,是text/XML呢,還是Binary,或是MTOM;此外,對(duì)于一個(gè)企業(yè)級(jí)的分布式應(yīng)用,Security與Robustness是我們必須考慮的問(wèn)題——我們應(yīng)該采用Transport Level的Security(SSL)還是Message Level的Security;如何確保我們的Message的傳遞是可靠的(Reliable Messaging); 如何把在各application中執(zhí)行的操作納入同一個(gè)事物中(Transaction)。而這一些都是Binding需要解決的問(wèn)題。所以我們可以說(shuō)Binding實(shí)現(xiàn)了Client和Service通信的所有底層細(xì)節(jié)。
在WCF中,Binding一個(gè)Binding Element的集合,每個(gè)Binding Element解決Communication的某一個(gè)方面。所有的Binding Element大體上可以分為以下3類(lèi):
1. Transport Binding Element:實(shí)現(xiàn)Communication的Transport選取,每個(gè)Binding必須包含一格Transport Element。
2. Encoding Binding Element:解決傳遞數(shù)據(jù)的編碼的問(wèn)題,每個(gè)Binding必須包含一個(gè)Encoding Element,一般由Transport Binding Element來(lái)提供。
3. Protocol Binding Element:解決Security,Reliable Messaging和Transaction的問(wèn)題。
下邊這個(gè)表格列出了Binding中的各個(gè)層次結(jié)構(gòu)。
|
Layer |
Options |
Required |
|
Transactions |
TransactionFlowBindingElement |
No |
|
Reliability |
ReliableSessionBindingElement |
No |
|
Security |
SecurityBindingElement |
No |
|
Encoding |
Text, Binary, MTOM, Custom |
Yes |
|
Transport |
TCP, Named Pipes, HTTP, HTTPS, MSMQ, Custom |
Yes |
WCF相關(guān)內(nèi)容:
[原創(chuàng)]我的WCF之旅(1):創(chuàng)建一個(gè)簡(jiǎn)單的WCF程序
[原創(chuàng)]我的WCF之旅(2):Endpoint Overview
[原創(chuàng)]我的WCF之旅(3):在WCF中實(shí)現(xiàn)雙向通信(Bi-directional Communication)
[原創(chuàng)]我的WCF之旅(4):WCF中的序列化(Serialization)- Part I
[原創(chuàng)]我的WCF之旅(4):WCF中的序列化(Serialization)- Part II
[原創(chuàng)]我的WCF之旅(5):Service Contract中的重載(Overloading)
[原創(chuàng)]我的WCF之旅(6):在Winform Application中調(diào)用Duplex Service出現(xiàn)TimeoutException的原因和解決方案
[原創(chuàng)]我的WCF之旅(7):面向服務(wù)架構(gòu)(SOA)和面向?qū)ο缶幊蹋∣OP)的結(jié)合——如何實(shí)現(xiàn)Service Contract的繼承
[原創(chuàng)]我的WCF之旅(8):WCF中的Session和Instancing Management
[原創(chuàng)]我的WCF之旅(9):如何在WCF中使用tcpTrace來(lái)進(jìn)行Soap Trace
[原創(chuàng)]我的WCF之旅(10): 如何在WCF進(jìn)行Exception Handling
[原創(chuàng)]我的WCF之旅(11):再談WCF的雙向通訊-基于Http的雙向通訊 V.S. 基于TCP的雙向通訊
[原創(chuàng)]我的WCF之旅(12):使用MSMQ進(jìn)行Reliable Messaging
[原創(chuàng)]我的WCF之旅(13):創(chuàng)建基于MSMQ的Responsive Service


WCF實(shí)際上是構(gòu)建了一個(gè)框架,這個(gè)框架實(shí)現(xiàn)了在互聯(lián)系統(tǒng)中各個(gè)Application之間如何通信。使得Developers和Architect在構(gòu)建分布式系統(tǒng)中,無(wú)需在考慮如何去實(shí)現(xiàn)通信相關(guān)的問(wèn)題,更加關(guān)注與系統(tǒng)的業(yè)務(wù)邏輯本身。而在WCF Infrastructure中,各個(gè)Application之間的通信是由Endpoint來(lái)實(shí)現(xiàn)的......

浙公網(wǎng)安備 33010602011771號(hào)