日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WCF傳送二進(jìn)制流數(shù)據(jù)基本實(shí)現(xiàn)步驟詳解

我們知道,在實(shí)現(xiàn)WCF傳送二進(jìn)制流數(shù)據(jù)這一操作過程中,會(huì)有一些限制因素。我們?cè)趯?shí)際應(yīng)用中要特別注意這一點(diǎn)。今天我們就會(huì)針對(duì)這方面的問題做一個(gè)詳細(xì)的介紹,希望對(duì)大家有所幫助。#t#

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、網(wǎng)站建設(shè)與策劃設(shè)計(jì),盧龍網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:盧龍等地區(qū)。盧龍做網(wǎng)站價(jià)格咨詢:18980820575

只有 BasicHttpBinding、WebHttpBinding、NetTcpBinding 和 NetNamedPipeBinding 支持傳送流數(shù)據(jù)。
流數(shù)據(jù)類型必須是可序列化的 Stream 或 MemoryStream。
傳遞時(shí)消息體(Message Body)中不能包含其他數(shù)據(jù)。

我們先看看下面的WCF傳送二進(jìn)制流數(shù)據(jù)例子。

注意將 Binding.TransferMode 設(shè)置為 TransferMode.Streamed,我們還可以修改 Binding.MaxReceivedMessageSize 來調(diào)整消息大小(默認(rèn)是64KB)。

 
 
 
  1. [ServiceContract]  
  2. public interface IFileService  
  3. {  
  4. [OperationContract]  
  5. void Upload(Stream stream);  
  6. }  
  7. public class FileService : IFileService, IDisposable  
  8. {  
  9. public void Upload(Stream stream)  
  10. {  
  11. FileStream file = new FileStream("test.dll", FileMode.Create);  
  12. try  
  13. {  
  14. BinaryWriter writer = new BinaryWriter(file);  
  15. BinaryReader reader = new BinaryReader(stream);  
  16. byte[] buffer;  
  17. do  
  18. {  
  19. buffer = reader.ReadBytes(1024);  
  20. writer.Write(buffer);  
  21. }  
  22. while (buffer.Length > 0);  
  23. }  
  24. finally  
  25. {  
  26. file.Close();  
  27. stream.Close();  
  28. }  
  29. }  
  30. public void Dispose()  
  31. {  
  32. Console.WriteLine("Dispose...");  
  33. }  
  34. }  
  35. public class WcfTest  
  36. {  
  37. public static void Test()  
  38. {  
  39. AppDomain.CreateDomain("Server").DoCallBack(delegate  
  40. {  
  41. ServiceHost host = new ServiceHost(typeof(FileService),   
  42. new Uri("http://localhost:8080/FileService"));  
  43. BasicHttpBinding binding = new BasicHttpBinding();  
  44. binding.TransferMode = TransferMode.Streamed;  
  45. host.AddServiceEndpoint(typeof(IFileService), binding, "");  
  46. host.Open();  
  47. });  
  48. BasicHttpBinding binding2 = new BasicHttpBinding();  
  49. binding2.TransferMode = TransferMode.Streamed;  
  50. IFileService channel = ChannelFactory.
    CreateChannel(binding2,   
  51. new EndpointAddress("http://localhost:8080/FileService"));  
  52. using (channel as IDisposable)  
  53. {  
  54. FileStream stream = new FileStream("MyLibrary2.dll", FileMode.Open);  
  55. channel.Test(stream);  
  56. stream.Close();  
  57. }  
  58. }  

 

一切正常。那么 "傳遞時(shí)消息體(Memory Body)中不能包含其他數(shù)據(jù)" 是什么意思?我們修改一下上面的契約,除了傳遞文件流外,我們還希望傳遞文件名。

 
 
 
  1. [ServiceContract]  
  2. public interface IFileService  
  3. {  
  4. [OperationContract]  
  5. void Upload(string filename, Stream stream);  
  6. }  
  7. // ... 其他代碼暫略 ... 

 

當(dāng)你修改完WCF傳送二進(jìn)制流數(shù)據(jù)的代碼后,運(yùn)行時(shí)你發(fā)現(xiàn)觸發(fā)了一個(gè) InvalidOperationException 異常。

未處理 System.InvalidOperationException
Message="For request in operation Upload to be a stream the operation must have a single parameter whose type is Stream."
Source="System.ServiceModel"

那么該怎么辦呢?DataContract 肯定不行。 沒錯(cuò)!你應(yīng)該記得 MessageContract,將 filename 放到 MessageHeader 里面就行了。

 
 
 
  1. [MessageContract]  
  2. public class FileData  
  3. {  
  4. [MessageHeader]public string filename;  
  5. [MessageBodyMember]public Stream data;  
  6. }  
  7. [ServiceContract]  
  8. public interface IFileService  
  9. {  
  10. [OperationContract]  
  11. void Upload(FileData file);  
  12. }  
  13. public class FileService : IFileService, IDisposable  
  14. {  
  15. public void Upload(FileData file)  
  16. {  
  17. FileStream f = new FileStream(file.filename, FileMode.Create);  
  18. try  
  19. {  
  20. BinaryWriter writer = new BinaryWriter(f);  
  21. BinaryReader reader = new BinaryReader(file.data);  
  22. byte[] buffer;  
  23. do  
  24. {  
  25. buffer = reader.ReadBytes(1024);  
  26. writer.Write(buffer);  
  27. }  
  28. while (buffer.Length > 0);  
  29. }  
  30. finally  
  31. {  
  32. f.Close();  
  33. file.data.Close();  
  34. }  
  35. }  
  36. public void Dispose(){  
  37. Console.WriteLine("Dispose...");  
  38. }  
  39. }  
  40. public class WcfTest  
  41. {  
  42. public static void Test()  
  43. {  
  44. AppDomain.CreateDomain("Server").DoCallBack(delegate  
  45. {  
  46. ServiceHost host = new ServiceHost(typeof(FileService),   
  47. new Uri("http://localhost:8080/FileService"));  
  48. BasicHttpBinding binding = new BasicHttpBinding();  
  49. binding.TransferMode = TransferMode.Streamed;  
  50. host.AddServiceEndpoint(typeof(IFileService), binding, "");  
  51. host.Open();  
  52. });  
  53. BasicHttpBinding binding2 = new BasicHttpBinding();  
  54. binding2.TransferMode = TransferMode.Streamed;  
  55. IFileService channel = ChannelFactory.
    CreateChannel(binding2,   
  56. new EndpointAddress("http://localhost:8080/FileService"));  
  57. using (channel as IDisposable)  
  58. {  
  59. FileData file = new FileData();  
  60. file.filename = "test2.dll";  
  61. file.data = new FileStream("MyLibrary2.dll", FileMode.Open);  
  62. channel.Upload(file);  
  63. file.data.Close();  
  64. }  
  65. }  

 

問題解決了。上面的例子使用 BaseHttpBinding,如果使用 NetTcpBinding,相信速度要快很多。除了向服務(wù)器傳送流外,也可反向返回流數(shù)據(jù)。

 
 
 
  1. [ServiceContract]  
  2. public interface IFileService  
  3. {  
  4. [OperationContract]  
  5. void Upload(Stream stream);  
  6. [OperationContract]  
  7. Stream Download(string filename);  

 

雖然服務(wù)器在操作結(jié)束時(shí)會(huì)自動(dòng)關(guān)閉客戶端 Request Stream,但個(gè)人建議還是使用 try...finnaly... 自主關(guān)閉要好一些,因?yàn)橐馔饪偸菚?huì)發(fā)生的。

WCF傳送二進(jìn)制流數(shù)據(jù)的全部操作方法就為大家介紹到這里。


新聞名稱:WCF傳送二進(jìn)制流數(shù)據(jù)基本實(shí)現(xiàn)步驟詳解
文章鏈接:http://www.dlmjj.cn/article/dhidppg.html