WCF消息处理分布剖析

开发 开发工具
WCF消息处理是一个比较重要的基础概念,对于初学者来说,我们需要在学习的过程中对此进行详细的分析,以此来提高我们的应用水平。

WCF是一个使用了托管代码建立的统一框架。它的应用可以帮助开发者创建一个安全性高,可依赖性的解决方案。那么今天,我们将会在这里为大家详细介绍一下其中WCF消息处理的相关概念。

使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架

WCF消息处理:使用流数据传输文件,减少内存开销。

示例

1、WCF消息处理之服务

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

#p#

2、WCF消息处理之宿主

Streamed.cs  
using (ServiceHost host = new ServiceHost(typeof
(WCF.ServiceLib.Message.Streamed)))  
{   host.Open();   Console.WriteLine("服务已启动(WCF.ServiceLib.Message.Streamed)");   Console.WriteLine("按< ENTER>停止服务");   Console.ReadLine();   }   App.config   < ?xml version="1.0" encoding="utf-8" ?>  < configuration>  < system.serviceModel>  < services>  < !--name - 提供服务的类名-->  < !--behaviorConfiguration - 指定相关的行为配置-->  < service name="WCF.ServiceLib.Message.Streamed" 
behaviorConfiguration="MessageBehavior">  < !--address - 服务地址-->  < !--binding - 通信方式-->  < !--contract - 服务契约-->  < !--bindingConfiguration - 指定相关的绑定配置-->  < endpoint address="Message/Streamed" binding="netTcpBinding" 
contract="WCF.ServiceLib.Message.IStreamed" 
bindingConfiguration="StreamedBindingConfiguration" />  < endpoint address="mex" binding="mexHttpBinding" 
contract="IMetadataExchange" />  < host>  < baseAddresses>  < add baseAddress="http://localhost:12345/Message/Streamed/"/>  < add baseAddress="net.tcp://localhost:54321/"/>  < /baseAddresses>  < /host>  < /service>  < /services>  < behaviors>  < serviceBehaviors>  < behavior name="MessageBehavior">  < !--httpGetEnabled - 使用get方式提供服务-->  < serviceMetadata httpGetEnabled="true" />  < serviceDebug includeExceptionDetailInFaults="true"/>  < /behavior>  < /serviceBehaviors>  < /behaviors>  < bindings>  < netTcpBinding>  < !--transferMode - 指示通道是使用流处理模式还是缓冲模式来传输请求和响应消息-->  < !--maxReceivedMessageSize - 
在采用此绑定配置的通道上可接收的***消息大小(单位:字节)--
>  < !--receiveTimeout - 在传输引发异常之前可用于完成读取操作的时间间隔-->  < binding name="StreamedBindingConfiguration" transferMode="Streamed" 
maxReceivedMessageSize="1073741824" receiveTimeout="00:10:00" />  < /netTcpBinding>  < /bindings>  < /system.serviceModel>  < /configuration> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.

3、WCF消息处理之客户端

Streamed.cs  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
using System.ServiceModel;  
using System.IO;  
namespace Client2.Message  
{  
/**//// < summary> 
/// 演示Message.Streamed的类  
/// < /summary> 
public class Streamed  
{  
/**//// < summary> 
/// 流数据上传文件  
/// < /summary> 
/// < param name="source">源文件地址< /param> 
/// < param name="destination">目标路径< /param> 
public void HelloStreamed(string source, string destination)  
{  
try  
{  
var proxy = new MessageSvc.Streamed.StreamedClient();  
var sr = new System.IO.FileStream(  
source, System.IO.FileMode.Open);  
proxy.UploadFile(destination + Path.GetFileName(source), sr);  
sr.Close();  
proxy.Close();  
MessageBox.Show("上传成功");  
}  
catch (Exception ex)  
{  
MessageBox.Show(ex.ToString());  
}  
}  
}  
}  
App.config  
< ?xml version="1.0" encoding="utf-8" ?> 
< configuration> 
< system.serviceModel> 
< client> 
< !--address - 服务地址--> 
< !--binding - 通信方式--> 
< !--contract - 服务契约--> 
< endpoint address="net.tcp://localhost:54321/Message/Streamed" 
binding="netTcpBinding" contract="MessageSvc.Streamed.IStreamed" 
bindingConfiguration="StreamedBindingConfiguration" />  < /client>  < bindings>  < netTcpBinding>  < !--transferMode - 指示通道是使用流处理模式还是缓冲模式来传输请求和响应消息-->  < !--sendTimeout - 在传输引发异常之前可用于完成写入操作的时间间隔-->  < binding name="StreamedBindingConfiguration" 
transferMode="Streamed" sendTimeout="00:10:00" />  < /netTcpBinding>  < /bindings>  < /system.serviceModel>  < /configuration> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.

 以上就是对WCF消息处理的相关概念介绍。

【编辑推荐】

  1. 六步骤完成WCF开发
  2. WCF附加属性技巧掌握
  3. 学习WCF绑定经验分享
  4. WCF效率提高技巧讲解
  5. WCF自承载实践心得分享
责任编辑:曹凯 来源: 博客园
相关推荐

2009-11-09 11:15:06

WCF消息队列

2010-02-22 16:09:33

WCF宿主

2010-02-23 09:34:15

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-25 13:40:17

WCF禁用安全配置

2010-02-22 10:29:11

WCF上传文件

2010-02-23 11:22:15

WCF跟踪调试

2010-02-23 16:07:39

2010-02-24 15:42:03

WCF服务端安全

2009-12-08 16:09:02

WCF消息

2010-02-24 09:18:49

WCF Adapter

2010-03-02 11:10:43

WCF标准终结点

2009-11-09 13:04:53

WCF事物处理
点赞
收藏

51CTO技术栈公众号