WCF客戶端具體搭建方法解析
我們在一系列的文章中為大家詳細(xì)介紹了有關(guān)WCF的相關(guān)基礎(chǔ)內(nèi)容,相信大家應(yīng)該可以通過我們介紹的內(nèi)容能夠充分掌握這一工具的應(yīng)用方法。在這里我們繼續(xù)對WCF客戶端的相關(guān)應(yīng)用方法做一個介紹。#t#
搭建WCF客戶端,最重要就是要遵循服務(wù)端的契約,客戶端通過代理(Proxy)來訪問服務(wù)端點(diǎn),而并不關(guān)心服務(wù)端的具體實(shí)現(xiàn)。代理要做的就是通過與服務(wù)端確認(rèn)通訊協(xié)議,并通過信道(channels)交換數(shù)據(jù)。在服務(wù)端,ServiceHost會為每個端點(diǎn)創(chuàng)建一個信道偵聽器,由偵聽器產(chǎn)生信道。而客戶端代理則產(chǎn)生一個信道發(fā)生器,產(chǎn)生客戶端信道。只有在服務(wù)端信道和客戶端信道一致的情況下,雙方才允許進(jìn)行通訊。信道會對通訊過程進(jìn)行監(jiān)控,保障通訊的安全性。
為了簡單的完成一個WCF客戶端,微軟為我們準(zhǔn)備了一個小工具,就是Service Model Metadata Utility。這個工具能幫你快速的從服務(wù)地址中生成客戶代理和配置文件。
首先允許服務(wù)器端程序,等服務(wù)啟動后。在VS2008命令行窗口中輸入如下命令:svcutil.exe http://localhost:8080/MyWCF 回車后得到如下頁面。
從上面畫面中可以看到,wcf為客戶端生成了一個客戶代理類TemperatureService.cs和一個配置文件output.config。客戶端只需要整合這兩個文件就可以與服務(wù)端通訊了。我們來看看這兩個文件的內(nèi)容:
- TemperatureService.cs
 - // < auto-generated>
 - // 此代碼由工具生成。
 - // 運(yùn)行時版本:2.0.50727.3053
 - //
 - // 對此文件的更改可能會導(dǎo)致不正確的行為,并且如果
 - // 重新生成代碼,這些更改將會丟失。
 - // < /auto-generated>
 - [System.CodeDom.Compiler.GeneratedCodeAttribute
 
("System.ServiceModel", "3.0.0.0")]- [System.ServiceModel.ServiceContractAttribute
 
(ConfigurationName = "IContract")]- public interface IContract
 - {
 - [System.ServiceModel.OperationContractAttribute(Action =
 
"http://tempuri.org/IContract/GetFahrenheit", ReplyAction =
"http://tempuri.org/IContract/GetFahrenheitResponse")]- float GetFahrenheit(float celsius);
 - }
 - [System.CodeDom.Compiler.GeneratedCodeAttribute
 
("System.ServiceModel", "3.0.0.0")]- public interface IContractChannel : IContract, System.
 
