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

六步驟完成WCF開發

開發 開發工具
我們今天在這里為大家分了六個步驟詳細介紹了有關WCF開發的一些方法,希望對這方面有需求的朋友們能夠從這篇文章中獲得一些幫助。

WCF是一個由微軟公司開發的一個.NET Framework 3.5的重要組成部分。大家在學習應用的過程中應該對這一工具有一個詳細的認識。首先,在這里我就用一個據于一個簡單的場景:服務端為客服端提供獲取客戶信息的一個接口讀取客戶信息,來完成WCF開發入門的六個步驟。#t#

WCF開發之1. 定義WCF服務契約

A. 項目引用節點右鍵添加System.ServiceModel引用。

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

using System.ServiceModel;

using System;

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

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

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

F. 代碼:

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

 

 

WCF開發之2. 實現WCF服務契約
 

實現WCF服務契約很簡單,就是實現上一步聚定義的WCF服務契約定義的接口就可以。下面看代碼

  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開發之3. 啟動WCF服務

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

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

 

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

 

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

 

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

 

D.配置文件

E.啟動服服就簡單了

 

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

 

F.當服務啟動時,在IE欄中輸入: http://localhost:8000/conwcfr,將會收到一些幫助的提示信息。

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

 

  1. < service 
  2. 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、端口到配置環境排除錯誤,就是搞不明白為什么會跟類的命稱聯系起來。不過,最終也解決了。

#p#

WCF開發之4. 創建一個基本的WCF客服端

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

1. 啟動上幾步驟創建好的WCF服務端。

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

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

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

WCF開發之5. WCF客服端基本配置

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

  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" allowCookies="false"> 
  11. < readerQuotas maxDepth="32" maxStringContentLength="8192" 
    maxArrayLength="16384" 
  12. maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
  13. < reliableSession ordered="true" 
    inactivityTimeout="00:10:00"enabled="false" /> 
  14. < security mode="Message"> 
  15. < transport clientCredentialType="Windows"proxyCredentialType=
    "None"
    realm="" /> 
  16. < message clientCredentialType="Windows"
    negotiateServiceCredential="true"algorithmSuite=
    "Default"
    establishSecurityContext="true" /> 
  17. < /security> 
  18. < /binding> 
  19. < /wsHttpBinding> 
  20. < /bindings> 
  21. < client> 
  22. < endpoint address="http://localhost:8000/conwcfr"binding=
    "wsHttpBinding"
    bindingConfiguration="WSHttpBinding_ICustomerService" 
    contract="ICustomerService"name="WSHttpBinding_ICustomerService"> 
  23. < identity> 
  24. < userPrincipalName value="30DA1D0B1D1E4D\Administrator" /> 
  25. < /identity> 
  26. < /endpoint> 
  27. < /client> 
  28. < /system.serviceModel> 
  29. < /configuration> 

 

 

WCF開發之6. 使用WCF客戶端

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

  1. 1using 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. }  

以上就是對WCF開發的相關步驟介紹。

責任編輯:曹凱 來源: 博客園
相關推薦

2010-02-23 15:12:25

WCF客戶端

2010-09-25 16:32:13

企業風險管理安全管理

2010-02-23 14:03:53

WCF契約回調

2012-05-04 10:07:56

2010-09-24 15:50:03

2021-11-15 23:44:56

網絡安全零信任隱私

2010-02-26 16:16:15

2012-03-29 09:50:17

2009-12-21 10:31:04

2009-09-09 09:46:00

MyEclipse配置

2010-07-09 12:08:36

設置SNMP協議

2013-08-23 09:30:56

BYOD方案BYODMDM

2010-11-19 10:18:11

網絡連接故障

2011-07-30 13:28:03

2009-10-27 17:40:35

Oracle表空間狀態

2010-06-29 19:23:20

UML活動圖

2011-03-03 10:55:07

2009-12-11 13:31:31

策略路由配置

2009-04-10 00:25:53

上網行為管理安全策略企業安全

2010-09-13 10:39:43

CSSCSS文件
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品一区二区在线免费观看 | 日韩欧美在线播放 | 欧美三级成人理伦 | 国产精品九九视频 | 91玖玖| 国产在线视频一区二区 | 国产精品99久久久久久动医院 | 中文字幕亚洲无线 | av国产精品 | 成人国产免费视频 | 中文字幕一区二区在线观看 | www操操| 国产午夜在线 | 日本黄视频在线观看 | 激情的网站 | 自拍 亚洲 欧美 老师 丝袜 | 欧美一区二区精品 | 精品中文字幕一区二区 | 国产激情片在线观看 | 国产综合久久 | 精品欧美一区二区精品久久 | 日本亚洲精品成人欧美一区 | 99久久影院 | 91大神在线看 | 国产一区二区三区高清 | 国产免费一区二区三区 | 日本在线一区二区三区 | 国产精品乱码一区二区三区 | 国产综合精品一区二区三区 | 国产精品久久久久久吹潮 | 亚洲成人99 | 亚洲日本欧美日韩高观看 | 日韩精品在线看 | www.4hu影院| 黄色在线免费观看视频 | 激情伊人网 | 欧美日韩精品 | 成人一区二区三区在线 | 91麻豆精品国产91久久久更新资源速度超快 | 日本aaaa| 日日日日操 |