[索引页]
[×××]
作者: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
{
///
/// IMtom接口
///
[ServiceContract]
public interface IMtom
{
///
/// 上传文件
///
/// "path">文件目标路径
/// "fileData">文件字节数组
[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
{
///
/// IMtom接口
///
[ServiceContract]
public interface IMtom
{
///
/// 上传文件
///
/// "path">文件目标路径
/// "fileData">文件字节数组
[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
{
///
/// Mtom类
///
public class Mtom : IMtom
{
///
/// 上传文件
///
/// "path">文件目标路径
/// "fileData">文件字节数组
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
{
///
/// Mtom类
///
public class Mtom : IMtom
{
///
/// 上传文件
///
/// "path">文件目标路径
/// "fileData">文件字节数组
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
3、客户端
Mtom.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Mtom.aspx.cs"
Inherits="Message_Mtom" Title="消息处理(使用消息传输优化机制 - MTOM)" %>
源文件:
上传路径:
Inherits="Message_Mtom" Title="消息处理(使用消息传输优化机制 - MTOM)" %>
MTOM(Message Transmission Optimization Mechanism) - 消息传输优化机制
- 可以指定用 MTOM 还是 Text 对 SOAP 消息编码
- 抓soap消息的时候可以用tcpTrace
- 用17,766,901字节大小的文件测试:Text编码(soap大小:31,591,929字节);MTOM编码(soap大小:23,696,066字节)
源文件:
上传路径:
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
运行结果:
上传文件后提示上传成功
OK
[×××]