一、Web Service程序创建
1. 新建Web Service工程,选择SOAP ServerApplication.
2. 选择Web AppDebugger executable,Class Name中添入类名“Demo1”.
3. 单击“Yes” 按钮.
4. Service name中添入“Main” .
5. 在”MainIntf”单元”IMain”接口下增加函数”functionGetMsg(AMsg: string): String; stdcall;”
代码如下:
{ Invokable interfaceIMain }
unit MainIntf;
interface
usesInvokeRegistry, Types, XSBuiltIns;
type
{ Invokable interfaces must derive fromIInvokable }
IMain = interface(IInvokable)
['{4074B47F-D452-46DD-9453-F51A82442D4E}']
function GetMsg(AMsg: string): String;stdcall;
{ Methods of Invokable interface must notuse the default }
{ calling convention; stdcall isrecommended }
end;
implementation
initialization
{ Invokableinterfaces must be registered }
InvRegistry.RegisterInterface(TypeInfo(IMain));
end.
6. 在”MainImpl”单元”Main”接口下增加函数”functionGetMsg(AMsg: string): String; stdcall;”
代码如下:
{ Invokableimplementation File for TMain which implements IMain }
unit MainImpl;
interface
usesInvokeRegistry, Types, XSBuiltIns, MainIntf;
type
{ TMain }
TMain = class(TInvokableClass, IMain)
public
function GetMsg(AMsg: string): String;stdcall;
end;
implementation
{ TMain }
functionTMain.GetMsg(AMsg: string): String;
begin
Result := 'Get:' + AMsg;
end;
initialization
{ Invokableclasses must be registered }
InvRegistry.RegisterInvokableClass(TMain);
end.
7. 先运行程序,然后运行Web App Debugger工具,打开方法为Delphi菜单Tools->Web AppDebugger.
8. 打开菜单Server->Options.
Port为Web Service 端口号,Default URL填写为工程名.步骤2中填写的类名.本例中为“Project1.Demo1”.点击”OK”按钮保存.
9. 点击”Start”按钮运行Web Service, 点击Web Service的URL Default URL : http://localhost:8081/Project.Demo1,看到页面
则说明Web Service运行正常.
二、Web Service 调用
1. 新建VCL FormsApplication 工程.
2. 新建WSDL Importer 单元.
3. 输入Web ServiceURL地址,点击”Next”至生成单元文件.
4. 在使用Web Service的窗体引用生成的单元文件,通过Get函数获取对象就可以使用了
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants,Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses IMain1;
{$R *.dfm}
procedureTForm1.Button1Click(Sender: TObject);
var
Main:IMain;
begin
Main:=GetIMain();
ShowMessage(Main.GetMsg('Test'));
end;
end.