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向导自动生成的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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向导自动生成的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{ 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.

  

 

posted @   delphi中间件  阅读(2436)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-04-07 咏南中间件支持JWT TOKEN
2014-04-07 多屏幕程序
点击右上角即可分享
微信分享提示