化零为整WCF(7) - 消息处理(使用消息传输优化机制 - MTOM)

本文通过上传大文件的示例介绍了WCF中的MTOM(消息传输优化机制)。演示了如何通过配置MTOM来减少SOAP消息的大小,特别是在处理大型文件时。展示了服务端与客户端的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[索引页]
[源码下载] 


化零为整WCF(7) - 消息处理(使用消息传输优化机制 - MTOM)


作者: webabcd


介绍
WCF(Windows Communication Foundation) - 消息处理:MTOM(Message Transmission Optimization Mechanism) - 消息传输优化机制。本文以web方式上传大文件为例。


示例
1、服务
IMtom.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         /// IMtom接口 
InBlock.gif         /// </summary> 
InBlock.gif        [ServiceContract] 
InBlock.gif         public  interface IMtom 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 上传文件 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="path">文件目标路径</param> 
InBlock.gif                 /// <param name="fileData">文件字节数组</param> 
InBlock.gif                [OperationContract] 
InBlock.gif                 void UploadFile( string path,  byte[] fileData); 
InBlock.gif        } 
InBlock.gif}
 
Mtom.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         /// Mtom类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class Mtom : IMtom 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 上传文件 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="path">文件目标路径</param> 
InBlock.gif                 /// <param name="fileData">文件字节数组</param> 
InBlock.gif                 public  void UploadFile( string path,  byte[] fileData) 
InBlock.gif                { 
InBlock.gif                        FileStream fs =  new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); 
InBlock.gif                        fs.Write(fileData, 0, fileData.Length); 
InBlock.gif                        fs.Flush(); 
InBlock.gif                        fs.Close(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 

2、宿主
Mtom.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Message.Mtom" %>
 
Web.config
<?xml version="1.0"?> 
<configuration> 
    <system.serviceModel> 
        <services> 
            <!--name - 提供服务的类名--> 
            <!--behaviorConfiguration - 指定相关的行为配置--> 
            <service name="WCF.ServiceLib.Message.Mtom" behaviorConfiguration="MessageBehavior"> 
                <!--address - 服务地址--> 
                <!--binding - 通信方式--> 
                <!--contract - 服务契约--> 
                <!--bindingConfiguration - 指定相关的绑定配置--> 
                <endpoint address="" binding="wsHttpBinding" contract="WCF.ServiceLib.Message.IMtom" bindingConfiguration="MtomBindingConfiguration" /> 
            </service> 
        </services> 
        <behaviors> 
            <serviceBehaviors> 
                <behavior name="MessageBehavior"> 
                    <!--httpGetEnabled - 使用get方式提供服务--> 
                    <serviceMetadata httpGetEnabled="true" /> 
                    <serviceDebug includeExceptionDetailInFaults="true"/> 
                </behavior> 
            </serviceBehaviors> 
        </behaviors> 
        <bindings> 
            <wsHttpBinding> 
                <!--messageEncoding - 指定用 MTOM 还是 Text 对 SOAP 消息编码--> 
                <!--maxReceivedMessageSize - 在采用此绑定配置的通道上可接收的最大消息大小(单位:字节)--> 
                <!--receiveTimeout - 在传输引发异常之前可用于完成读取操作的时间间隔--> 
                <binding name="MtomBindingConfiguration" messageEncoding="Mtom" maxReceivedMessageSize="1073741824" receiveTimeout="00:10:00"> 
                    <!--maxArrayLength - 配额控制:允许的最大数组长度--> 
                    <readerQuotas maxArrayLength="1073741824" /> 
                </binding> 
            </wsHttpBinding> 
        </bindings> 
    </system.serviceModel> 
</configuration>
 
 

3、客户端
Mtom.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Mtom.aspx.cs" 
        Inherits="Message_Mtom" Title="消息处理(使用消息传输优化机制 - MTOM)" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
        <p> 
                MTOM(Message Transmission Optimization Mechanism) - 消息传输优化机制 
        </p> 
        <div> 
                <ul> 
                        <li>可以指定用 MTOM 还是 Text 对 SOAP 消息编码</li> 
                        <li>抓soap消息的时候可以用tcpTrace</li> 
                        <li>用17,766,901字节大小的文件测试:Text编码(soap大小:31,591,929字节);MTOM编码(soap大小:23,696,066字节)</li> 
                </ul> 
        </div> 
        <div> 
                源文件: 
                <asp:FileUpload ID="file" runat="server" /> 
                  
                上传路径: 
                <asp:TextBox ID="txtDestination" runat="server" Text="C:\"></asp:TextBox> 
                  
                <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" /> 
        </div> 
</asp:Content>
 
Mtom.aspx.cs
InBlock.gif using System; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Data; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Xml.Linq; 
InBlock.gif using System.ServiceModel.Channels; 
InBlock.gif using System.IO; 
InBlock.gif 
InBlock.gif public partial  class Message_Mtom : System.Web.UI.Page 
InBlock.gif
InBlock.gif         protected  void Page_Load( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         protected  void btnUpload_Click( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                var proxy =  new MessageSvc.Mtom.MtomClient(); 
InBlock.gif 
InBlock.gif                var length = file.PostedFile.ContentLength; 
InBlock.gif                var bytes =  new  byte[length]; 
InBlock.gif                file.PostedFile.InputStream.Read(bytes, 0, length); 
InBlock.gif 
InBlock.gif                 try 
InBlock.gif                { 
InBlock.gif                        proxy.UploadFile( 
InBlock.gif                                txtDestination.Text + Path.GetFileName(file.PostedFile.FileName),    
InBlock.gif                                bytes); 
InBlock.gif                        Page.ClientScript.RegisterStartupScript( typeof(Page),  "js""alert('上传成功');"true); 
InBlock.gif                } 
InBlock.gif                 catch (Exception ex) 
InBlock.gif                { 
InBlock.gif                        Page.ClientScript.RegisterStartupScript( typeof(Page),  "js""alert('" + ex.ToString() +  "');"true); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                proxy.Close(); 
InBlock.gif        } 
InBlock.gif}
 
Web.config
<?xml version="1.0"?> 
<configuration> 
    <system.serviceModel> 
        <client> 
            <!--address - 服务地址--> 
            <!--binding - 通信方式--> 
            <!--contract - 服务契约--> 
            <!--bindingConfiguration - 指定相关的绑定配置--> 
            <!--behaviorConfiguration - 指定相关的行为配置--> 
            <!--endpoint address="http://localhost:3502/ServiceHost/Message/Mtom.svc" binding="wsHttpBinding" contract="MessageSvc.Mtom.IMtom" bindingConfiguration="MtomBindingConfiguration" behaviorConfiguration="MtomEndpointBehavior" /--> 
            <endpoint address="http://localhost:3502/ServiceHost/Message/Mtom.svc" binding="wsHttpBinding" contract="MessageSvc.Mtom.IMtom" bindingConfiguration="MtomBindingConfiguration" /> 
        </client> 
        <bindings> 
            <wsHttpBinding> 
                <!--messageEncoding - 指定用 MTOM 还是 Text 对 SOAP 消息编码--> 
                <!--sendTimeout - 在传输引发异常之前可用于完成写入操作的时间间隔--> 
                <binding name="MtomBindingConfiguration" messageEncoding="Mtom" sendTimeout="00:10:00"> 
                    <!--maxArrayLength - 配额控制:允许的最大数组长度--> 
                    <readerQuotas maxArrayLength="1073741824" /> 
                </binding> 
            </wsHttpBinding> 
        </bindings> 
        <behaviors> 
            <endpointBehaviors> 
                <behavior name="MtomEndpointBehavior"> 
                    <!--clientVia - 创建传输通道的 URI (tcpTrace抓soap的时候用)--> 
                    <clientVia viaUri="http://localhost:8888/ServiceHost/Message/Mtom.svc" /> 
                </behavior> 
            </endpointBehaviors> 
        </behaviors> 
    </system.serviceModel> 
</configuration>
 

运行结果:
上传文件后提示上传成功
 


OK
[源码下载]


     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/344124 ,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值