成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

WCF客戶端具體搭建方法解析

開發 開發工具
WCF客戶端的正確搭建對于初學者來說是非常重的,他們需要熟練的掌握這一應用技術,才能方便將來的應用,并提高我們的開發能力。

我們在一系列的文章中為大家詳細介紹了有關WCF的相關基礎內容,相信大家應該可以通過我們介紹的內容能夠充分掌握這一工具的應用方法。在這里我們繼續對WCF客戶端的相關應用方法做一個介紹。#t#

搭建WCF客戶端,最重要就是要遵循服務端的契約,客戶端通過代理(Proxy)來訪問服務端點,而并不關心服務端的具體實現。代理要做的就是通過與服務端確認通訊協議,并通過信道(channels)交換數據。在服務端,ServiceHost會為每個端點創建一個信道偵聽器,由偵聽器產生信道。而客戶端代理則產生一個信道發生器,產生客戶端信道。只有在服務端信道和客戶端信道一致的情況下,雙方才允許進行通訊。信道會對通訊過程進行監控,保障通訊的安全性。

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

首先允許服務器端程序,等服務啟動后。在VS2008命令行窗口中輸入如下命令:svcutil.exe http://localhost:8080/MyWCF 回車后得到如下頁面。

 

從上面畫面中可以看到,wcf為客戶端生成了一個客戶代理類TemperatureService.cs和一個配置文件output.config。客戶端只需要整合這兩個文件就可以與服務端通訊了。我們來看看這兩個文件的內容:

 

  1. TemperatureService.cs  
  2. // < auto-generated> 
  3. // 此代碼由工具生成。  
  4. // 運行時版本:2.0.50727.3053  
  5. //  
  6. // 對此文件的更改可能會導致不正確的行為,并且如果  
  7. // 重新生成代碼,這些更改將會丟失。  
  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. }  

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

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文件則定義了和服務端匹配的endpoint,有了這兩個文件,***要做的事情就是將其整合到WCF客戶端程序中,其步驟如下:

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

2)另外在方案中再添加一個類庫項目,項目名稱叫MyWCF.ClientBase,為項目添加System.ServiceModel的引用,類名改為ClientBase。將TemperatureService.cs文件中的代碼拷貝到ClientBase中的命名空間引用下。

3)在項目MyWCF.Client項目中添加一個App.config文件,將output.config文件的代碼粘貼到該文件中覆蓋原來的代碼。并為該項目添加對MyWCF.ClientBase項目和System.ServiceModel的引用。

4)在項目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)客戶端代碼編寫完成,此時請首先運行服務端的MyWCF.Hosting項目,將服務端啟動。

6)回到客戶端的MyWCF.Client項目,按Ctrl + F5執行程序。

 

由此可見,WCF客戶端由兩部分組成,一是用于同服務端確認通訊的代理層MyWCF.ClientBase,二是客戶端的業務邏輯層MyWCF.Client。實際上,只要服務端確定后,我們就可以使用工具輕松的生成客戶端架構。當然,這只是WCF的一個最為簡單的示例,目的是使大家對WCF的各個部件有一個大致的了解,對架構有一個簡單認識。

責任編輯:曹凱 來源: CSDN
相關推薦

2010-02-24 16:39:27

WCF客戶端處理

2009-12-22 10:29:59

WCF客戶端處理

2009-11-05 13:00:25

WCF客戶端

2009-12-07 18:26:36

WCF客戶端

2009-12-22 18:18:11

WCF客戶端編程

2009-12-08 16:47:06

WCF IP

2009-12-22 18:43:00

WCF異步調用

2009-11-25 13:21:30

PHP作為memcac

2009-11-09 15:49:01

WCF異步調用

2010-02-22 11:10:17

WCF獲取客戶端IP

2009-11-05 13:08:44

WCF客戶端配置

2009-12-21 15:53:56

WCF獲取客戶端IP

2011-09-09 09:44:23

WCF

2010-02-23 09:58:21

WCF客戶端驗證

2010-07-06 15:21:25

UDP客戶端

2010-02-23 15:12:25

WCF客戶端

2009-12-21 10:19:05

Silverlight

2010-02-24 16:17:09

WCF獲取客戶端IP

2009-12-21 10:09:26

WCF創建客戶端服務對

2015-06-03 09:27:05

JavaScript客戶端檢測技術
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美一级特黄aaa大片在线观看 | 国产成人精品一区二区三 | 99久久免费精品国产男女高不卡 | 一区二区日韩 | 国产一区二区三区 | 91视频三区| 天天躁日日躁狠狠躁2018小说 | 免费国产一区二区视频 | 在线看片网站 | 在线观看黄色大片 | 国产一区精品 | 久久国产精品视频 | 成人在线免费观看视频 | 天堂素人约啪 | 欧美a在线 | 欧美激情视频一区二区三区在线播放 | 国产一区久久 | 羞羞网站免费观看 | av网站免费在线观看 | 最新高清无码专区 | 亚洲国产aⅴ成人精品无吗 综合国产在线 | 在线免费观看欧美 | 一区二区在线观看免费视频 | 在线中文视频 | 色婷婷久久久亚洲一区二区三区 | 日日夜夜免费精品 | 日韩字幕一区 | 91免费小视频 | www国产亚洲精品久久网站 | 国产精品久久久久久久一区二区 | 亚洲精品一区二区三区丝袜 | 久久久久免费精品国产 | 欧美日一区二区 | 精品视频在线播放 | 亚洲精品乱码久久久久久按摩观 | 午夜免费观看网站 | 久久精品国产免费高清 | 日本一区视频在线观看 | 欧美色欧美亚洲另类七区 | 免费一区二区三区 | 懂色tv |