這幾天,光忙著寫程序員職場了,對(duì)wse的翻譯和學(xué)習(xí)沒有太大的進(jìn)展,這可不太好,畢竟咱還是靠技術(shù)混飯吃的,荒廢了技術(shù)怎么行!今天再上一盤有關(guān)wse的小菜,目的是想用wse實(shí)現(xiàn)大數(shù)據(jù)的傳輸。
jillzhang jillzhang@126.com
這幾天,光忙著寫程序員職場了,對(duì)wse的翻譯和學(xué)習(xí)沒有太大的進(jìn)展,這可不太好,畢竟咱還是靠技術(shù)混飯吃的,荒廢了技術(shù)怎么行!今天再上一盤有關(guān)wse的小菜,目的是想用wse實(shí)現(xiàn)大數(shù)據(jù)的傳輸。
在wse框架結(jié)構(gòu)中,能夠使用Message Transmission Optimization Mechanism (MTOM) 協(xié)議來傳輸大數(shù)據(jù),這在前文的翻譯中也有提到,詳細(xì)內(nèi)容可以參見WSE 3.0 文檔翻譯:WSE的新功能 中的介紹。本文就是對(duì)用MTOM傳輸大數(shù)據(jù)做一個(gè)項(xiàng)目示例,主要解決用web service上傳,下載文件的問題。
實(shí)現(xiàn)本示例,需要三步:
1) 創(chuàng)建asp.net xml web服務(wù)項(xiàng)目,并對(duì)其進(jìn)行配置編碼
2) 創(chuàng)建一個(gè)web服務(wù)的客戶端應(yīng)用程序,對(duì)其進(jìn)行配置編碼
3) 準(zhǔn)備好測試文件,運(yùn)行客戶端程序,查看運(yùn)行結(jié)果
-
創(chuàng)建asp.net xml web服務(wù)項(xiàng)目,并對(duì)其進(jìn)行配置編碼
通過項(xiàng)目模板,創(chuàng)建一個(gè)傳統(tǒng)的asp.net xml web service網(wǎng)站工程,在解決方案資源管理中右鍵點(diǎn)擊該工程,選擇wse 3.0 settting,進(jìn)行如下圖所示的操作
此操作可以為項(xiàng)目添加對(duì)Microsoft.Web.Services3的引用,為項(xiàng)目能使用wse 3.0框架做出第一步設(shè)置
添加引用之后,我們需要設(shè)置網(wǎng)絡(luò)服務(wù)能夠使用MTOM協(xié)議來進(jìn)行消息傳輸,方法如下圖所示,仍通過點(diǎn)擊wse 3.0 settting,然后進(jìn)行如下的配置
這里我們可以將Server Mode設(shè)置為always或者optional,二者的區(qū)別如下:
Optional(可選的): 不管經(jīng)Wse處理的請(qǐng)求消息是否經(jīng)過MTOM編碼,Soap響應(yīng)或者Soap錯(cuò)誤都是經(jīng)過MTOM編碼的
Always(總是):經(jīng)wse處理的請(qǐng)求和響應(yīng)一定要是MTOM編碼的
經(jīng)過這個(gè)操作,會(huì)在web.config中增加如下的配置信息:
在Service.asmx中添加代碼

Service端
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService


{

public Service ()
{
}

[WebMethod]

public byte[] Send(string fileName)
{
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
if (!File.Exists(filePath))

{
throw new Exception(string.Format("文件:{0}不存在",filePath));
}
byte[] buffer = File.ReadAllBytes(filePath);
return buffer;
}
[WebMethod]
public bool Receive(byte[] buffer,string fileName)

{
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
File.WriteAllBytes(filePath, buffer);
return false;
}
}

-
創(chuàng)建一個(gè)web服務(wù)的客戶端應(yīng)用程序,對(duì)其進(jìn)行配置編碼
創(chuàng)建一個(gè)console程序,名為BinaryMTOMClient,也按1中所示添加Microsoft.Web.Services3的引用,然后設(shè)置MTOM選項(xiàng),如下圖所示:
將Client Mode設(shè)置為On表示客戶端允許將Soap消息進(jìn)行MTOM編碼。此操作會(huì)在app.config中增加如下配置信息:
添加對(duì)上面1中創(chuàng)建的網(wǎng)絡(luò)服務(wù)的web引用,然后將Programe代碼更改如下:

Client端
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace BinaryMTOMClient


{
public class Program:WseBase.AppBase

{

localhost.ServiceWse serviceProxy;
public Program()

{
serviceProxy = new BinaryMTOMClient.localhost.ServiceWse();
ConfigureProxy(serviceProxy);
}
static void Main(string[] args)

{
Program p = new Program();
string fileName = "1.txt";
p.Download(fileName);
fileName = "2.txt";
p.Upload(fileName);
Console.Read();
}
public void Download(string fileName)

{
try

{
Console.WriteLine("開始調(diào)用網(wǎng)絡(luò)服務(wù):"+serviceProxy.Url);
byte[] buffer = serviceProxy.Send(fileName);
Console.WriteLine("接收到未加密的數(shù)據(jù)");
Console.WriteLine("數(shù)據(jù)長度為:"+buffer.Length.ToString());
File.WriteAllBytes(fileName, buffer);
}
catch (Exception ex)

{
Console.WriteLine(ex.Message);
}
}
public void Upload(string fileName)

{
try

{
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
if (!File.Exists(filePath))

{
Console.WriteLine(string.Format("文件:{0}不存在", filePath));
return;
}
Console.WriteLine("開始調(diào)用網(wǎng)絡(luò)服務(wù):" + serviceProxy.Url);
byte[] buffer = File.ReadAllBytes(filePath);
serviceProxy.Receive(buffer, fileName);
Console.WriteLine("發(fā)送未加密的數(shù)據(jù)");
Console.WriteLine("數(shù)據(jù)長度為:" + buffer.Length.ToString());
}
catch (Exception ex)

{
Console.WriteLine(ex.Message);
}
}
}
}

-
準(zhǔn)備好測試文件,運(yùn)行客戶端程序,查看運(yùn)行結(jié)果
在網(wǎng)絡(luò)服務(wù)的根目錄中,添加1.txt作為客戶端要下載的文件資源,在客戶端運(yùn)行目錄中添加2.txt,作為客戶端要上傳的文件資源,按下圖所示,運(yùn)行客戶端:
瀏覽網(wǎng)絡(luò)服務(wù)根目錄,新增了文件2.txt,而客戶端運(yùn)行目錄,新增了文件1.txt,表示示例成功
-
示例項(xiàng)目:示例文件