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

WCF消息處理分布剖析

開(kāi)發(fā) 開(kāi)發(fā)工具
WCF消息處理是一個(gè)比較重要的基礎(chǔ)概念,對(duì)于初學(xué)者來(lái)說(shuō),我們需要在學(xué)習(xí)的過(guò)程中對(duì)此進(jìn)行詳細(xì)的分析,以此來(lái)提高我們的應(yīng)用水平。

WCF是一個(gè)使用了托管代碼建立的統(tǒng)一框架。它的應(yīng)用可以幫助開(kāi)發(fā)者創(chuàng)建一個(gè)安全性高,可依賴性的解決方案。那么今天,我們將會(huì)在這里為大家詳細(xì)介紹一下其中WCF消息處理的相關(guān)概念。

使用托管代碼建立和運(yùn)行面向服務(wù)(Service Oriented)應(yīng)用程序的統(tǒng)一框架

WCF消息處理:使用流數(shù)據(jù)傳輸文件,減少內(nèi)存開(kāi)銷(xiāo)。

示例

1、WCF消息處理之服務(wù)

  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 消息相對(duì)應(yīng)的強(qiáng)類型類)  
  12. /// < /summary> 
  13. [MessageContract]  
  14. public class FileWrapper  
  15. {  
  16. /**//// < summary> 
  17. /// 指定數(shù)據(jù)成員為 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、支持?jǐn)?shù)據(jù)流傳輸?shù)慕壎ㄓ校築asicHttpBinding、NetTcpBinding 
    和 NetNamedPipeBinding  
  38. /// 2、流數(shù)據(jù)類型必須是可序列化的 Stream 或 MemoryStream  
  39. // /3、傳遞時(shí)消息體(Message Body)中不能包含其他數(shù)據(jù),即參數(shù)中只能有一個(gè)
    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("服務(wù)已啟動(dòng)(WCF.ServiceLib.Message.Streamed)");  
  6. Console.WriteLine("按< ENTER>停止服務(wù)");  
  7. Console.ReadLine();  
  8. }  
  9. App.config  
  10. < ?xml version="1.0" encoding="utf-8" ?> 
  11. < configuration> 
  12. < system.serviceModel> 
  13. < services> 
  14. < !--name - 提供服務(wù)的類名--> 
  15. < !--behaviorConfiguration - 指定相關(guān)的行為配置--> 
  16. < service name="WCF.ServiceLib.Message.Streamed" 
    behaviorConfiguration="MessageBehavior"> 
  17. < !--address - 服務(wù)地址--> 
  18. < !--binding - 通信方式--> 
  19. < !--contract - 服務(wù)契約--> 
  20. < !--bindingConfiguration - 指定相關(guān)的綁定配置--> 
  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方式提供服務(wù)--> 
  35. < serviceMetadata httpGetEnabled="true" /> 
  36. < serviceDebug includeExceptionDetailInFaults="true"/> 
  37. < /behavior> 
  38. < /serviceBehaviors> 
  39. < /behaviors> 
  40. < bindings> 
  41. < netTcpBinding> 
  42. < !--transferMode - 指示通道是使用流處理模式還是緩沖模式來(lái)傳輸請(qǐng)求和響應(yīng)消息--> 
  43. < !--maxReceivedMessageSize - 
    在采用此綁定配置的通道上可接收的***消息大小(單位:字節(jié))--
    > 
  44. < !--receiveTimeout - 在傳輸引發(fā)異常之前可用于完成讀取操作的時(shí)間間隔--> 
  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. /// 流數(shù)據(jù)上傳文件  
  18. /// < /summary> 
  19. /// < param name="source">源文件地址< /param> 
  20. /// < param name="destination">目標(biāo)路徑< /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 - 服務(wù)地址--> 
  46. < !--binding - 通信方式--> 
  47. < !--contract - 服務(wù)契約--> 
  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 - 指示通道是使用流處理模式還是緩沖模式來(lái)傳輸請(qǐng)求和響應(yīng)消息--> 
  53. < !--sendTimeout - 在傳輸引發(fā)異常之前可用于完成寫(xiě)入操作的時(shí)間間隔--> 
  54. < binding name="StreamedBindingConfiguration" 
    transferMode="Streamed" sendTimeout="00:10:00" /> 
  55. < /netTcpBinding> 
  56. < /bindings> 
  57. < /system.serviceModel> 
  58. < /configuration> 

 以上就是對(duì)WCF消息處理的相關(guān)概念介紹。

【編輯推薦】

  1. 六步驟完成WCF開(kāi)發(fā)
  2. WCF附加屬性技巧掌握
  3. 學(xué)習(xí)WCF綁定經(jīng)驗(yàn)分享
  4. WCF效率提高技巧講解
  5. WCF自承載實(shí)踐心得分享
責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2009-11-09 11:15:06

WCF消息隊(duì)列

2010-02-22 15:27:05

WCF數(shù)據(jù)契約

2010-02-22 16:09:33

WCF宿主

2010-02-23 09:34:15

WCF重載

2009-12-08 17:56:16

WCF配置

2010-03-02 16:28:11

WCF發(fā)布訂閱

2009-12-07 18:43:29

WCF框架

2009-11-06 09:14:14

WCF可靠性

2010-02-22 16:26:47

WCF傳輸數(shù)據(jù)

2010-03-02 13:43:01

WCF事務(wù)演示

2009-12-07 09:23:05

2010-02-23 11:22:15

WCF跟蹤調(diào)試

2010-02-23 16:07:39

2010-02-22 10:29:11

WCF上傳文件

2010-02-25 13:40:17

WCF禁用安全配置

2010-02-24 09:18:49

WCF Adapter

2009-12-08 16:09:02

WCF消息

2010-03-02 11:10:43

WCF標(biāo)準(zhǔn)終結(jié)點(diǎn)

2010-02-24 15:42:03

WCF服務(wù)端安全

2010-02-22 13:35:03

WCF異常處理
點(diǎn)贊
收藏

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