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

WCF消息處理分布剖析

開發 開發工具
WCF消息處理是一個比較重要的基礎概念,對于初學者來說,我們需要在學習的過程中對此進行詳細的分析,以此來提高我們的應用水平。

WCF是一個使用了托管代碼建立的統一框架。它的應用可以幫助開發者創建一個安全性高,可依賴性的解決方案。那么今天,我們將會在這里為大家詳細介紹一下其中WCF消息處理的相關概念。

使用托管代碼建立和運行面向服務(Service Oriented)應用程序的統一框架

WCF消息處理:使用流數據傳輸文件,減少內存開銷。

示例

1、WCF消息處理之服務

  1. IStreamed.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.ServiceModel;  
  7. using System.IO;  
  8. namespace WCF.ServiceLib.Message  
  9. {  
  10. /**//// < summary> 
  11. /// 消息契約(定義與 SOAP 消息相對應的強類型類)  
  12. /// < /summary> 
  13. [MessageContract]  
  14. public class FileWrapper  
  15. {  
  16. /**//// < summary> 
  17. /// 指定數據成員為 SOAP 消息頭  
  18. /// < /summary> 
  19. [MessageHeader]  
  20. public string FilePath;  
  21. /**//// < summary> 
  22. /// 指定將成員序列化為 SOAP 正文中的元素  
  23. /// < /summary> 
  24. [MessageBodyMember]  
  25. public Stream FileData;  
  26. }  
  27. /**//// < summary> 
  28. /// IStreamed接口  
  29. /// < /summary> 
  30. [ServiceContract]  
  31. public interface IStreamed  
  32. {  
  33. /**//// < summary> 
  34. /// 上傳文件  
  35. /// < /summary> 
  36. /// < remarks> 
  37. /// 1、支持數據流傳輸的綁定有:BasicHttpBinding、NetTcpBinding 
    和 NetNamedPipeBinding  
  38. /// 2、流數據類型必須是可序列化的 Stream 或 MemoryStream  
  39. // /3、傳遞時消息體(Message Body)中不能包含其他數據,即參數中只能有一個
    System.ServiceModel.MessageBodyMember  
  40. /**//// < /remarks> 
  41. /// < param name="fileWrapper">WCF.ServiceLib.Message.FileWrapper< /param> 
  42. [OperationContract]  
  43. void UploadFile(FileWrapper fileWrapper);  
  44. }  
  45. }  
  46. Streamed.cs  
  47. using System;  
  48. using System.Collections.Generic;  
  49. using System.Linq;  
  50. using System.Text;  
  51. using System.ServiceModel;  
  52. using System.IO;  
  53. namespace WCF.ServiceLib.Message  
  54. {  
  55. /**//// < summary> 
  56. /// IStreamed類  
  57. /// < /summary> 
  58. public class Streamed : IStreamed  
  59. {  
  60. /**//// < summary> 
  61. /// 上傳文件  
  62. /// < /summary> 
  63. /// < param name="fileWrapper">WCF.ServiceLib.Message.
    FileWrapper
    < /param> 
  64. public void UploadFile(FileWrapper fileWrapper)  
  65. {  
  66. var sourceStream = fileWrapper.FileData;  
  67. var targetStream = new FileStream(fileWrapper.FilePath,  
  68. FileMode.Create,  
  69. FileAccess.Write,  
  70. FileShare.None);  
  71. var buffer = new byte[4096];  
  72. var count = 0;  
  73. while ((count = sourceStream.Read(buffer, 0, buffer.Length)) > 0)  
  74. {  
  75. targetStream.Write(buffer, 0, count);  
  76. }  
  77. targetStream.Close();  
  78. sourceStream.Close();  
  79. }  
  80. }  

#p#

2、WCF消息處理之宿主

  1. Streamed.cs  
  2. using (ServiceHost host = new ServiceHost(typeof
    (WCF.ServiceLib.Message.Streamed)))  
  3. {  
  4. host.Open();  
  5. Console.WriteLine("服務已啟動(WCF.ServiceLib.Message.Streamed)");  
  6. Console.WriteLine("按< ENTER>停止服務");  
  7. Console.ReadLine();  
  8. }  
  9. App.config  
  10. < ?xml version="1.0" encoding="utf-8" ?> 
  11. < configuration> 
  12. < system.serviceModel> 
  13. < services> 
  14. < !--name - 提供服務的類名--> 
  15. < !--behaviorConfiguration - 指定相關的行為配置--> 
  16. < service name="WCF.ServiceLib.Message.Streamed" 
    behaviorConfiguration="MessageBehavior"> 
  17. < !--address - 服務地址--> 
  18. < !--binding - 通信方式--> 
  19. < !--contract - 服務契約--> 
  20. < !--bindingConfiguration - 指定相關的綁定配置--> 
  21. < endpoint address="Message/Streamed" binding="netTcpBinding" 
    contract="WCF.ServiceLib.Message.IStreamed" 
    bindingConfiguration="StreamedBindingConfiguration" /> 
  22. < endpoint address="mex" binding="mexHttpBinding" 
    contract="IMetadataExchange" /> 
  23. < host> 
  24. < baseAddresses> 
  25. < add baseAddress="http://localhost:12345/Message/Streamed/"/> 
  26. < add baseAddress="net.tcp://localhost:54321/"/> 
  27. < /baseAddresses> 
  28. < /host> 
  29. < /service> 
  30. < /services> 
  31. < behaviors> 
  32. < serviceBehaviors> 
  33. < behavior name="MessageBehavior"> 
  34. < !--httpGetEnabled - 使用get方式提供服務--> 
  35. < serviceMetadata httpGetEnabled="true" /> 
  36. < serviceDebug includeExceptionDetailInFaults="true"/> 
  37. < /behavior> 
  38. < /serviceBehaviors> 
  39. < /behaviors> 
  40. < bindings> 
  41. < netTcpBinding> 
  42. < !--transferMode - 指示通道是使用流處理模式還是緩沖模式來傳輸請求和響應消息--> 
  43. < !--maxReceivedMessageSize - 
    在采用此綁定配置的通道上可接收的***消息大?。▎挝唬鹤止潱?-
    > 
  44. < !--receiveTimeout - 在傳輸引發異常之前可用于完成讀取操作的時間間隔--> 
  45. < binding name="StreamedBindingConfiguration" transferMode="Streamed" 
    maxReceivedMessageSize="1073741824" receiveTimeout="00:10:00" /> 
  46. < /netTcpBinding> 
  47. < /bindings> 
  48. < /system.serviceModel> 
  49. < /configuration> 

3、WCF消息處理之客戶端

  1. Streamed.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Windows.Forms;  
  7. using System.ServiceModel;  
  8. using System.IO;  
  9. namespace Client2.Message  
  10. {  
  11. /**//// < summary> 
  12. /// 演示Message.Streamed的類  
  13. /// < /summary> 
  14. public class Streamed  
  15. {  
  16. /**//// < summary> 
  17. /// 流數據上傳文件  
  18. /// < /summary> 
  19. /// < param name="source">源文件地址< /param> 
  20. /// < param name="destination">目標路徑< /param> 
  21. public void HelloStreamed(string source, string destination)  
  22. {  
  23. try  
  24. {  
  25. var proxy = new MessageSvc.Streamed.StreamedClient();  
  26. var sr = new System.IO.FileStream(  
  27. source, System.IO.FileMode.Open);  
  28. proxy.UploadFile(destination + Path.GetFileName(source), sr);  
  29. sr.Close();  
  30. proxy.Close();  
  31. MessageBox.Show("上傳成功");  
  32. }  
  33. catch (Exception ex)  
  34. {  
  35. MessageBox.Show(ex.ToString());  
  36. }  
  37. }  
  38. }  
  39. }  
  40. App.config  
  41. < ?xml version="1.0" encoding="utf-8" ?> 
  42. < configuration> 
  43. < system.serviceModel> 
  44. < client> 
  45. < !--address - 服務地址--> 
  46. < !--binding - 通信方式--> 
  47. < !--contract - 服務契約--> 
  48. < endpoint address="net.tcp://localhost:54321/Message/Streamed" 
    binding="netTcpBinding" contract="MessageSvc.Streamed.IStreamed" 
    bindingConfiguration="StreamedBindingConfiguration" /> 
  49. < /client> 
  50. < bindings> 
  51. < netTcpBinding> 
  52. < !--transferMode - 指示通道是使用流處理模式還是緩沖模式來傳輸請求和響應消息--> 
  53. < !--sendTimeout - 在傳輸引發異常之前可用于完成寫入操作的時間間隔--> 
  54. < binding name="StreamedBindingConfiguration" 
    transferMode="Streamed" sendTimeout="00:10:00" /> 
  55. < /netTcpBinding> 
  56. < /bindings> 
  57. < /system.serviceModel> 
  58. < /configuration> 

 以上就是對WCF消息處理的相關概念介紹。

【編輯推薦】

  1. 六步驟完成WCF開發
  2. WCF附加屬性技巧掌握
  3. 學習WCF綁定經驗分享
  4. WCF效率提高技巧講解
  5. WCF自承載實踐心得分享
責任編輯:曹凱 來源: 博客園
相關推薦

2009-11-09 11:15:06

WCF消息隊列

2010-02-23 09:34:15

WCF重載

2010-02-22 16:09:33

WCF宿主

2010-02-22 15:27:05

WCF數據契約

2009-11-06 09:14:14

WCF可靠性

2010-02-22 16:26:47

WCF傳輸數據

2010-03-02 13:43:01

WCF事務演示

2009-12-07 18:43:29

WCF框架

2009-12-08 17:56:16

WCF配置

2010-03-02 16:28:11

WCF發布訂閱

2009-12-07 09:23:05

2010-02-22 10:29:11

WCF上傳文件

2010-02-25 13:40:17

WCF禁用安全配置

2010-02-23 16:07:39

2010-02-23 11:22:15

WCF跟蹤調試

2009-12-08 16:09:02

WCF消息

2010-02-24 09:18:49

WCF Adapter

2010-02-24 15:42:03

WCF服務端安全

2010-03-02 11:10:43

WCF標準終結點

2009-11-09 13:04:53

WCF事物處理
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲性人人天天夜夜摸 | 日韩一级免费电影 | 亚洲精品久久久 | 国产一区二区精品在线 | 国产精品一区二区久久 | 91亚洲国产成人久久精品网站 | 黄色网一级片 | 国产免费又色又爽又黄在线观看 | 午夜91| 男女视频免费 | 国产精品久久久久久久久久免费 | 日韩色视频| 亚洲97 | 91av在线免费播放 | 欧美精品一区二区在线观看 | 九九综合九九 | 一区二区三区不卡视频 | 色男人天堂av | 国产精品欧美一区二区三区不卡 | 国产一区二区三区在线 | 亚洲国产一区在线 | 国产一区二区日韩 | 嫩草黄色影院 | 午夜在线免费观看 | 国产欧美精品区一区二区三区 | 欧美激情亚洲天堂 | 犬夜叉在线观看 | 日本三级网站在线 | 在线伊人 | 久久99国产精品 | 中文字幕在线观看一区 | 国产精品亚洲一区二区三区在线观看 | 色综合久久88色综合天天 | 美女黄网 | 午夜激情视频在线 | 精品一二区 | 日韩av免费在线观看 | 插插宗合网 | 日韩中文一区二区三区 | 欧美福利网站 | 欧美456|