使用DIME協議上傳檔案

來源:互聯網
上載者:User

 

        在某些Web Service的應用情境下,例如公文的傳送,在Web Service返回結果的同時將word文檔及其它附件返回,這時候可以使用DIME協議來進行檔案的傳輸。使用它來傳輸不需要經過SOAP訊息的序列化/還原序列化,有很高的效率。當然這裡要用到Web Services Enhancements (WSE) ,目前的最新版本為3.0。本文中所使用的版本為2.0sp2,有趣的是WSE的各個版本中的命令空間都有很大的變化。這一點的確有點讓人苦惱!在安裝WSE時推薦將Visual Studio Tools也安裝上,這樣會免去手工修改Web Service的Web.config檔案的工作。

本文的樣本下載:http://files.cnblogs.com/lcybest/DIMESample.rar
Web Service:
       首先要引用Microsoft.Web.Services2.dll,修改Web.config檔案,將下面這段配置添加進去:
    <webServices>
      <soapExtensionTypes>
        <add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0" />
      </soapExtensionTypes>
    </webServices>
  </system.web>
如果你安裝了WSE的Visual Studio工具,以上工作可以通過工具來實現。
下面的代碼示範了在ResponseSoapContext中加入DIME附件的實現:

  [WebMethod]
  public string GetDocument(string DocumentId)
  {
            if(DocumentId.Length==0)
    return "DocumentId can not be empty!";
   Attachment attach=new Attachment(Guid.NewGuid().ToString(),@"D:\test.doc");
   Microsoft.Web.Services2.ResponseSoapContext.Current.Attachments.Add(attach);

   return "SendOK";
  }

       我們使用一個windows應用程式來示範一下可以接收Web Service附件的用戶端
首先要將Microsoft.Web.Services2.dll引用到項目中,添加對Web Service的引用。此時如果安裝了WSE工具會自己動產生一個以“WSE”為結尾的代理類。在代碼中可以直接使用這個代理類。
        如果沒有安裝工具則需要手工修改Visual Studio產生的代理類,代理類預設是從System.Web.Services.Protocols.WebClientProtocol繼承的,在這裡要修改為從Microsoft.Web.Services2.WebServicesClientProtocol來繼承。

在我們用戶端中可以通過以下代碼來實現將Response中的檔案取出來儲存到檔案系統中:

        private void button1_Click(object sender, System.EventArgs e)
        {
            TalkServer.DataInterface client=new DIMEClient.TalkServer.DataInterface();
            string strvalue=client.GetDocument("test111");
            if(client.ResponseSoapContext.Attachments.Count==0)
            {
                MessageBox.Show("No Attachments in the webservice response!");
                return;
            }
            Microsoft.Web.Services2.Attachments.Attachment attach;
            attach=client.ResponseSoapContext.Attachments[0];
            byte[] buffer=new byte[attach.Stream.Length];
            client.ResponseSoapContext.Attachments[0].Stream.Read(buffer,0,buffer.Length);
            System.IO.FileStream stream=new System.IO.FileStream(@"C:\test.doc",System.IO.FileMode.Create);
            stream.Write(buffer,0,buffer.Length);
            stream.Flush();
            stream.Close();    
            if(strvalue=="SendOK")
                MessageBox.Show("Receive succeed");
            else
                MessageBox.Show("Receive fail");
            
    
        }

以上就是實現的全部過程。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.