WCF 之旅——1
1.什么是WCF?
?????? 根據(jù)微軟官方的解釋,WCF(之前的版本名為“Indigo”)是使用托管代碼建立和運(yùn)行面向服務(wù)(Service Oriented)應(yīng)用程序的統(tǒng)一框架。它使得開發(fā)者能夠建立一個跨平臺的安全、可信賴、事務(wù)性的解決方案,且能與已有系統(tǒng)兼容協(xié)作。WCF是微軟分布式應(yīng)用程序開發(fā)的集大成者,它整合了.Net平臺下所有的和分布式系統(tǒng)有關(guān)的技術(shù),例如.Net Remoting、ASMX、WSE和MSMQ。
?????? WCF是微軟重點(diǎn)介紹的產(chǎn)品,因此也推出了專門的官方網(wǎng)站(http://windowscommunication.net),該網(wǎng)站有最新的WCF新聞發(fā)布,以及介紹WCF的技術(shù)文檔和樣例代碼。
2. WCF的ABC
????? 每一篇入門性質(zhì)的介紹WCF的文章,都會提到ABC:Address, Binding, Contract。
- Address: 每一個WCF的Service都有一個唯一的地址。這個地址給出了Service的地址和傳輸協(xié)議(Transport Protocol)
- Binding: 通信(Communication)的方式很多,同步的request/reply模式,非同步的fire-and-forget模式。消息可以單向或者雙向的發(fā)送接收,可以立即發(fā)送或者把它放入到某一個隊(duì)列中再處理。所供選擇的傳輸協(xié)議也有Http, Tcp,P2P, IPC等。當(dāng)要考慮Client/Server如何進(jìn)行通訊的時候,除了考慮以上提到的幾點(diǎn)之外,還有其它很多需要考慮的因素,如安全,性能等。因此,簡單來說,Binding只不過是微軟提供的一組考慮比較周全、常用的封裝好的通信方式。
- Contract:Contract描述了Service能提供的各種服務(wù)。Contract有四種,包括Service Contract, Data Contract, Fault Contract和Message Contract
3. Endpoint
???? 每一個Service都需要具備ABC三個元素,而WCF把這三者之間的關(guān)系規(guī)范化為Endpoint.
???????????????????
4. 簡單的例子
????? 每一個Service都需要有一個host。這個host的形式可以是多種多樣,可以是WinForm Application, Console Application,也可以IIS,或者是WAS(Vista),Windows Service等。
????? 首先,來定義Service的Contract.
[ServiceContract]public interface IHelloService{[OperationContract]void SayHello();[OperationContract]string SayHelloToEmployee(Employee employee);// TODO: Add your service operations here}// Use a data contract as illustrated in the sample below to add composite types to service operations[DataContract]public class Employee{private string firstName;private string lastName;[DataMember]public string FirstName{get { return firstName; }set { firstName = value; }}[DataMember]public string LastName{get { return lastName; }set { lastName = value; }}}在定義好了Service Contract和Data Contract之后,還需要定義另外兩個元素,Address和Binding。這兩者都可以通過編程和配置文件來控制。這里就用配置文件的方式了,把這些代碼寫入host程序(Console)的App.config文件中:
<configuration><system.serviceModel><behaviors><serviceBehaviors><behavior name="Default" ><serviceMetadata httpGetEnabled="true"/></behavior></serviceBehaviors></behaviors><services><service name="HelloServiceLibrary.HelloService" behaviorConfiguration="Default"><host><baseAddresses><add baseAddress="http://localhost/HelloService"/></baseAddresses></host><endpoint address="net.tcp://localhost/HelloService01" binding="netTcpBinding"bindingConfiguration="" contract="HelloServiceLibrary.IHelloService" /><endpoint address="" binding="wsHttpBinding"contract="HelloServiceLibrary.IHelloService" ></endpoint></service></services></system.serviceModel> </configuration>再通過Servicehost提供的方法Open(),就可以啟動Service了。
Client端要調(diào)用Service,都需要通過Proxy來完成,Proxy可以用VS2005或者Windows SDK提供的工具SvcUtil來完成。
?
寫的不好,請大家包涵,輕拍
不知道如何添加附件,要源代碼的請留下email
轉(zhuǎn)載于:https://www.cnblogs.com/jun1st/archive/2007/12/25/1014690.html
總結(jié)
- 上一篇: 《Essential ASP.NET 2
- 下一篇: SQL Server 2005:你应该知