天天看點

化零為整WCF(7) - 消息處理(使用消息傳輸優化機制 - MTOM)

<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 

        /// &lt;summary&gt; 

        /// IMtom接口 

        /// &lt;/summary&gt; 

        [ServiceContract] 

        public interface IMtom 

        { 

                /// &lt;summary&gt; 

                /// 上傳檔案 

                /// &lt;/summary&gt; 

                /// &lt;param name="path"&gt;檔案目标路徑&lt;/param&gt; 

                /// &lt;param name="fileData"&gt;檔案位元組數組&lt;/param&gt; 

                [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

&lt;%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Message.Mtom" %&gt;

Web.config

&lt;?xml version="1.0"?&gt; 

&lt;configuration&gt; 

    &lt;system.serviceModel&gt; 

        &lt;services&gt; 

            &lt;!--name - 提供服務的類名--&gt; 

            &lt;!--behaviorConfiguration - 指定相關的行為配置--&gt; 

            &lt;service name="WCF.ServiceLib.Message.Mtom" behaviorConfiguration="MessageBehavior"&gt; 

                &lt;!--address - 服務位址--&gt; 

                &lt;!--binding - 通信方式--&gt; 

                &lt;!--contract - 服務契約--&gt; 

                &lt;!--bindingConfiguration - 指定相關的綁定配置--&gt; 

                &lt;endpoint address="" binding="wsHttpBinding" contract="WCF.ServiceLib.Message.IMtom" bindingConfiguration="MtomBindingConfiguration" /&gt; 

            &lt;/service&gt; 

        &lt;/services&gt; 

        &lt;behaviors&gt; 

            &lt;serviceBehaviors&gt; 

                &lt;behavior name="MessageBehavior"&gt; 

                    &lt;!--httpGetEnabled - 使用get方式提供服務--&gt; 

                    &lt;serviceMetadata httpGetEnabled="true" /&gt; 

                    &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt; 

                &lt;/behavior&gt; 

            &lt;/serviceBehaviors&gt; 

        &lt;/behaviors&gt; 

        &lt;bindings&gt; 

            &lt;wsHttpBinding&gt; 

                &lt;!--messageEncoding - 指定用 MTOM 還是 Text 對 SOAP 消息編碼--&gt; 

                &lt;!--maxReceivedMessageSize - 在采用此綁定配置的通道上可接收的最大消息大小(機關:位元組)--&gt; 

                &lt;!--receiveTimeout - 在傳輸引發異常之前可用于完成讀取操作的時間間隔--&gt; 

                &lt;binding name="MtomBindingConfiguration" messageEncoding="Mtom" maxReceivedMessageSize="1073741824" receiveTimeout="00:10:00"&gt; 

                    &lt;!--maxArrayLength - 配額控制:允許的最大數組長度--&gt; 

                    &lt;readerQuotas maxArrayLength="1073741824" /&gt; 

                &lt;/binding&gt; 

            &lt;/wsHttpBinding&gt; 

        &lt;/bindings&gt; 

    &lt;/system.serviceModel&gt; 

&lt;/configuration&gt;

3、用戶端

Mtom.aspx

&lt;%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Mtom.aspx.cs" 

        Inherits="Message_Mtom" Title="消息處理(使用消息傳輸優化機制 - MTOM)" %&gt; 

&lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"&gt; 

&lt;/asp:Content&gt; 

&lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"&gt; 

        &lt;p&gt; 

                MTOM(Message Transmission Optimization Mechanism) - 消息傳輸優化機制 

        &lt;/p&gt; 

        &lt;div&gt; 

                &lt;ul&gt; 

                        &lt;li&gt;可以指定用 MTOM 還是 Text 對 SOAP 消息編碼&lt;/li&gt; 

                        &lt;li&gt;抓soap消息的時候可以用tcpTrace&lt;/li&gt; 

                        &lt;li&gt;用17,766,901位元組大小的檔案測試:Text編碼(soap大小:31,591,929位元組);MTOM編碼(soap大小:23,696,066位元組)&lt;/li&gt; 

                &lt;/ul&gt; 

        &lt;/div&gt; 

                源檔案: 

                &lt;asp:FileUpload ID="file" runat="server" /&gt; 

                上傳路徑: 

                &lt;asp:TextBox ID="txtDestination" runat="server" Text="C:\"&gt;&lt;/asp:TextBox&gt; 

                &lt;asp:Button ID="btnUpload" runat="server" Text="上傳" OnClick="btnUpload_Click" /&gt; 

&lt;/asp:Content&gt;

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(); 

        &lt;client&gt; 

            &lt;!--address - 服務位址--&gt; 

            &lt;!--binding - 通信方式--&gt; 

            &lt;!--contract - 服務契約--&gt; 

            &lt;!--bindingConfiguration - 指定相關的綁定配置--&gt; 

            &lt;!--endpoint address="http://localhost:3502/ServiceHost/Message/Mtom.svc" binding="wsHttpBinding" contract="MessageSvc.Mtom.IMtom" bindingConfiguration="MtomBindingConfiguration" behaviorConfiguration="MtomEndpointBehavior" /--&gt; 

            &lt;endpoint address="http://localhost:3502/ServiceHost/Message/Mtom.svc" binding="wsHttpBinding" contract="MessageSvc.Mtom.IMtom" bindingConfiguration="MtomBindingConfiguration" /&gt; 

        &lt;/client&gt; 

                &lt;!--sendTimeout - 在傳輸引發異常之前可用于完成寫入操作的時間間隔--&gt; 

                &lt;binding name="MtomBindingConfiguration" messageEncoding="Mtom" sendTimeout="00:10:00"&gt; 

            &lt;endpointBehaviors&gt; 

                &lt;behavior name="MtomEndpointBehavior"&gt; 

                    &lt;!--clientVia - 建立傳輸通道的 URI (tcpTrace抓soap的時候用)--&gt; 

                    &lt;clientVia viaUri="http://localhost:8888/ServiceHost/Message/Mtom.svc" /&gt; 

            &lt;/endpointBehaviors&gt; 

運作結果:

上傳檔案後提示上傳成功 

OK

<a href="http://down.51cto.com/data/100781" target="_blank">[源碼下載下傳]</a>

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/344124,如需轉載請自行聯系原作者

繼續閱讀