程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WCF示例(7) - 消息處理(使用消息傳輸優化機制 - MTOM)

WCF示例(7) - 消息處理(使用消息傳輸優化機制 - MTOM)

編輯:關於.NET

介紹

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" />
    &nbsp;
    上傳路徑:
    <asp:TextBox ID="txtDestination" runat="server" Text="C:\"></asp:TextBox>
    &nbsp;
    <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();
  }
}

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>

運行結果:

上傳文件後提示上傳成功

OK

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved