日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
六步驟打造WCF客戶端

WCF應(yīng)用方法比較靈活,它可以幫助開發(fā)人員輕松打造一個跨平臺的可依賴性的解決方案。那么如何才能正確的使用WCF開發(fā)呢?在這里我們將會為大家詳細介紹一下WCF客戶端的一些創(chuàng)建步驟。#t#

創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、固鎮(zhèn)網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、商城網(wǎng)站開發(fā)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為固鎮(zhèn)等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

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

WCF客戶端創(chuàng)建步驟1. 定義WCF服務(wù)契約

A. 項目引用節(jié)點右鍵添加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.ServiceModel.Samples")]
  5. public interface CustomerService
  6. [OperationContract]
  7. String CustomerInformation();
  8. }
  9. }

WCF客戶端創(chuàng)建步驟2. 實現(xiàn)WCF服務(wù)契約

實現(xiàn)WCF服務(wù)契約很簡單,就是實現(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. [OperationContract]
  7. String CustomerInformation();
  8. }
  9. public class CustomerService:ICustomerService
  10. {
  11. #region ICustomerService 成員
  12.  public string CustomerInformation()
  13. {
  14. return "這是客戶的信息!";
  15. }
  16. #endregion
  17. }
  18. }

WCF客戶端創(chuàng)建步驟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=“”,意思就是使用上面配置的基本地址,當然也可以在這里指定。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.當服務(wù)啟動時,在IE欄中輸入: http://localhost:8000/conwcfr,將會收到一些幫助的提示信息。

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

WCF客戶端創(chuàng)建步驟4. 創(chuàng)建一個基本的WCF客服端

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

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

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

WCF客戶端創(chuàng)建步驟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" 
    hostNameComparisonMode="StrongWildcard"
  9. maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
  10. messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
  11. allowCookies="false">
  12. < readerQuotas maxDepth="32" maxStringContentLength="8192" 
    maxArrayLength="16384"
  13. maxBytesPerRead="4096" maxNameTableCharCount="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客戶端創(chuàng)建步驟6. 使用WCF客戶端

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

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

標題名稱:六步驟打造WCF客戶端
網(wǎng)頁鏈接:http://www.5511xx.com/article/dphhgei.html