delphi webservice服务端开发

delphi webservice服务端开发

1)引用单元

ueses
  Web.WebReq,
  IdHTTPWebBrokerBridge;

2)WebModuleUnit1单元,DELPHI WEBSERVICE向导自动生成的

if Web.WebReq.WebRequestHandler <> nil then
    Web.WebReq.WebRequestHandler.WebModuleClass := WebModuleUnit1.WebModuleClass;

3)创建indy通讯

var FServer: TIdHTTPWebBrokerBridge;
FServer := TIdHTTPWebBrokerBridge.Create(Self);
FServer.DefaultPort := port;
FServer.Bindings.Clear;
FServer.Active := True;

4)接口声明单元,DELPHI WEBSERVICE向导自动生成的。

unit WebservicesIntf;

interface

uses Soap.InvokeRegistry, System.Types;

type
  TGoods = class(TRemotable)
  private
    FGoodsId: String;
    FGoodsName: String;
  published
    property GoodId: String read FGoodsId write FGoodsId;
    property GoodName: String read FGoodsName write FGoodsName;
  end;

  TGoodsArray = array of TGoods;

  TClass1 = class(TRemotable)
  private
    FId: String;
    FName: UnicodeString;
  published
    property Id: String read FId write FId;
    property Name: String read FName write FName;
  end;

  TClass1Array = array of TClass1;

  { Invokable interfaces must derive from IInvokable }
  IynServices = interface(IInvokable)
  ['{91ACA512-4204-4FAF-A26D-05E265BEAA72}']

    { Methods of Invokable interface must not use the default }
    { calling convention; stdcall is recommended }
    function getClass1Array: TClass1Array; stdcall;
    function saveClass1Array(value: TClass1Array): Boolean; stdcall;
    function GetGoodsArray(AAccountNo, ASQL: string): TGoodsArray; stdcall;
    function GetGoods(AAccountNo, ASQL: string): string; stdcall;
  end;

implementation

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

end.

  5)接口实现单元,DELPHI WEBSERVICE向导自动生成的

{ Invokable implementation File for TTest which implements ITest }

unit WebservicesImpl;

interface

uses
  ynJsonSerial, System.SysUtils, uUnidacPool, uUnidac, uLog, Soap.InvokeRegistry,
  System.Types, WebservicesIntf;

type

  { TynServices }
  TynServices = class(TInvokableClass, IynServices)
  public
    function getClass1Array: TClass1Array; stdcall;
    function saveClass1Array(value: TClass1Array): Boolean; stdcall;
    function GetGoodsArray(AAccountNo, ASQL: string): TGoodsArray; stdcall;
    function GetGoods(AAccountNo, ASQL: string): string; stdcall;
  end;

implementation

{ TynServices }

function TynServices.getClass1Array: TClass1Array;
begin
  SetLength(Result, 2);
  Result[0] := TClass1.Create;
  Result[0].Id := '编号1';
  Result[0].Name := '名称1';
  Result[1] := TClass1.Create;
  Result[1].Id := '编号2';
  Result[1].Name := '名称2';
end;

function TynServices.GetGoods(AAccountNo, ASQL: string): string;
begin
  var pool: TUnidacPool := GetDBPool(AAccountNo);
  var js: TynJsonCross := TynJsonCross.Create;
  var dm: TUnidac := pool.Lock;
  try
    try
      dm.qry.Close;
      dm.qry.SQL.Clear;
      dm.qry.SQL.Add(ASQL);
      dm.qry.Open;
      Result := js.DataSetToJson(dm.qry);
    except
      on E: Exception do
        Log.WriteLog('TynServices.GetGoods ' + E.Message);
    end;
  finally
    js.Free;
    pool.Unlock(dm);
  end;
end;

function TynServices.GetGoodsArray(AAccountNo, ASQL: string): TGoodsArray;
//var
//  dbpool: TDBPool;
//  firedac: TynFiredac;
begin
//  dbpool := GetDBPool(AAccountNo);
//  firedac := dbpool.Lock;
//  try
//    try
//      firedac.FDQuery1.Close;
//      firedac.FDQuery1.SQL.Clear;
//      firedac.FDQuery1.SQL.Add(ASQL);
//      firedac.FDQuery1.Open;
//      SetLength(Result, firedac.FDQuery1.RecordCount);
//      firedac.FDQuery1.First;
//      while not firedac.FDQuery1.Eof do
//      begin
//        Result[firedac.FDQuery1.RecNo].GoodId := firedac.FDQuery1.FieldByName('goodsid').AsString;
//        Result[firedac.FDQuery1.RecNo].GoodName := firedac.FDQuery1.FieldByName('goodsname').AsString;
//        firedac.FDQuery1.Next;
//      end;
//    except
//      on E: Exception do
//      begin
//        Log.WriteLog('TynServices.GetGoodsArray ' + E.Message);
//      end;
//    end;
//  finally
//    firedac.FDQuery1.Close;
//    firedac.FDConnection1.Close;
//    dbpool.Unlock(firedac);
//  end;
end;

function TynServices.saveClass1Array(value: TClass1Array): Boolean;
begin
  Result := False;
end;

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

end.

  

 

你可能感兴趣的:(delphi webservice服务端开发)