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数据契约

2010-02-22 16:26:47

WCF传输数据

2009-11-06 09:14:14

WCF可靠性

2010-03-02 13:43:01

WCF事务演示

2009-12-08 17:56:16

WCF配置

2009-12-07 18:43:29

WCF框架

2010-03-02 16:28:11

WCF发布订阅

2009-12-07 09:23:05

2010-02-25 13:40:17

WCF禁用安全配置

2010-02-22 10:29:11

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技术栈公众号