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

簡單實(shí)現(xiàn)WCF基礎(chǔ)開發(fā)

開發(fā) 開發(fā)工具
我們在這里為大家總結(jié)的WCF基礎(chǔ)開發(fā)的實(shí)現(xiàn)步驟可以分為六步,分別為:定義WCF服務(wù)契約;實(shí)現(xiàn)WCF服務(wù)契約;啟動WCF服務(wù);創(chuàng)建一個基本的WCF客服端;WCF客服端基本配置;使用WCF客戶端等。

WCF是微軟的一個通信編程框架。很多人可能對于這樣的一款新框架了解的還不是很全面。開發(fā)人員可以通過這款框架實(shí)現(xiàn)跨平臺可靠的互聯(lián)網(wǎng)解決方案。#t#

在這里我就用一個據(jù)于一個簡單的場景:服務(wù)端為客服端提供獲取客戶信息的一個接口讀取客戶信息,來完成WCF基礎(chǔ)開發(fā)的六個步驟。

WCF基礎(chǔ)開發(fā)1. 定義WCF服務(wù)契約

A. 項(xiàng)目引用節(jié)點(diǎn)右鍵添加System.ServiceModel引用。

B. 在代碼文件里,添加以下命名空間的引用

using System.ServiceModel;

using System;

C. 新建一個命為ICustomerService 接口,并添加一個獲取客戶信息的方法定義名為CustomerInfomation,返回字符串類型的客戶信息。

D. 為接口ICustomerService添加ServiceContract的屬性修飾使它成為WCF服務(wù)中公開的接口。

E. 為方法CustomerInfomation添加OperationContract的屬性修飾使它成為WCF服務(wù)公開接口中公開的成員。

