[索引页]
[源码下载]
作者: webabcd
介绍
WCF(Windows Communication Foundation) - 消息处理:MTOM(Message Transmission Optimization Mechanism) - 消息传输优化机制。本文以web方式上传大文件为例。
示例
1、服务
IMtom.cs
2、宿主
Mtom.svc
3、客户端
Mtom.aspx
运行结果:
上传文件后提示上传成功
OK
[源码下载]
[源码下载]
化零为整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);
}
}
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();
}
}
}
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>
<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>
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;
using System.ServiceModel.Channels;
using System.IO;
public partial class Message_Mtom : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
}
protected void btnUpload_Click( object sender, EventArgs e)
{
var proxy = new MessageSvc.Mtom.MtomClient();
var length = file.PostedFile.ContentLength;
var bytes = new byte[length];
file.PostedFile.InputStream.Read(bytes, 0, length);
try
{
proxy.UploadFile(
txtDestination.Text + Path.GetFileName(file.PostedFile.FileName),
bytes);
Page.ClientScript.RegisterStartupScript( typeof(Page), "js", "alert('上传成功');", true);
}
catch (Exception ex)
{
Page.ClientScript.RegisterStartupScript( typeof(Page), "js", "alert('" + ex.ToString() + "');", true);
}
proxy.Close();
}
}
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;
using System.ServiceModel.Channels;
using System.IO;
public partial class Message_Mtom : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
}
protected void btnUpload_Click( object sender, EventArgs e)
{
var proxy = new MessageSvc.Mtom.MtomClient();
var length = file.PostedFile.ContentLength;
var bytes = new byte[length];
file.PostedFile.InputStream.Read(bytes, 0, length);
try
{
proxy.UploadFile(
txtDestination.Text + Path.GetFileName(file.PostedFile.FileName),
bytes);
Page.ClientScript.RegisterStartupScript( typeof(Page), "js", "alert('上传成功');", true);
}
catch (Exception ex)
{
Page.ClientScript.RegisterStartupScript( typeof(Page), "js", "alert('" + ex.ToString() + "');", true);
}
proxy.Close();
}
}
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>
<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
,如需转载请自行联系原作者