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

WCF傳送二進制流數據基本實現步驟詳解

開發 開發工具
WCF傳送二進制流數據的相關操作方法在實際應用中是一個比較基礎的操作應用。我們在這里將會針對此做一個詳細介紹。

我們知道,在實現WCF傳送二進制流數據這一操作過程中,會有一些限制因素。我們在實際應用中要特別注意這一點。今天我們就會針對這方面的問題做一個詳細的介紹,希望對大家有所幫助。#t#

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

我們先看看下面的WCF傳送二進制流數據例子。

注意將 Binding.TransferMode 設置為 TransferMode.Streamed,我們還可以修改 Binding.MaxReceivedMessageSize 來調整消息大小(默認是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. }  

 

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

 

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

 

當你修改完WCF傳送二進制流數據的代碼后,運行時你發現觸發了一個 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 肯定不行。 沒錯!你應該記得 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. }  

 

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

 

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

 

雖然服務器在操作結束時會自動關閉客戶端 Request Stream,但個人建議還是使用 try...finnaly... 自主關閉要好一些,因為意外總是會發生的。

WCF傳送二進制流數據的全部操作方法就為大家介紹到這里。

責任編輯:曹凱 來源: CSDN
相關推薦

2018-10-22 14:37:16

二進制數據存儲

2013-07-29 11:19:16

iOS開發iOS開發學習FMDB更新二進制圖片

2009-02-27 09:37:33

Google二進制代碼

2022-10-31 08:02:42

二進制計算乘法

2009-12-22 10:05:54

WCF編程生命周期

2010-03-01 10:54:29

WCF雙工會話通道

2010-03-01 16:31:58

WCF實現SOA

2010-06-09 13:02:29

MySQL啟用二進制日

2010-10-13 15:45:23

MySQL二進制日志

2009-08-12 18:06:53

C#讀取二進制文件

2025-01-26 10:21:54

2009-12-16 10:49:42

Ruby操作二進制文件

2017-04-11 10:48:53

JS二進制

2022-07-26 13:00:01

安全符號源代碼

2022-07-18 09:01:15

SwiftApple二進制目標

2024-01-31 09:55:53

2021-01-14 09:40:54

漏洞macOS屬性表文件

2009-12-10 09:24:50

PHP函數fwrite

2020-06-15 17:05:46

前端二進制瀏覽器

2023-09-18 23:50:25

二進制文件裁剪Layout
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本一道本视频 | 日本精品一区二区三区视频 | 久久三区 | 一级黄大片 | 日韩日韩日韩日韩日韩日韩日韩 | 2019天天操 | 久久中文字幕一区 | 国产视频精品在线观看 | 成人午夜网站 | 精品日韩一区二区三区 | 欧美精品久久久久久久久久 | 日韩综合网 | 一区二区三区在线播放 | 欧美亚洲另类在线 | 福利视频一区 | 国内自拍第一页 | 高清av在线| 久在线精品视频 | 日韩欧美中文字幕在线视频 | 在线激情视频 | 天天亚洲 | 一区二区不卡视频 | 中文字幕高清av | 成人欧美一区二区三区黑人孕妇 | 国产精品大片 | 免费成人国产 | 91视频一区二区三区 | 欧美国产精品 | 99视频在线免费观看 | 免费一看一级毛片 | 欧美一级二级三级 | 日本高清aⅴ毛片免费 | 精产国产伦理一二三区 | 国产1区2区在线观看 | 国产一区二区影院 | 日本高清视频在线播放 | 欧美电影在线观看网站 | 国产黄色在线观看 | 久久久久久91香蕉国产 | 欧美一二区 | 国产精品久久久久久亚洲调教 |