ServiceModel.IClientChannel- {
 - }
 - [System.Diagnostics.DebuggerStepThroughAttribute()]
 - [System.CodeDom.Compiler.GeneratedCodeAttribute("System.
 
ServiceModel", "3.0.0.0")]- public partial class ContractClient : System.ServiceModel.
 
ClientBase< IContract>, IContract- {
 - public ContractClient()
 - {
 - }
 - public ContractClient(string endpointConfigurationName) :
 - base(endpointConfigurationName)
 - {
 - }
 - public ContractClient(string endpointConfigurationName, string remoteAddress) :
 - base(endpointConfigurationName, remoteAddress)
 - {
 - }
 - public ContractClient(string endpointConfigurationName,
 
System.ServiceModel.EndpointAddress remoteAddress) :- base(endpointConfigurationName, remoteAddress)
 - {
 - }
 - public ContractClient(System.ServiceModel.Channels.Binding binding,
 
System.ServiceModel.EndpointAddress remoteAddress) :- base(binding, remoteAddress)
 - {
 - }
 - public float GetFahrenheit(float celsius)
 - {
 - return base.Channel.GetFahrenheit(celsius);
 - }
 - }
 
從這個文件可以看到,WCF客戶端實(shí)際上是繼承了兩個接口,System.ServiceModel.ClientBase< IContract>和IContract。其中IContract是服務(wù)端契約的接口。
output.config
- < ?xml version="1.0" encoding="utf-8"?>
 - < configuration>
 - < system.serviceModel>
 - < bindings>
 - < basicHttpBinding>
 - < binding name="BasicHttpBinding_IContract" closeTimeout="00:01:00"
 - openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
 - allowCookies="false" bypassProxyOnLocal="false"
 
hostNameComparisonMode="StrongWildcard"- maxBufferSize="65536" maxBufferPoolSize="524288"
 
maxReceivedMessageSize="65536"- messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
 - useDefaultWebProxy="true">
 - < readerQuotas maxDepth="32" maxStringContentLength="8192"
 
maxArrayLength="16384"- maxBytesPerRead="4096" maxNameTableCharCount="16384" />
 - < security mode="None">
 - < transport clientCredentialType="None" proxyCredentialType="None"
 - realm="" />
 - < message clientCredentialType="UserName" algorithmSuite="Default" />
 - < /security>
 - < /binding>
 - < /basicHttpBinding>
 - < /bindings>
 - < client>
 - < endpoint address="http://localhost:8080/MyWCF"
 
binding="basicHttpBinding"- bindingConfiguration="BasicHttpBinding_IContract" contract="IContract"
 - name="BasicHttpBinding_IContract" />
 - < /client>
 - < /system.serviceModel>
 - < /configuration>
 
output.config文件則定義了和服務(wù)端匹配的endpoint,有了這兩個文件,***要做的事情就是將其整合到WCF客戶端程序中,其步驟如下:
1)建立一個空白解決方案,方案的名稱叫MyWCFClient,添加一個名稱為MyWCF.Client的ConsoleApplication項(xiàng)目。在該項(xiàng)目中添加System.ServiceModel的引用。
2)另外在方案中再添加一個類庫項(xiàng)目,項(xiàng)目名稱叫MyWCF.ClientBase,為項(xiàng)目添加System.ServiceModel的引用,類名改為ClientBase。將TemperatureService.cs文件中的代碼拷貝到ClientBase中的命名空間引用下。
3)在項(xiàng)目MyWCF.Client項(xiàng)目中添加一個App.config文件,將output.config文件的代碼粘貼到該文件中覆蓋原來的代碼。并為該項(xiàng)目添加對MyWCF.ClientBase項(xiàng)目和System.ServiceModel的引用。
4)在項(xiàng)目MyWCF.Client的Main方法中添加如下代碼。
- using System;
 - using System.Collections.Generic;
 - using System.Text;
 - using MyWCF.ClientBase;
 - namespace MyWCF.Client
 - {
 - class Program
 - {
 - static void Main(string[] args)
 - {
 - ContractClient CC = new ContractClient();
 - float result = CC.GetFahrenheit(23.4f);
 - Console.WriteLine("華氏溫度為{0}度!", result);
 - }
 - }
 - }
 
5)客戶端代碼編寫完成,此時請首先運(yùn)行服務(wù)端的MyWCF.Hosting項(xiàng)目,將服務(wù)端啟動。
6)回到客戶端的MyWCF.Client項(xiàng)目,按Ctrl + F5執(zhí)行程序。
由此可見,WCF客戶端由兩部分組成,一是用于同服務(wù)端確認(rèn)通訊的代理層MyWCF.ClientBase,二是客戶端的業(yè)務(wù)邏輯層MyWCF.Client。實(shí)際上,只要服務(wù)端確定后,我們就可以使用工具輕松的生成客戶端架構(gòu)。當(dāng)然,這只是WCF的一個最為簡單的示例,目的是使大家對WCF的各個部件有一個大致的了解,對架構(gòu)有一個簡單認(rèn)識。















 
 
 
 
 
 
 