delphi 웹 서비스 서버 개발

4539 단어
delphi 웹 서비스 서버 개발
1) 참조 유닛
ueses  Web.WebReq,  IdHTTPWebBrokerBridge;
2) WebModule Unit1 유닛, 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.

  

좋은 웹페이지 즐겨찾기