当前位置:编程学习 > 网站相关 >>

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

作者:webabcd


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


示例
1、服务
IMtom.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.IO;

namespace WCF.ServiceLib.Message
{
    /**//// <summary>
    /// IMtom接口
    /// </summary>
    [ServiceContract]
    public interface IMtom
    {
        /**//// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="path">文件目标路径</param>
        /// <param name="fileData">文件字节数组</param>
        [OperationContract]
        void UploadFile(string path, byte[] fileData);
    }
}

Mtom.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.IO;

namespace WCF.ServiceLib.Message
{
    /**//// <summary>
    /// Mtom类
    /// </summary>
    public class Mtom : IMtom
    {
        /**//// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="path">文件目标路径</param>
        /// <param name="fileData">文件字节数组</param>
        public void UploadFile(string path, byte[] fileData)
        {
            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
            fs.Write(fileData, 0, fileData.Length);
            fs.Flush();
            fs.Close();
        }
    }
}

 

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
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,