WCF異步服務(wù)正確創(chuàng)建方式詳解
在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方法中。
- using System;
- using System.ServiceModel;
- namespace Artech.AsyncServices.Contracts
- {
- [ServiceContract(Namespace="http://www.artech.com/")]
- public interface IFileReader
- {
- [OperationContract(AsyncPattern = true)]
- IAsyncResult BeginRead(string fileName, AsyncCallback
userCallback, object stateObject);- string EndRead(IAsyncResult asynResult);
- }
- }
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ì)象。
- using System;
- using System.Text;
- using Artech.AsyncServices.Contracts;
- using System.IO;
- namespace Artech.AsyncServices.Services
- {
- public class FileReaderService : IFileReader
- {
- private const string baseLocation = @"E:\";
- private FileStream _stream;
- private byte[] _buffer;
- #region IFileReader Members
- public IAsyncResult BeginRead(string fileName, AsyncCallback
userCallback, object stateObject)- {
- this._stream = new FileStream(baseLocation + fileName,
FileMode.Open, FileAccess.Read, FileShare.Read);- this._buffer = new byte[this._stream.Length];
- return this._stream.BeginRead(this._buffer, 0, this._buffer.Length,
userCallback, stateObject);- }
- public string EndRead(IAsyncResult ar)
- {
- this._stream.EndRead(ar);
- this._stream.Close();
- return Encoding.ASCII.GetString(this._buffer);
- }
- #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)用。
- namespace Clients.ServiceReferences
- {
- [ServiceContractAttribute(ConfigurationName=
"ServiceReferences.IFileReader")]- public interface IFileReader
- {
- [OperationContractAttribute(Action =
" http://www.artech.com/IFileReader/Read",
ReplyAction = " http://www.artech.com/IFileReader/
ReadResponse")]- string Read(string fileName);
- }
- public partial class FileReaderClient :
ClientBase<IFileReader>, IFileReader- {
- public string Read(string fileName)
- {
- return base.Channel.Read(fileName);
- }
- }
- }
直接借助于生成的服務(wù)代理類FileReaderClient,服務(wù)調(diào)用的代碼就顯得很簡(jiǎn)單了。
- using System;
- using Clients.ServiceReferences;
- namespace Clients
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (FileReaderClient proxy = new FileReaderClient())
- {
- Console.WriteLine(proxy.Read("test.txt"));
- }
- Console.Read();
- }
- }
- }
以上就是對(duì)WCF異步服務(wù)的實(shí)現(xiàn)做的詳細(xì)介紹。
【編輯推薦】