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

WCF傳送二進(jìn)制流數(shù)據(jù)基本實(shí)現(xiàn)步驟詳解

開(kāi)發(fā) 開(kāi)發(fā)工具
WCF傳送二進(jìn)制流數(shù)據(jù)的相關(guān)操作方法在實(shí)際應(yīng)用中是一個(gè)比較基礎(chǔ)的操作應(yīng)用。我們?cè)谶@里將會(huì)針對(duì)此做一個(gè)詳細(xì)介紹。

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

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

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

注意將 Binding.TransferMode 設(shè)置為 TransferMode.Streamed,我們還可以修改 Binding.MaxReceivedMessageSize 來(lái)調(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<IFileService>.
    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 肯定不行。 沒(méi)錯(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<IFileService>.
    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. }  

 

問(wèn)題解決了。上面的例子使用 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ù)的全部操作方法就為大家介紹到這里。

責(zé)任編輯:曹凱 來(lái)源: CSDN
相關(guān)推薦

2018-10-22 14:37:16

二進(jìn)制數(shù)據(jù)存儲(chǔ)

2013-07-29 11:19:16

iOS開(kāi)發(fā)iOS開(kāi)發(fā)學(xué)習(xí)FMDB更新二進(jìn)制圖片

2009-02-27 09:37:33

Google二進(jìn)制代碼

2022-10-31 08:02:42

二進(jìn)制計(jì)算乘法

2009-12-22 10:05:54

WCF編程生命周期

2009-08-12 18:06:53

C#讀取二進(jìn)制文件

2010-10-13 15:45:23

MySQL二進(jìn)制日志

2010-06-09 13:02:29

MySQL啟用二進(jìn)制日

2010-03-01 10:54:29

WCF雙工會(huì)話通道

2025-01-26 10:21:54

2009-12-16 10:49:42

Ruby操作二進(jìn)制文件

2017-04-11 10:48:53

JS二進(jìn)制

2022-07-26 13:00:01

安全符號(hào)源代碼

2010-03-01 16:31:58

WCF實(shí)現(xiàn)SOA

2022-07-18 09:01:15

SwiftApple二進(jìn)制目標(biāo)

2021-01-14 09:40:54

漏洞macOS屬性表文件

2009-12-10 09:24:50

PHP函數(shù)fwrite

2024-01-31 09:55:53

2023-09-18 23:50:25

二進(jìn)制文件裁剪Layout

2020-06-15 17:05:46

前端二進(jìn)制瀏覽器
點(diǎn)贊
收藏

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