自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF客戶(hù)端具體搭建方法解析

開(kāi)發(fā) 開(kāi)發(fā)工具
WCF客戶(hù)端的正確搭建對(duì)于初學(xué)者來(lái)說(shuō)是非常重的,他們需要熟練的掌握這一應(yīng)用技術(shù),才能方便將來(lái)的應(yīng)用,并提高我們的開(kāi)發(fā)能力。

我們?cè)谝幌盗械奈恼轮袨榇蠹以敿?xì)介紹了有關(guān)WCF的相關(guān)基礎(chǔ)內(nèi)容,相信大家應(yīng)該可以通過(guò)我們介紹的內(nèi)容能夠充分掌握這一工具的應(yīng)用方法。在這里我們繼續(xù)對(duì)WCF客戶(hù)端的相關(guān)應(yīng)用方法做一個(gè)介紹。#t#

搭建WCF客戶(hù)端,最重要就是要遵循服務(wù)端的契約,客戶(hù)端通過(guò)代理(Proxy)來(lái)訪問(wèn)服務(wù)端點(diǎn),而并不關(guān)心服務(wù)端的具體實(shí)現(xiàn)。代理要做的就是通過(guò)與服務(wù)端確認(rèn)通訊協(xié)議,并通過(guò)信道(channels)交換數(shù)據(jù)。在服務(wù)端,ServiceHost會(huì)為每個(gè)端點(diǎn)創(chuàng)建一個(gè)信道偵聽(tīng)器,由偵聽(tīng)器產(chǎn)生信道。而客戶(hù)端代理則產(chǎn)生一個(gè)信道發(fā)生器,產(chǎn)生客戶(hù)端信道。只有在服務(wù)端信道和客戶(hù)端信道一致的情況下,雙方才允許進(jìn)行通訊。信道會(huì)對(duì)通訊過(guò)程進(jìn)行監(jiān)控,保障通訊的安全性。

為了簡(jiǎn)單的完成一個(gè)WCF客戶(hù)端,微軟為我們準(zhǔn)備了一個(gè)小工具,就是Service Model Metadata Utility。這個(gè)工具能幫你快速的從服務(wù)地址中生成客戶(hù)代理和配置文件。

首先允許服務(wù)器端程序,等服務(wù)啟動(dòng)后。在VS2008命令行窗口中輸入如下命令:svcutil.exe http://localhost:8080/MyWCF 回車(chē)后得到如下頁(yè)面。

 

從上面畫(huà)面中可以看到,wcf為客戶(hù)端生成了一個(gè)客戶(hù)代理類(lèi)TemperatureService.cs和一個(gè)配置文件output.config??蛻?hù)端只需要整合這兩個(gè)文件就可以與服務(wù)端通訊了。我們來(lái)看看這兩個(gè)文件的內(nèi)容:

 

  1. TemperatureService.cs  
  2. // < auto-generated> 
  3. // 此代碼由工具生成。  
  4. // 運(yùn)行時(shí)版本:2.0.50727.3053  
  5. //  
  6. // 對(duì)此文件的更改可能會(huì)導(dǎo)致不正確的行為,并且如果  
  7. // 重新生成代碼,這些更改將會(huì)丟失。  
  8. // < /auto-generated>   
  9. [System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  10. [System.ServiceModel.ServiceContractAttribute
    (
    ConfigurationName = "IContract")]  
  11. public interface IContract  
  12. {  
  13. [System.ServiceModel.OperationContractAttribute(Action = 
    "http://tempuri.org/IContract/GetFahrenheit"ReplyAction = 
    "http://tempuri.org/IContract/GetFahrenheitResponse")]  
  14. float GetFahrenheit(float celsius);  
  15. }  
  16. [System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  17. public interface IContractChannel : IContract, System.
    ServiceModel.IClientChannel  
  18. {  
  19. }  
  20. [System.Diagnostics.DebuggerStepThroughAttribute()]  
  21. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.
    ServiceModel", "3.0.0.0")]  
  22. public partial class ContractClient : System.ServiceModel.
    ClientBase
    < IContract>, IContract  
  23. {  
  24. public ContractClient()  
  25. {  
  26. }  
  27. public ContractClient(string endpointConfigurationName) :  
  28. base(endpointConfigurationName)  
  29. {  
  30. }  
  31. public ContractClient(string endpointConfigurationName, string remoteAddress) :  
  32. base(endpointConfigurationName, remoteAddress)  
  33. {  
  34. }  
  35. public ContractClient(string endpointConfigurationName, 
    System.ServiceModel.EndpointAddress remoteAddress) :  
  36. base(endpointConfigurationName, remoteAddress)  
  37. {  
  38. }  
  39. public ContractClient(System.ServiceModel.Channels.Binding binding, 
    System.ServiceModel.EndpointAddress remoteAddress) :  
  40. base(binding, remoteAddress)  
  41. {  
  42. }  
  43. public float GetFahrenheit(float celsius)  
  44. {  
  45. return base.Channel.GetFahrenheit(celsius);  
  46. }  

從這個(gè)文件可以看到,WCF客戶(hù)端實(shí)際上是繼承了兩個(gè)接口,System.ServiceModel.ClientBase< IContract>和IContract。其中IContract是服務(wù)端契約的接口。

output.config

  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < configuration> 
  3. < system.serviceModel> 
  4. < bindings> 
  5. < basicHttpBinding> 
  6. < binding name="BasicHttpBinding_IContract" closeTimeout="00:01:00" 
  7. openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
  8. allowCookies="false" bypassProxyOnLocal="false" 
    hostNameComparisonMode="StrongWildcard" 
  9. maxBufferSize="65536" maxBufferPoolSize="524288" 
    maxReceivedMessageSize="65536" 
  10. messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
  11. useDefaultWebProxy="true"> 
  12. < readerQuotas maxDepth="32" maxStringContentLength="8192" 

    maxArrayLength="16384" 
  13. maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
  14. < security mode="None"> 
  15. < transport clientCredentialType="None" proxyCredentialType="None" 
  16. realm="" /> 
  17. < message clientCredentialType="UserName" algorithmSuite="Default" /> 
  18. < /security> 
  19. < /binding> 
  20. < /basicHttpBinding> 
  21. < /bindings> 
  22. < client> 
  23. < endpoint address="http://localhost:8080/MyWCF" 
    binding="basicHttpBinding" 
  24. bindingConfiguration="BasicHttpBinding_IContract" contract="IContract" 
  25. name="BasicHttpBinding_IContract" /> 
  26. < /client> 
  27. < /system.serviceModel> 
  28. < /configuration> 

output.config文件則定義了和服務(wù)端匹配的endpoint,有了這兩個(gè)文件,***要做的事情就是將其整合到WCF客戶(hù)端程序中,其步驟如下:

1)建立一個(gè)空白解決方案,方案的名稱(chēng)叫MyWCFClient,添加一個(gè)名稱(chēng)為MyWCF.Client的ConsoleApplication項(xiàng)目。在該項(xiàng)目中添加System.ServiceModel的引用。

2)另外在方案中再添加一個(gè)類(lèi)庫(kù)項(xiàng)目,項(xiàng)目名稱(chēng)叫MyWCF.ClientBase,為項(xiàng)目添加System.ServiceModel的引用,類(lèi)名改為ClientBase。將TemperatureService.cs文件中的代碼拷貝到ClientBase中的命名空間引用下。

3)在項(xiàng)目MyWCF.Client項(xiàng)目中添加一個(gè)App.config文件,將output.config文件的代碼粘貼到該文件中覆蓋原來(lái)的代碼。并為該項(xiàng)目添加對(duì)MyWCF.ClientBase項(xiàng)目和System.ServiceModel的引用。

