使用 SOAP Toolkit 2.0 將現(xiàn)有代碼作為 Web 服務(wù)提供
2024-07-21 02:21:36
供稿:網(wǎng)友
使用 soap toolkit 2.0 將現(xiàn)有代碼作為 web 服務(wù)提供
使用 .net 建立分布式應(yīng)用程序
steve kirk 和 priya dhawan
microsoft developer network
摘要:本文介紹了使用 microsoft soap toolkit 版本 2.0 將現(xiàn)有 microsoft visual basic 6.0 代碼作為 web 服務(wù)提供時(shí)所進(jìn)行的數(shù)據(jù)轉(zhuǎn)換。
目錄
簡(jiǎn)介
現(xiàn)有代碼提供的數(shù)據(jù)類型
ado 2x command 對(duì)象
ado 2x recordset 對(duì)象
stream 對(duì)象
xmldom 對(duì)象
總結(jié)
簡(jiǎn)介
microsoft®soap toolkit 版本 2.0 簡(jiǎn)化了將現(xiàn)有代碼作為 web 服務(wù)提供和使用的任務(wù),msdn library 的 sdk 部分中的 soap toolkit 2.0 文檔(英文)對(duì)此進(jìn)行了說(shuō)明。在服務(wù)器端執(zhí)行的一些主要功能是,在現(xiàn)有代碼傳遞的不同數(shù)據(jù)類型的數(shù)據(jù)和 xml 消息(在 web 服務(wù)客戶機(jī)和服務(wù)器之間使用)之間進(jìn)行轉(zhuǎn)換。簡(jiǎn)單數(shù)據(jù)類型的轉(zhuǎn)換可以自動(dòng)處理,較為復(fù)雜的數(shù)據(jù)類型則需要開(kāi)發(fā)人員提供轉(zhuǎn)換代碼。
評(píng)估現(xiàn)有代碼是否適合于作為 web 服務(wù)提供時(shí),本文討論的數(shù)據(jù)轉(zhuǎn)換問(wèn)題并不是唯一需要考慮的問(wèn)題。應(yīng)考慮的其它因素包括對(duì)象和狀態(tài)模型、返回的數(shù)據(jù)大小、如何表示已經(jīng)成功、如何返回錯(cuò)誤信息、安全模型(包括訪問(wèn)控制、身份驗(yàn)證和加密)、執(zhí)行模型(同步或異步)、如何分發(fā)代碼,以及事務(wù)模型(com+ 事務(wù)或聲明型事務(wù)),等等。這些問(wèn)題將在即將發(fā)表的體系結(jié)構(gòu)主題(英文)文章中進(jìn)行討論。
現(xiàn)有代碼提供的數(shù)據(jù)類型
要介紹現(xiàn)有代碼傳遞的所有數(shù)據(jù)類型的轉(zhuǎn)換是一個(gè)相當(dāng)大的工程,因此本文介紹了某些最常用的數(shù)據(jù)類型。通過(guò) soap toolkit 代碼進(jìn)行轉(zhuǎn)換的一個(gè)替代方案是使用 xml 接口對(duì)現(xiàn)有代碼進(jìn)行擴(kuò)展。本文討論了以下數(shù)據(jù)類型的轉(zhuǎn)換方法:
ado 2x command 對(duì)象
ado2x recordset 對(duì)象
stream 對(duì)象
xmldom 對(duì)象
ado 2x command 對(duì)象
直接訪問(wèn)數(shù)據(jù)庫(kù)的現(xiàn)有代碼經(jīng)常會(huì)提供 microsoft activex® 數(shù)據(jù)對(duì)象 (ado) 的 command 對(duì)象。雖然不能在運(yùn)行于不同進(jìn)程中的應(yīng)用程序?qū)又g傳遞 command 對(duì)象,但可以在同一進(jìn)程內(nèi)傳遞該對(duì)象。對(duì)于單行數(shù)據(jù)實(shí)體,通過(guò) command 對(duì)象的輸出參數(shù)返回?cái)?shù)據(jù)比通過(guò) ado 記錄集返回?cái)?shù)據(jù)效率更高。因此,ado command 對(duì)象對(duì)于返回單行實(shí)體數(shù)據(jù)十分有用。
讀數(shù)據(jù)
以下示例中的現(xiàn)有代碼返回一個(gè) ado command 對(duì)象,它包含作為輸出參數(shù)的數(shù)據(jù)。在傳遞給 web 服務(wù)的客戶之前,custom type mapper 中的代碼使用 soapserializer 對(duì)象對(duì) command 對(duì)象進(jìn)行轉(zhuǎn)換:
with soapserializer
`轉(zhuǎn)換 commandtype
.startelement "commandtype"
.writestring cmd.commandtype
.endelement
`轉(zhuǎn)換 commandtext
.startelement "commandtext"
cmdtext = cmd.commandtext
cmdtext = left(cmd.commandtext, len(cmdtext) - 8)
cmdtext = right(cmdtext, len(cmdtext) - 7)
.writestring cmdtext
.endelement
`轉(zhuǎn)換 parameters 集合
.startelement "parameters"
for i = 0 to ocmd.parameters.count - 1
.startelement right(ocmd.parameters(i).name, _
len(ocmd.parameters(i).name) - 1)
.startelement "direction"
.writestring ocmd.parameters(i).direction
.endelement
.startelement "type"
.writestring ocmd.parameters(i).type
.endelement
.startelement "size"
.writestring ocmd.parameters(i).size
.endelement
.startelement "value"
.writestring ocmd.parameters(i).value
.endelement
.endelement
next
.endelement
end with
寫數(shù)據(jù)
將數(shù)據(jù)作為 command 對(duì)象的參數(shù)傳遞是一種非常有效的數(shù)據(jù)傳遞方法。它還可以進(jìn)行擴(kuò)展,并提供了一些類型檢查功能。
以下示例中,客戶提交給 web 服務(wù)的 xml 數(shù)據(jù)被轉(zhuǎn)換為 ado command 對(duì)象的參數(shù),它將被傳遞到現(xiàn)有代碼:
dim cmd as adodb.command
dim param as adodb.parameter
' pnode 是包含客戶提交的 xml 的 msxml2.ixmldomnode
' 實(shí)例化一個(gè) ado command 對(duì)象
set cmd = new adodb.command
with cmd
' 應(yīng)用 commandtype 和 commandtext
.commandtype = _
cint(pnode.selectsinglenode("commandtype").nodetypedvalue)
.commandtext = pnode.selectsinglenode("commandtext").nodetypedvalue
' 填充 parameters 集合
set nodeparent = pnode.selectsinglenode("parameters")
for i = 0 to nodeparent.childnodes.length - 1
set nodeparameter = nodeparent.childnodes(i)
set param = new adodb.parameter
with param
.name = "@" + nodeparameter.nodename
.direction = _
nodeparameter.selectsinglenode("direction").nodetypedvalue
.type = nodeparameter.selectsinglenode("type").nodetypedvalue
.size = nodeparameter.selectsinglenode("size").nodetypedvalue
.value = factory.getmapper(enxsdstring, _
nothing).read(nodeparameter.selectsinglenode("value"), _
bstrencoding, encodingmode, lflags)
end with
.parameters.append oparam
next
end with
ado 2x recordset 對(duì)象
ado 2x 斷開(kāi)連接的記錄集通常用于在多層應(yīng)用程序的各層之間傳遞數(shù)據(jù)。數(shù)據(jù)可以是單行、多行或分層次的行。
讀數(shù)據(jù)
本示例中,現(xiàn)有代碼返回一個(gè) ado recordset 對(duì)象,它包含的層次行數(shù)據(jù)將被轉(zhuǎn)換為 xml,然后再返回給客戶:
dim doc as msxml2.domdocument
set doc = new msxml2.domdocument
' 將記錄集數(shù)據(jù)寫入 xmldom
rs.save odoc, adpersistxml
' 將 xml 傳遞到 soap toolkit serializer
soapserializer.writexml doc.xml
寫數(shù)據(jù)
以下示例中,使用表示層次行數(shù)據(jù)的 xml 填充 ado recordset 對(duì)象,該對(duì)象將被傳遞到現(xiàn)有代碼:
dim rs as adodb.recordset
dim doc as msxml2.domdocument
set rs = new adodb.recordset
set doc = new msxml2.domdocument
' 將 xml 載入 xmldomdocument
doc.loadxml pnode.xml
' 使用來(lái)自 xmldomdocument 的 xml 填充記錄集
rs.open doc
stream 對(duì)象
流提供了一種在應(yīng)用程序的本地層之間傳遞數(shù)據(jù)的有效方法。它是從 microsoft sql server™2000 中讀取 xml 的主要方法。
讀數(shù)據(jù)
以下示例中,現(xiàn)有代碼返回表示層次行數(shù)據(jù)的 xml 流,它將被轉(zhuǎn)換,然后返回給客戶:
dim instream as adodb.stream
' pvar 包含現(xiàn)有代碼返回的流對(duì)象
set instream = pvar
' 將 xml 數(shù)據(jù)從流傳遞到 soap serializer
soapserializer.writestring instream.readtext
xmldom 對(duì)象
xmldom 對(duì)象是一種在多層應(yīng)用程序的本地層之間傳遞數(shù)據(jù)的好方法。它提供了接口可擴(kuò)展性、類型檢查和架構(gòu)驗(yàn)證功能。
讀數(shù)據(jù)
以下示例中,現(xiàn)有代碼返回一個(gè) xmldomdocument 對(duì)象,該對(duì)象將被轉(zhuǎn)換,然后返回給客戶:
`pvar 包含 xmldomdocument
psoapserializer.writexml pvar.xml
寫數(shù)據(jù)
以下示例中,使用表示層次行數(shù)據(jù)(由客戶提交)的 xml 填充 xmldom 對(duì)象,然后將該對(duì)象傳遞到現(xiàn)有代碼:
set odoc = new msxml2.domdocument
' 將 ixmldomnode xml 載入 domdocument 對(duì)象
`pnode 包含 ixmldomnode 對(duì)象
odoc.loadxml pnode.childnodes(0).xml
總結(jié)
本文及附帶的示例介紹了有關(guān)數(shù)據(jù)轉(zhuǎn)換的信息。通過(guò)數(shù)據(jù)轉(zhuǎn)換,可以使用 soap toolkit 2.0 將現(xiàn)有代碼作為 web 服務(wù)提供。本文介紹了一些常用的接口對(duì)象。
這些解決方案的性能各異,并且受所傳遞的數(shù)據(jù)大小影響。在本系列后面的文章中,我們將對(duì)這些實(shí)現(xiàn)方法進(jìn)行比較。
評(píng)估現(xiàn)有代碼是否適合作為 web 服務(wù)時(shí),接口只不過(guò)是應(yīng)當(dāng)考慮的諸多因素之一。應(yīng)考慮的其它因素包括安全性(包括授權(quán)、身份驗(yàn)證和加密)、事務(wù)模型、狀態(tài)模型、返回錯(cuò)誤和結(jié)果的方式,以及代碼是同步還是異步執(zhí)行,等等。