F. 代碼:

  1. using System;  
  2. using System.ServiceModel;  
  3. namespace ConWCF  
  4. { [ServiceContract(Namespace =
     
    "http://Microsoft.Service
    Model.Samples"
    )]  
  5. public interface CustomerService  
  6. {    
  7. [OperationContract]  
  8. String CustomerInformation();  
  9. }   

WCF基礎(chǔ)開發(fā)2. 實(shí)現(xiàn)WCF服務(wù)契約

實(shí)現(xiàn)WCF服務(wù)契約很簡單,就是實(shí)現(xiàn)上一步聚定義的WCF服務(wù)契約定義的接口就可以。下面看代碼

  1. using System;  
  2. using System.ServiceModel;  
  3. namespace ConWCF  
  4. { [ServiceContract(Namespace = 
    "http://Microsoft.ServiceModel.Samples")]  
  5. public interface ICustomerService  
  6. {    
  7. [OperationContract]  
  8. String CustomerInformation();  
  9. }  
  10. public class CustomerService:
    ICustomerService   
  11. {   
  12. #region ICustomerService 成員  
  13. public string CustomerInformation()  
  14. {  
  15. return "這是客戶的信息!";  
  16. }  
  17. #endregion  
  18. }  

WCF基礎(chǔ)開發(fā)3. 啟動WCF服務(wù)

A.添加一個應(yīng)用程序配置文件,文件件名為App.config。

B.配置WCF服務(wù)的基本地址,如下所示

  1. < host> 
  2. < baseAddresses> 
  3. < addbaseAddressaddbaseAddress=
    "http://localhost:8000/conwcfr"/> 
  4. < /baseAddresses> 
  5. < /host> 

C.配置WCF服務(wù)的端口。Address=“”,意思就是使用上面配置的基本地址,當(dāng)然也可以在這里指定。Bingding=“wsHttpBinding”,意思是WCF服務(wù)使用的是HTTP協(xié)議。再接下來就是配置WCF服務(wù)契約了(命名空間.服務(wù)契約接口名),如下所示:

  1. < endpointaddressendpointaddress="" 
  2. binding="wsHttpBinding" 
  3. contract="ConWCF.ICustomerService" /> 

D.配置文件

E.啟動服服就簡單了

  1. ServiceHost host = new 
    ServiceHost(typeof(CustomerService));  
  2. host.Open();  
  3. Console.WriteLine("客戶信息服務(wù)已啟動");  
  4. Console.WriteLine("按任意鍵結(jié)束服務(wù)!");  
  5. Console.Read();  
  6. host.Close(); 

F.當(dāng)服務(wù)啟動時(shí),在IE欄中輸入: http://localhost:8000/conwcfr,將會收到一些幫助的提示信息。

G.異常:配置文件中的服務(wù)名稱一定是:命名空間.實(shí)現(xiàn)WCF服務(wù)契約類的名稱,否則將會發(fā)生找到不配置的異常。

  1. < service name=
    "ConWCF.CustomerService" 

異常信息: Service 'ConWCF.CustomerService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

這個異常搞得我昏了半天,害得我以為從IIS、端口到配置環(huán)境排除錯誤,就是搞不明白為什么會跟類的命稱聯(lián)系起來。不過,最終也解決了。

WCF基礎(chǔ)開發(fā)4. 創(chuàng)建一個基本的WCF客服端

WCF服務(wù)端創(chuàng)建好啊,創(chuàng)建客戶端就容易多了,直接用SVCUTIL 命令行工具去完成代碼的生成。我安裝了WINDOWS SDK,其帶了一個CMDShell 命令行工具,打開后就可以運(yùn)行SVCUTIL命令,這個命令是運(yùn)行于 framework 3.0以上環(huán)境。查看詳細(xì)幫助信息可以輸入:svcutil /?,回車。

1. 啟動上幾步驟創(chuàng)建好的WCF服務(wù)端。

2. 在CMDShell工具中用CD 轉(zhuǎn)到你要存放客戶端代碼的目錄下,輸入以下命令生成代碼和配置文件。

D:"client>svcutil /language:c# /out:CustomerClient.cs /config:app.config http:/

/localhost:8000/conwcfr

上面命令指定了要生成代碼的語言,代碼文件和配置文件名,WCF服務(wù)端地址,注意運(yùn)行命令時(shí)必須確定WCF服務(wù)端正在運(yùn)行中。

WCF基礎(chǔ)開發(fā)5. WCF客服端基本配置

WCF客戶端配置就是配置調(diào)用WCF服務(wù)端的協(xié)議,輸傳寬帶,服務(wù)地址,安全等等信息。下面就上一步驟命令自動生成的配置文件。

  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < configuration> 
  3. < system.serviceModel> 
  4. < bindings> 
  5. < wsHttpBinding> 
  6. < binding name="WSHttpBinding_
    ICustomerService"
     closeTimeout="00:01:00" 
  7. openTimeout="00:01:00" receiveTimeout=
    "00:10:00" sendTimeout="00:01:00" 
  8. bypassProxyOnLocal="false" 
    transactionFlow="false" hostName
    ComparisonMode
    ="StrongWildcard" 
  9. maxBufferPoolSize="524288" maxRece
    ivedMessageSize
    ="65536" 
  10. messageEncoding="Text" textEncoding
    ="utf-8" useDefaultWebProxy="true" 
  11. allowCookies="false"> 
  12. < readerQuotas maxDepth="32" maxStr
    ingContentLength
    ="8192" 
    maxArrayLength="16384" 
  13. maxBytesPerRead="4096" maxNameTab
    leCharCount
    ="16384" /> 
  14. < reliableSession ordered="true" 
    inactivityTimeout="00:10:00" 
  15. enabled="false" /> 
  16. < security mode="Message"> 
  17. < transport clientCredentialType=
    "Windows" proxyCredentialType="None" 
  18. realm="" /> 
  19. < message clientCredentialType=
    "Windows" negotiateServiceCredential="true" 
  20. algorithmSuite="Default" 
    establishSecurityContext="true" /> 
  21. < /security> 
  22. < /binding> 
  23. < /wsHttpBinding> 
  24. < /bindings> 
  25. < client> 
  26. < endpoint address="http:
    //localhost:8000/conwcfr"
     
    binding="wsHttpBinding" 
  27. bindingConfiguration="WSHttpBinding
    _ICustomerService"
     contract="ICustomerService" 
  28. name="WSHttpBinding_ICustomerService"> 
  29. < identity> 
  30. < userPrincipalName value=
    "30DA1D0B1D1E4D2\Administrator" /> 
  31. < /identity> 
  32. < /endpoint> 
  33. < /client> 
  34. < /system.serviceModel> 
  35. < /configuration> 

WCF基礎(chǔ)開發(fā)6. 使用WCF客戶端

在客戶端項(xiàng)目中項(xiàng)目引用節(jié)點(diǎn)右鍵添加System.ServiceModel引用.
添加第四部中創(chuàng)建的客戶端代碼文件和配置文件。
客戶端調(diào)用服務(wù)端的服務(wù),只要創(chuàng)建生成客戶端類的實(shí)例就可調(diào)用了,但要確認(rèn)服務(wù)端正在起用狀態(tài),如下

  1. using System;  
  2. namespace ConWCFCustomerClient  
  3. {  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {   
  8. CustomerServiceClient client = 
    new CustomerServiceClient();  
  9. string message=client.
    CustomerInformation();  
  10. Console.WriteLine(message);  
  11. Console.Read();  
  12. }  
  13. }  

 

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-11-06 09:22:46

WCF應(yīng)用

2009-11-06 16:48:03

WCF簡介

2010-02-24 15:20:23

WCF Message

2010-02-22 13:56:35

WCF服務(wù)契約

2012-04-20 10:05:16

WCF

2010-02-25 13:35:27

WCF tcpTrac

2010-02-24 12:49:39

WCF枚舉

2009-11-05 09:51:14

WCF基礎(chǔ)

2010-03-02 16:22:31

WCF狀態(tài)應(yīng)用

2011-06-28 10:20:19

Ubuntu Qt Designer

2009-12-08 15:06:33

WCF傳輸DataSe

2009-12-21 17:48:30

WCF方法重載

2010-03-01 13:17:46

WCF單向服務(wù)

2010-02-26 14:19:03

WCF用戶驗(yàn)證

2011-10-27 16:24:48

API

2010-02-22 14:28:35

WCF實(shí)現(xiàn)loadin

2009-11-09 14:02:31

WCF傳輸數(shù)據(jù)

2010-02-22 16:43:09

WCF負(fù)載平衡

2011-05-16 09:30:30

jQueryWCF

2009-11-05 08:46:10

WCF與ExtJs
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 亚洲精品视频免费 | 欧美激情免费在线 | 久久久精品一区二区三区 | 国产一级特黄视频 | 久久精品99 | 国产精品毛片在线 | 日韩亚洲一区二区 | 国产精品一区二区三区久久 | 国产精品一区在线 | 国产精品大片在线观看 | 亚洲综合婷婷 | 国产在线一区二 | 精品国产一区二区国模嫣然 | 久久在线 | 华丽的挑战在线观看 | 日韩欧美视频 | 国产亚洲精品久久久优势 | 91资源在线 | 午夜私人影院在线观看 | 国产视频中文字幕 | 国产精品高潮呻吟久久av黑人 | 亚洲欧洲精品在线 | 亚洲一区在线日韩在线深爱 | 亚洲三区在线 | 亚洲一区二区在线视频 | 久久小视频 | 国产做a爱免费视频 | 国产精品久久久久久久久污网站 | 狠狠色综合欧美激情 | 午夜天堂 | 在线播放国产一区二区三区 | 超级乱淫av片免费播放 | 蜜桃传媒一区二区 | 成人a在线 | 日韩在线一区二区三区 | 国产成人免费视频 | 天天天堂 | 天天插天天操 | 亚洲国产高清在线观看 | 久久国产精品72免费观看 | 手机av网 |