创建独立的Web Server应用MySOAPServer.exe。
该程序使用了Delphi 7 企业版自带的 Indy 9中的IdHTTPWebBrokerBridg来创建独立的SOAP Web Service应用,无需IIS,Apache等Web服务器应用。
从 Delphi7\Source\Indy目录拷贝两个文件"IdHTTPWebBrokerBridge.pas" 和"IdCompilerDefines.inc"到你的工程目录。
从菜单File --> New --> Other中选择WebServices标签,选择SOAP Server Application图标, 点OK
新建 SOAP Server应用,选择Web App Debugger executable
在Class Name框中输入: Dummy
弹出确认对话框Create Interface for SOAP Module?,点Yes.
新建WebService,Service Name中输入: MySOAPWebService,Unit Identifier中会自动填入相同值,然后点OK。
View --> Project Manager打开项目管理器,展开Project(n).exe目录
选中MySOAPWebServiceImpl.pas按"DEL"键,弹出确认窗口“Remove MySOAPWebServiceImpl.pas?",点Yes
接下来问你 Save changes to MySOAPWebServiceImpl.pas?,点No(Impl.pas没有必要)
View --> Project Manager打开项目管理器,展开Unit1目录,双击Unit1.pas,删除initialization部分的代码:
initialization
TWebAppSockObjectFactory.Create('Dummy');
View --> Project Manager打开项目管理器,双击Form1,按F11激活对象观察器,将Name属性从Form1改为MySOAPServerForm
View --> Project Manager打开项目管理器,展开Unit2目录,双击WebModule2,按F11激活对象观察器,将Name属性从WebModule2改为MyWebModule
View --> Project Manager打开项目管理器,双击Unit2.pas,在initialization部分,对WebRequestHandler.WebModuleClass赋值时检查一下是否为nil
initialization
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := TMyWebModule;
File --> New --> Other,选择WebServices标签,选中SOAP Server Data Module图标 点Ok
在Soap Data Module Wizard向导窗口中输入Module Name: MyAppServerSoapDataModule
View --> Project Manager打开项目管理器,展开Unit3目录,双击Unit3.pas,
在 interface部分uses中加入MySOAPWebServiceIntf:
uses
SysUtils, Classes, InvokeRegistry, Midas, SOAPMidas, SOAPDm,
MySOAPWebServiceIntf;
在TMyAppServerSoapDM声明部分加入IMySOAPWebService
TMyAppServerSoapDataModule = class(TSoapDataModule,
IMyAppServerSoapDataModule, IAppServerSOAP, IAppServer,
IMySOAPWebService)
[注意结尾没有分号]
View --> Project Manager打开项目管理器,双击Unit1.pas
在interface部分uses中加入IdHTTPWebBrokerBridge
uses
SysUtils, Classes, Forms, IdHTTPWebBrokerBridge;
Add the private variable FWebBrokerBridge to the TForm1 declaration.
private
FWebBrokerBridge: TIdHTTPWebBrokerBridge;
在 implementation 部分uses中加入Unit2
uses
SockApp, Unit2;
View --> Project Manager打开项目管理器,双击MySOAPServerForm,按F11激活对象观察器,选Event标签,双击OnCreate事件,FormCreate方法加入下面代码:
// Create server.
FWebBrokerBridge := TIdHTTPWebBrokerBridge.Create(Self);
// Register web module class.
FWebBrokerBridge.RegisterWebModuleClass(TMyWebModule);
// Set default port.
FWebBrokerBridge.DefaultPort := 1024;
// Start server.
FWebBrokerBridge.Active := True;
按F11激活对象观察器,选Event标签,双击OnDestroy事件,FormDestroy方法加入下面代码:
// Stop server.
FWebBrokerBridge.Active := False;
// Free server component.
FreeAndNil(FWebBrokerBridge);
到此应该可以运行程序,它将侦听1024端口的SOAP请求,在浏览器中打开 http://localhost:1024/wsdl/IMySOAPWebService,会显示XML。
接下来在服务程序中加入SOAP方法。
View --> Project Manager打开项目管理器,双击Unit3.pas,在TMyAppServerSoapDataModule的声明部分加入webservice的publice方法声明
public
function HelloWorld(Param1: string): string; stdcall;
方法的实现代码为:
{ TMyAppServerSoapDataModule }
function TMyAppServerSoapDataModule.HelloWorld(Param1: string): string; stdcall;
begin
Result := 'Hello World. Your data: ' + Param1;
end;
View --> Project Manager打开项目管理器,双击MySOAPWebServiceIntf.pas,
在IMySOAPWebService中加入webservice方法的声明:
IMySOAPWebService = interface(IInvokable)
['{4BF42BCF-294B-4D28-93D8-8311FA8B190E}']
function HelloWorld(Param1: string): string; stdcall;
end;
现在你可以保存文件:
点File --> Save Project As,
保存Unit1,文件名为MySOAPServerFormU.pas
保存Unit2,文件名为MyWebModuleU.pas
保存MySOAPWebServiceIntf,文件名为MySOAPWebServiceIntf.pas
保存Unit3,文件名为MyAppServerSoapDataModuleU.pas
保存Project(n) As,文件名为 MySOAPServer.dpr
View --> Project Manager打开项目管理器,展开MySOAPServerFormU目录,双击MyFormU.pas,将Unit2引用改为MyWebModuleU.
uses
SockApp,
MyWebModuleU;
点File --> Save,进行保存。
现在你可以再次运行程序,它仍然将侦听1024端口的SOAP请求。