SOAP(BizSnap)でオブジェクトを受け渡す

Delphi 6 Enterprise
SOAP (BizSnap) で、オブジェクトを受け渡す方法です。受け渡しに使うクラスが TRemotable を継承している点と、各プロパティが published スコープである点を気をつければいいでしょう。以下のサンプルはクラス TProduct のオブジェクトをサーバーに渡し、サーバーがそのオブジェクトのプロパティに値をセットします。

■サーバー側のインターフェース
{ 起動可能インターフェース IServiceTest }

unit ServiceTestIntf;

interface

uses InvokeRegistry, Types, XSBuiltIns;

type
  TProduct = class(TRemotable)
  private
    FVersion: integer;
    FName: string;
    FEdition: string;
  published
    property  Name: string  read  FName write FName;
    property  Version: integer  read  FVersion  write FVersion;
    property  Edition: string read  FEdition  write FEdition;
  end;

  { 起動可能インターフェースは IInvokable から継承される。}
  IServiceTest = interface(IInvokable)
  ['{8B537E5C-1CC7-4D25-B7C3-3AE99F7E8A5B}']
    procedure GetTest(var Value: TProduct); stdcall;
    { 起動可能インターフェースのメソッドにはデフォルトの呼び出し規約を }  
    { 使うことはできません。stdcall 呼び出しを推奨します。}
  end;

implementation

initialization
  { 起動可能インターフェースは登録する必要がある }
  
  InvRegistry.RegisterInterface(TypeInfo(IServiceTest));

end.

■サーバー側の実装
{ IServiceTest インターフェースを実装する TServiceTest の定義 }         

unit ServiceTestImpl;

interface

uses InvokeRegistry, Types, XSBuiltIns, ServiceTestIntf;

type

  { TServiceTest }
  TServiceTest = class(TInvokableClass, IServiceTest)
  public
    procedure GetTest(var Value: TProduct); stdcall;
  end;

implementation

{ TServiceTest }

procedure TServiceTest.GetTest(var Value: TProduct);
begin
  Value.Name      :=  'Delphi';
  Value.Version   :=  6;
  Value.Edition   :=  'Enterprise';
end;

initialization

  { 起動可能インターフェースは登録する必要がある }
  InvRegistry.RegisterInvokableClass(TServiceTest);

end.

■クライアント側の実装
implementation

{$R *.dfm}

uses                                                                 
  ServiceTestIntf, SOAPHTTPClient;

procedure TForm1.Button1Click(Sender: TObject);
var
  Product : TProduct;
  HTTPRio : THTTPRio;
  i: IServiceTest;
begin
  HTTPRio :=  THTTPRio.Create(nil);
  HTTPRio.URL :=  'http://localhost/soap/test01.exe/SOAP/';
  i :=  HTTPRio as IServiceTest;
  Product :=  TProduct.Create;
  i.GetTest(Product);
  Label1.Caption  :=  Product.Name;
  Label2.Caption  :=  IntToStr(Product.Version);
  Label3.Caption  :=  Product.Edition;
  Product.Free;
  HTTPRio.Free;
end;