4)在項(xiàng)目MyWCF.Client的Main方法中添加如下代碼。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using MyWCF.ClientBase;  
  5. namespace MyWCF.Client  
  6. {  
  7. class Program  
  8. {  
  9. static void Main(string[] args)  
  10. {  
  11. ContractClient CC = new ContractClient();  
  12. float result = CC.GetFahrenheit(23.4f);  
  13. Console.WriteLine("華氏溫度為{0}度!", result);  
  14. }  
  15. }  

5)客戶(hù)端代碼編寫(xiě)完成,此時(shí)請(qǐng)首先運(yùn)行服務(wù)端的MyWCF.Hosting項(xiàng)目,將服務(wù)端啟動(dòng)。

6)回到客戶(hù)端的MyWCF.Client項(xiàng)目,按Ctrl + F5執(zhí)行程序。

 

由此可見(jiàn),WCF客戶(hù)端由兩部分組成,一是用于同服務(wù)端確認(rèn)通訊的代理層MyWCF.ClientBase,二是客戶(hù)端的業(yè)務(wù)邏輯層MyWCF.Client。實(shí)際上,只要服務(wù)端確定后,我們就可以使用工具輕松的生成客戶(hù)端架構(gòu)。當(dāng)然,這只是WCF的一個(gè)最為簡(jiǎn)單的示例,目的是使大家對(duì)WCF的各個(gè)部件有一個(gè)大致的了解,對(duì)架構(gòu)有一個(gè)簡(jiǎn)單認(rèn)識(shí)。

責(zé)任編輯:曹凱 來(lái)源: CSDN
相關(guān)推薦

2010-02-24 16:39:27

WCF客戶(hù)端處理

2009-12-22 10:29:59

WCF客戶(hù)端處理

2009-11-05 13:00:25

WCF客戶(hù)端

2009-12-07 18:26:36

WCF客戶(hù)端

2009-12-22 18:18:11

WCF客戶(hù)端編程

2009-12-08 16:47:06

WCF IP

2009-11-25 13:21:30

PHP作為memcac

2009-12-22 18:43:00

WCF異步調(diào)用

2010-07-06 15:21:25

UDP客戶(hù)端

2010-02-23 09:58:21

WCF客戶(hù)端驗(yàn)證

2009-11-05 13:08:44

WCF客戶(hù)端配置

2009-12-21 15:53:56

WCF獲取客戶(hù)端IP

2010-02-22 11:10:17

WCF獲取客戶(hù)端IP

2009-11-09 15:49:01

WCF異步調(diào)用

2011-09-09 09:44:23

WCF

2009-12-21 10:19:05

Silverlight

2010-02-24 16:17:09

WCF獲取客戶(hù)端IP

2010-02-23 15:12:25

WCF客戶(hù)端

2009-12-21 10:09:26

WCF創(chuàng)建客戶(hù)端服務(wù)對(duì)

2015-06-03 09:27:05

JavaScript客戶(hù)端檢測(cè)技術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)