化零为整WCF(8) - 消息处理(使用流数据传输文件)

[索引页]
[源码下载] 


化零为整WCF(8) - 消息处理(使用流数据传输文件)


作者: webabcd


介绍
WCF(Windows Communication Foundation) - 消息处理:使用流数据传输文件,减少内存开销。


示例
1、服务
IStreamed.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif using System.IO; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Message 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 消息契约(定义与 SOAP 消息相对应的强类型类) 
InBlock.gif         /// </summary> 
InBlock.gif        [MessageContract] 
InBlock.gif         public  class FileWrapper 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 指定数据成员为 SOAP 消息头 
InBlock.gif                 /// </summary> 
InBlock.gif                [MessageHeader] 
InBlock.gif                 public  string FilePath; 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 指定将成员序列化为 SOAP 正文中的元素 
InBlock.gif                 /// </summary> 
InBlock.gif                [MessageBodyMember] 
InBlock.gif                 public Stream FileData; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         /// <summary> 
InBlock.gif         /// IStreamed接口 
InBlock.gif         /// </summary> 
InBlock.gif        [ServiceContract] 
InBlock.gif         public  interface IStreamed 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 上传文件 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <remarks> 
InBlock.gif                /// 1、支持数据流传输的绑定有:BasicHttpBinding、NetTcpBinding 和 NetNamedPipeBinding 
InBlock.gif                /// 2、流数据类型必须是可序列化的 Stream 或 MemoryStream 
InBlock.gif                // /3、传递时消息体(Message Body)中不能包含其他数据,即参数中只能有一个System.ServiceModel.MessageBodyMember 
InBlock.gif                /// </remarks> 

InBlock.gif                 /// <param name="fileWrapper">WCF.ServiceLib.Message.FileWrapper</param> 
InBlock.gif                [OperationContract] 
InBlock.gif                 void UploadFile(FileWrapper fileWrapper); 
InBlock.gif        } 
InBlock.gif}
 
Streamed.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif using System.IO; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Message 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// IStreamed类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class Streamed : IStreamed 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 上传文件 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="fileWrapper">WCF.ServiceLib.Message.FileWrapper</param> 
InBlock.gif                 public  void UploadFile(FileWrapper fileWrapper) 
InBlock.gif                { 
InBlock.gif                        var sourceStream = fileWrapper.FileData; 
InBlock.gif 
InBlock.gif                        var targetStream =  new FileStream(fileWrapper.FilePath, 
InBlock.gif                                FileMode.Create, 
InBlock.gif                                FileAccess.Write, 
InBlock.gif                                FileShare.None); 
InBlock.gif 
InBlock.gif                        var buffer =  new  byte[4096]; 
InBlock.gif                        var count = 0; 
InBlock.gif 
InBlock.gif                         while ((count = sourceStream.Read(buffer, 0, buffer.Length)) > 0) 
InBlock.gif                        { 
InBlock.gif                                targetStream.Write(buffer, 0, count); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        targetStream.Close(); 
InBlock.gif                        sourceStream.Close(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
2、宿主
Streamed.cs
InBlock.gif using (ServiceHost host =  new ServiceHost( typeof(WCF.ServiceLib.Message.Streamed))) 
InBlock.gif
InBlock.gif        host.Open(); 
InBlock.gif 
InBlock.gif        Console.WriteLine( "服务已启动(WCF.ServiceLib.Message.Streamed)"); 
InBlock.gif        Console.WriteLine( "按<ENTER>停止服务"); 
InBlock.gif        Console.ReadLine(); 
InBlock.gif 
InBlock.gif}
 
 
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>
 
 
3、客户端
Streamed.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.Windows.Forms; 
InBlock.gif using System.ServiceModel; 
InBlock.gif using System.IO; 
InBlock.gif 
InBlock.gif namespace Client2.Message 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 演示Message.Streamed的类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class Streamed 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 流数据上传文件 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="source">源文件地址</param> 
InBlock.gif                 /// <param name="destination">目标路径</param> 
InBlock.gif                 public  void HelloStreamed( string source,  string destination) 
InBlock.gif                { 
InBlock.gif                         try 
InBlock.gif                        { 
InBlock.gif                                var proxy =  new MessageSvc.Streamed.StreamedClient(); 
InBlock.gif 
InBlock.gif                                var sr =  new System.IO.FileStream( 
InBlock.gif                                        source, System.IO.FileMode.Open); 
InBlock.gif 
InBlock.gif                                proxy.UploadFile(destination + Path.GetFileName(source), sr); 
InBlock.gif 
InBlock.gif                                sr.Close(); 
InBlock.gif                                proxy.Close(); 
InBlock.gif 
InBlock.gif                                MessageBox.Show( "上传成功"); 
InBlock.gif                        } 
InBlock.gif                         catch (Exception ex) 
InBlock.gif                        { 
InBlock.gif                                MessageBox.Show(ex.ToString()); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
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>
 
运行结果:
上传文件后提示上传成功


OK
[源码下载]

     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/344131 ,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值