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

WCF異步服務(wù)正確創(chuàng)建方式詳解

開(kāi)發(fā) 開(kāi)發(fā)工具
我們今天將會(huì)在這篇文章中通過(guò)一段簡(jiǎn)單的示例,為大家詳細(xì)介紹一下有關(guān)WCF異步服務(wù)的具體實(shí)現(xiàn)方法,方便大家在學(xué)習(xí)應(yīng)用過(guò)程中獲得幫助。

WCF應(yīng)用程序中,如何才能正確的實(shí)現(xiàn)WCF異步服務(wù)這一操作技巧呢?今天我們將會(huì)在這篇文章中為大家詳細(xì)介紹一下有關(guān)這方面的具體應(yīng)用方式,希望對(duì)于又需要的朋友們可以從中獲得一些幫助。

本例子中,我們通過(guò)服務(wù)調(diào)用來(lái)讀取服務(wù)端的文件,在實(shí)現(xiàn)文件讀取操作的時(shí)候,采用異步文件讀取方式。

先來(lái)看看服務(wù)契約的定義。服務(wù)契約通過(guò)接口IFileReader定義,基于文件名的文件讀取操作以異步的方式定義在BeginRead和EndRead方法中。

  1. using System;   
  2. using System.ServiceModel;   
  3. namespace Artech.AsyncServices.Contracts   
  4. {   
  5. [ServiceContract(Namespace="http://www.artech.com/")]   
  6. public interface IFileReader   
  7. {   
  8. [OperationContract(AsyncPattern = true)]   
  9. IAsyncResult BeginRead(string fileName, AsyncCallback 
    userCallback, object stateObject);   
  10. string EndRead(IAsyncResult asynResult);   
  11. }   
  12. }  

FileReader實(shí)現(xiàn)了契約契約,在BeginRead方法中,根據(jù)文件名稱創(chuàng)建FileStream對(duì)象,調(diào)用FileStream的BeginRead方法實(shí)現(xiàn)文件的異步讀取,并直接返回該方法的執(zhí)行結(jié)果:一個(gè)IAsyncResult對(duì)象。在EndRead方法中,調(diào)用FileStream的EndRead讀取文件內(nèi)容,并關(guān)閉FileStream對(duì)象。

  1. using System;   
  2. using System.Text;   
  3. using Artech.AsyncServices.Contracts;   
  4. using System.IO;   
  5. namespace Artech.AsyncServices.Services   
  6. {   
  7. public class FileReaderService : IFileReader   
  8. {   
  9. private const string baseLocation = @"E:\";   
  10. private FileStream _stream;   
  11. private byte[] _buffer;   
  12. #region IFileReader Members   
  13. public IAsyncResult BeginRead(string fileName, AsyncCallback 
    userCallback, object stateObject)   
  14. {   
  15. this._stream = new FileStream(baseLocation + fileName, 
    FileMode.Open, FileAccess.Read, FileShare.Read);   
  16. this._buffer = new byte[this._stream.Length];   
  17. return this._stream.BeginRead(this._buffer, 0, this._buffer.Length,
     userCallback, stateObject);   
  18. }   
  19. public string EndRead(IAsyncResult ar)   
  20. {   
  21. this._stream.EndRead(ar);   
  22. this._stream.Close();   
  23. return Encoding.ASCII.GetString(this._buffer);   
  24. }   
  25. #endregion 30: }   

采用傳統(tǒng)的方式寄宿該服務(wù),并發(fā)布元數(shù)據(jù)。在客戶端通過(guò)添加服務(wù)引用的方式生成相關(guān)的服務(wù)代理代碼和配置。你將會(huì)發(fā)現(xiàn)客戶端生成的服務(wù)契約和服務(wù)代理類中,會(huì)有一個(gè)***的操作Read。也就是說(shuō),不管服務(wù)采用同步模式還是WCF異步服務(wù)實(shí)現(xiàn),對(duì)客戶端的服務(wù)調(diào)用方式?jīng)]有任何影響,客戶端可以任意選擇相應(yīng)的模式進(jìn)行服務(wù)調(diào)用。

  1. namespace Clients.ServiceReferences   
  2. {   
  3. [ServiceContractAttribute(ConfigurationName
    "ServiceReferences.IFileReader")]   
  4. public interface IFileReader   
  5. {   
  6. [OperationContractAttribute(Action = 
    " http://www.artech.com/IFileReader/Read"
    ReplyAction = " http://www.artech.com/IFileReader/
    ReadResponse"
    )]   
  7. string Read(string fileName);   
  8. }   
  9. public partial class FileReaderClient :
     ClientBase
    <IFileReader>, IFileReader   
  10. {   
  11. public string Read(string fileName)   
  12. {   
  13. return base.Channel.Read(fileName);   
  14. }   
  15. }   

直接借助于生成的服務(wù)代理類FileReaderClient,服務(wù)調(diào)用的代碼就顯得很簡(jiǎn)單了。

  1. using System;   
  2. using Clients.ServiceReferences;   
  3. namespace Clients   
  4. {   
  5. class Program   
  6. {   
  7. static void Main(string[] args)   
  8. {   
  9. using (FileReaderClient proxy = new FileReaderClient())   
  10. {   
  11. Console.WriteLine(proxy.Read("test.txt"));   
  12. }   
  13. Console.Read();   
  14. }   
  15. }   

以上就是對(duì)WCF異步服務(wù)的實(shí)現(xiàn)做的詳細(xì)介紹。

【編輯推薦】

  1. WCF異步操作具體定義與應(yīng)用
  2. WCF自定義集合類型應(yīng)用注意事項(xiàng)探討
  3. WCF會(huì)話服務(wù)基本應(yīng)用技巧分享
  4. WCF編碼規(guī)范相關(guān)知識(shí)詳解
  5. Silverlight調(diào)用WCF服務(wù)相關(guān)應(yīng)用細(xì)節(jié)解析
責(zé)任編輯:曹凱 來(lái)源: CSDN
相關(guān)推薦

2010-03-01 17:44:39

Silverlight

2010-02-25 16:52:12

引用WCF服務(wù)

2010-03-01 16:59:31

WCF異常調(diào)試

2010-02-26 09:33:18

WCF創(chuàng)建WebSer

2010-03-02 09:32:54

WCF服務(wù)消息

2010-03-01 14:08:53

WCF編碼器

2010-02-26 14:05:57

WCF通信方式

2010-02-26 10:30:03

ASP.NET Aja

2009-12-08 14:10:55

Silverlight

2009-12-21 18:32:22

關(guān)閉WCF鏈接

2010-03-01 14:01:50

WCF服務(wù)異步調(diào)用

2010-02-22 14:09:08

WCF Dispose

2010-02-26 17:44:51

WCF安全參數(shù)

2010-03-02 16:05:48

WCF端點(diǎn)配置

2010-02-24 10:07:48

WCF跨越邊界

2009-12-21 10:09:26

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

2010-02-25 09:13:34

WCF異步調(diào)用

2010-02-22 14:18:34

WCF服務(wù)驗(yàn)證

2010-02-26 11:22:16

LitwareHR使用

2010-02-24 12:41:58

WCF異常處理
點(diǎn)贊
收藏

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