<a href="http://webabcd.blog.51cto.com/1787395/343999" target="_blank">[索引頁]</a>
化零為整WCF(7) - 消息處理(使用消息傳輸優化機制 - MTOM)
介紹
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
/// Mtom類
public class Mtom : IMtom
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>
源檔案:
<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" />
</asp:Content>
Mtom.aspx.cs
using System.Collections;
using System.Configuration;
using System.Data;
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;
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();
<client>
<!--address - 服務位址-->
<!--binding - 通信方式-->
<!--contract - 服務契約-->
<!--bindingConfiguration - 指定相關的綁定配置-->
<!--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>
<!--sendTimeout - 在傳輸引發異常之前可用于完成寫入操作的時間間隔-->
<binding name="MtomBindingConfiguration" messageEncoding="Mtom" sendTimeout="00:10:00">
<endpointBehaviors>
<behavior name="MtomEndpointBehavior">
<!--clientVia - 建立傳輸通道的 URI (tcpTrace抓soap的時候用)-->
<clientVia viaUri="http://localhost:8888/ServiceHost/Message/Mtom.svc" />
</endpointBehaviors>
運作結果:
上傳檔案後提示上傳成功
OK
<a href="http://down.51cto.com/data/100781" target="_blank">[源碼下載下傳]</a>
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/344124,如需轉載請自行聯系原作者