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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WCF消息處理分布剖析

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

創(chuàng)新互聯(lián)公司服務(wù)項目包括禹王臺網(wǎng)站建設(shè)、禹王臺網(wǎng)站制作、禹王臺網(wǎng)頁制作以及禹王臺網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,禹王臺網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到禹王臺省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

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

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

示例

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 消息相對應(yī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、支持數(shù)據(jù)流傳輸?shù)慕壎ㄓ校築asicHttpBinding、NetTcpBinding 
    和 NetNamedPipeBinding  
  38. /// 2、流數(shù)據(jù)類型必須是可序列化的 Stream 或 MemoryStream  
  39. // /3、傳遞時消息體(Message Body)中不能包含其他數(shù)據(jù),即參數(shù)中只能有一個
    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ù)已啟動(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 - 指示通道是使用流處理模式還是緩沖模式來傳輸請求和響應(yīng)消息--> 
  43. < !--maxReceivedMessageSize - 
    在采用此綁定配置的通道上可接收的***消息大?。▎挝唬鹤止?jié))--> 
  44. < !--receiveTimeout - 在傳輸引發(fā)異常之前可用于完成讀取操作的時間間隔--> 
  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">目標路徑< /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 - 指示通道是使用流處理模式還是緩沖模式來傳輸請求和響應(yīng)消息--> 
  53. < !--sendTimeout - 在傳輸引發(fā)異常之前可用于完成寫入操作的時間間隔--> 
  54. < binding name="StreamedBindingConfiguration" 
    transferMode="Streamed" sendTimeout="00:10:00" /> 
  55. < /netTcpBinding> 
  56. < /bindings> 
  57. < /system.serviceModel> 
  58. < /configuration> 

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


當(dāng)前標題:WCF消息處理分布剖析
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/djssgsj.html