DELPHI 6.02 抢先研究 -- BizSnap/SOAP/WebService 之四 - -- 补丁2#的意义

Borland发布了C++ Builder 6以及Delphi 6的第二个补丁。 这是一个对Delphi 6来说,非常重要的一个补丁,不但修正了Delphi 6中一些问题, 还在很大程度上增强了Delphi 6的功能,特别是在SOAP/Web Service开发方面。

上图为新的新建Web Service页,与打补丁之前相比(见《DELPHI 6 抢先研究 -- BizSnap/SOAP/WebService 之一 -- 一个 Hello world! 的例子》等), 除了将图标全部换掉(现在与C++ Builder 6相同)以外,还增加了一项: SOAP Server Interface向导,这是一个新建SOAP服务端接口的向导,在前面的介绍中, 新建一个SOAP服务端接口需要写不少代码,有了这个向导就可以省很多事。

因为有了SOAP Server Interface向导, 在新建一个Web Service应用时会自动询问是否产生一个服务端接口,如上图。 除了是要用SOAP进行多层应用开发以外,都是要新建接口的。 新建服务端接口的向导对话框如下图。

仿照《DELPHI 6 抢先研究 -- BizSnap/SOAP/WebService 之一 -- 一个 Hello world! 的例子》的例子,新建一个SoapHello接口,在Service Name中输入SoapHello,确定后即可产生两个单元:SoapHelloIntf.pas和SoapHelloImpl.pas,分别为此接口的接口定义和接口实现单元。

以下为SoapHelloIntf.pas单元的内容:

{ Invokable interface ISoapHello }

unit SoapHelloIntf;

interface

uses InvokeRegistry, Types, XSBuiltIns;

type

{ Invokable interfaces must derive from IInvokable }
ISoapHello = interface(IInvokable)
['{3A9E6BD6-F128-40AD-B9F1-FB254C463CCC}']

{ Methods of Invokable interface must not use the default }
{ calling convention; stdcall is recommended }
end;

implementation

initialization
{ Invokable interfaces must be registered }
InvRegistry.RegisterInterface(TypeInfo(ISoapHello));

end.

    以下为SoapHelloImpl.pas单元的内容:

{ Invokable implementation File for TSoapHello which implements ISoapHello }

unit SoapHelloImpl;

interface

uses InvokeRegistry, Types, XSBuiltIns, SoapHelloIntf;

type

{ TSoapHello }
TSoapHello = class(TInvokableClass, ISoapHello)
public
end;

implementation

initialization
{ Invokable classes must be registered }
InvRegistry.RegisterInvokableClass(TSoapHello);

end.

然后,只需要在这两个单元中加入所需的接口方法的定义和实现,即可完成一个服务端接口,较原来方便很多。 这还只是表面的变化,在本章第一个例子中,如果输入 http://localhost/soap/soaptest.dll 是看不到什么东西的,必须输入 http://localhost/soap/soaptest.dll/wsdl 才能看到所有接口的WSDL列表。但是在打了Delphi 6补丁2后重新编译此例子程序,可以看到 http://localhost/soap/soaptest.dll 显示一个漂亮的页面,与用Visual Studio.net进行SOAP开发类似。而 http://localhost/soap/soaptest.dll/wsdl 页面也与原来的不同。然而这还不是最主要的改变,看看 http://localhost/soap/soaptest.dll/wsdl/IsoapHello 页面,与前面的WSDL相比之下有了一些小小的改变,而这才是最重要的,这一改进终于让用Delphi 6开发的Web Service应用程序可以被Visual Studio.net开发的客户端程序调用了!下面就是新的WSDL文件:

<?xml version="1.0"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" name="IHelloservice"
targetNamespace="http://tempuri.org/" xmlns:tns="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<message name="GetHelloRequest">
<part name="aID" type="xs:int"/>
</message>
<message name="GetHelloResponse">
<part name="return" type="xs:string"/>
</message>
<portType name="ISoapHello">
<operation name="GetHello">
<input message="tns:GetHelloRequest"/>
<output message="tns:GetHelloResponse"/>
</operation>
</portType>
<binding name="IHellobinding" type="tns:IHello">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetHello">
<soap:operation soapAction="urn:HelloIntf-IHello#GetHello" style="rpc"/>
<input>
<soap:body use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:HelloIntf-IHello"/>
</input>
<output>
<soap:body use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:HelloIntf-IHello"/>
</output>
</operation>
</binding>
<service name="IHelloservice">
<port name="IHelloPort" binding="tns:IHellobinding">
<soap:address location="http://localhost:1024/WSDemo1.WSDemo1/soap/IHello"/>
</port>
</service>
</definitions>

新的WSDL Importer也有很大的变化。上图显示了新的WSDL Importer向导,较原来的(如《DELPHI 6 抢先研究 -- BizSnap/SOAP/WebService 之一 -- 一个 Hello world! 的例子》)要漂亮很多,当然这也还只是表面,单击"下一步",如果成功导入接口,即可立即看到生成的接口文件内容,如下图按"完成"就可以生成接口文件(注意:因为生成的接口文件名默认为接口名,但Delphi有一个限制就是单元名不可以与对象重名,所以必须改名后才能保存)。

当然,WSDL Importer的改进也不只是这些表面的东西, 最重要的改进也是它已经可以方便地导入用Visual Studio.net写的Web Service程序产生的WSDL,并可以顺利访问其接口。

最后值得一提的是: C++ Builder 6 的 SOAP 部分与 Delphi 6 的补丁2#相同, 如果在未打补丁2#的 Delphi 6 的同一台机器上装 C++ Builder 6 将导致 Delphi 6 的 SOAP 部分不可用, 解决办法就是打 Delphi 6 的补丁2#。