MTOM檔案的續傳功能(三) Client 端程式介紹

使用WSE 3.0 MTOM的技術,讓Web Service也可以傳檔案,而且傳輸更有效率,這篇將帶到Client端的部份說明.

  上一篇寫了MTOM Server端的部份,這次要寫的就是Client端的程式要怎麼跟Server互動,也就是最後一篇,廢話不多說,馬上進入正題,Client端的程式跟Server端一樣,要用WSE的Configuration Tool來設定,而Web Service是存在Web.Config,那Client端的WinForm就是存在app.config

?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MTOMClient.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MTOMClient.MTOMSet" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
    <section name="microsoft.web.services3" type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <applicationSettings>
    <MTOMClient.Properties.Settings>
      <setting name="MTOMClient_MTOMWS_ServiceMTOM" serializeAs="String">
        <value>http://localhost:2448/MTOMWebService/ServiceMTOM.asmx</value>
      </setting>
    </MTOMClient.Properties.Settings>
  </applicationSettings>
  <userSettings>
    <MTOMClient.MTOMSet>
      <setting name="FolderPath" serializeAs="String">
        <value>c:\temp</value>
      </setting>
    </MTOMClient.MTOMSet>
  </userSettings>
  <system.web>
    <webServices>
      <soapExtensionImporterTypes>
        <add type="Microsoft.Web.Services3.Description.WseExtensionImporter, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </soapExtensionImporterTypes>
    </webServices>
    <compilation>
      <assemblies>
        <add assembly="Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
  <microsoft.web.services3>
    <messaging>
      <mtom clientMode="On" />
    </messaging>
  </microsoft.web.services3>
</configuration>

因為有Tool可以幫忙,裡面的設定就不用一個一個打上去了.

開始步驟跟Web Service的做法一樣,開啟app.config,並做以下的設定.

 

做完上述設定,app.config就大致上完成了,接著下來就要要將Web Service加入參考,原本這是個很平常的動作,但有一點地方要注意一下,加入後的Web Service,要看一下它的Reference.cs.

反藍的那一段必需為Microsoft.Web.Services3.WebServicesClientProtocol

系統預設會是System.Web.Services.Protocols.SoapHttpClientProtocol

這點要注意到,它會導致錯誤.

這樣就完成大部份的設定工作,接下來就是Coding部份,在我們new Web Service時,有個屬性要記得設,RequireMtom = true;

接下來就是呼叫Web Service的Method來進行上/下載,其實設定到這裡,已經完成了大半,而呼叫Web Service的動作就像我們在呼叫一般的function一樣,沒有什麼特別的動作,比如說下載.

如此就可以從Web Service取得到檔案的buffer,再將buffer用FileStream寫到檔案即可,上傳也是一樣,用FileStream讀取本端的檔檔到buffer,再呼叫Web Service把這Buffer給上傳過去即可.

在使用WSE這段時間以來,發現它還不錯用,在用SSL時有遇到一點小問題,但後來也是解決了,但WSE 3.0後就沒有再出下一版了,因為它整合到WCF內,未來有機會再來看看WCF版的傳檔.

這次程式碼的介紹部份不多,因為有點小長,主要是因為用backgroundworker,及浮動式依傳輸速率去調整Buffer的長度,如果走固定又不用backgroundworker,程式碼就簡單許多,所以這次放上整個Source Code,給有興趣的朋友參考,這個就包含Web Service及Client端的程式.

 

原始碼下載 :

MTOM.rar