使用delphi 10.2 开发linux 上的webservice

前几天做了linux下apache的开发,今天做一个linux 下的webservice ,以供客户端调用。

闲话少说,直接干。

新建一个工程。选other...,选择如图。

继续输入服务名

然后就生成对应的单元。

 增加linux 平台。

 

完善对应的单元代码

{ Invokable implementation File for Txaliontest which implements Ixaliontest }

unit xaliontestImpl;

interface

uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns, xaliontestIntf;

type

  { Txaliontest }
  Txaliontest = class(TInvokableClass, Ixaliontest)
  public

    function echoname(const Value:string):string; stdcall;
    function sumall(const Value:integer):integer; stdcall;

  end;

implementation

{ Txaliontest }

function Txaliontest.echoname(const Value: string): string;
begin
   result:='你好'+value;
end;

function Txaliontest.sumall(const Value: integer): integer;
var
  i:integer;
  isumall:integer;
begin
  isumall:=0;
  for i := 1 to value do
   isumall:=isumall+i;

  result:=isumall;
end;

initialization
{ Invokable classes must be registered }
   InvRegistry.RegisterInvokableClass(Txaliontest);
end.
{ Invokable interface Ixaliontest }

unit xaliontestIntf;

interface

uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns;

type

  { Invokable interfaces must derive from IInvokable }
  Ixaliontest = interface(IInvokable)
  ['{20590E4A-BF8C-41AE-A630-94D2DD38EEE1}']

    { Methods of Invokable interface must not use the default }
    { calling convention; stdcall is recommended }
      function echoname(const Value:string):string; stdcall;
    function sumall(const Value:integer):integer; stdcall;
  end;

implementation

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

end.

编译本工程。

哎呀,怎么出错了?

不要害怕,这是因为linux 上apache的引用没有加入。我们确认apache 已经在linux上安装成功。

那么我们在IDE 里面处理一下。

记住设以上的地方为True, 允许未定义的引用。

现在重新编译

哈哈,OK 了

现在的任务就是在发布这个apache 模块。

这一块的内容请参照前面的文章。

配置文件如图:

 

 

 

启动apache.

 在浏览器里面输入对应的地址,就可以显示webservice 的接口信息了。

 

 好了,服务端搞定了。我们做一个客户端来调用一下这个服务。

直接建一个vcl application .

然后选择WDSL 导入器。

 

输入对应的地址。

 系统会生成对应的接口文件

// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL     : http://192.168.1.66/xalionws/wsdl/Ixaliontest
// Encoding : utf-8
// Version  : 1.0
// (2017-4-8 21:33:51 - - $Rev: 90173 $)
// ************************************************************************ //

unit Ixaliontest1;

interface

uses Soap.InvokeRegistry, Soap.SOAPHTTPClient, System.Types, Soap.XSBuiltIns;

type

  // ************************************************************************ //
  // The following types, referred to in the WSDL document are not being represented
  // in this file. They are either aliases[@] of other types represented or were referred
  // to but never[!] declared in the document. The types from the latter category
  // typically map to predefined/known XML or Embarcadero types; however, they could also
  // indicate incorrect WSDL documents that failed to declare or import a schema type.
  // ************************************************************************ //
  // !:int             - "http://www.w3.org/2001/XMLSchema"[]
  // !:string          - "http://www.w3.org/2001/XMLSchema"[]

  // ************************************************************************ //
  // Namespace : urn:xaliontestIntf-Ixaliontest
  // soapAction: urn:xaliontestIntf-Ixaliontest#%operationName%
  // transport : http://schemas.xmlsoap.org/soap/http
  // style     : rpc
  // use       : encoded
  // binding   : Ixaliontestbinding
  // service   : Ixaliontestservice
  // port      : IxaliontestPort
  // URL       : http://192.168.1.66/xalionws/soap/Ixaliontest
  // ************************************************************************ //
  Ixaliontest = interface(IInvokable)
  ['{A3FB1A48-1AE7-B449-16DC-42CCE1A48832}']
    function  echoname(const Value: string): string; stdcall;
    function  sumall(const Value: Integer): Integer; stdcall;
  end;

function GetIxaliontest(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): Ixaliontest;

implementation
  uses System.SysUtils;

function GetIxaliontest(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): Ixaliontest;
const
  defWSDL = 'http://192.168.1.66/xalionws/wsdl/Ixaliontest';
  defURL  = 'http://192.168.1.66/xalionws/soap/Ixaliontest';
  defSvc  = 'Ixaliontestservice';
  defPrt  = 'IxaliontestPort';
var
  RIO: THTTPRIO;
begin
  Result := nil;
  if (Addr = '') then
  begin
    if UseWSDL then
      Addr := defWSDL
    else
      Addr := defURL;
  end;
  if HTTPRIO = nil then
    RIO := THTTPRIO.Create(nil)
  else
    RIO := HTTPRIO;
  try
    Result := (RIO as Ixaliontest);
    if UseWSDL then
    begin
      RIO.WSDLLocation := Addr;
      RIO.Service := defSvc;
      RIO.Port := defPrt;
    end else
      RIO.URL := Addr;
  finally
    if (Result = nil) and (HTTPRIO = nil) then
      RIO.Free;
  end;
end;

initialization
  { Ixaliontest }
  InvRegistry.RegisterInterface(TypeInfo(Ixaliontest), 'urn:xaliontestIntf-Ixaliontest', 'utf-8');
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(Ixaliontest), 'urn:xaliontestIntf-Ixaliontest#%operationName%');

end.

在主窗体放两个按钮。

 

代码如下

implementation

{$R *.dfm}

uses Ixaliontest1;

procedure TForm2.Button1Click(Sender: TObject);
begin
  showmessage(  GetIxaliontest.echoname('xalion'));
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  showmessage(  GetIxaliontest.sumall(100).tostring);
end;

编译运行。

 

 

 

可以看见,客户端很顺利的调用了服务器端的函数。

我们今天的任务完成了。

 

时间: 2024-07-29 06:03:15

使用delphi 10.2 开发linux 上的webservice的相关文章

使用delphi 10.2 开发linux 上的Daemon

   delphi 10.2 支持linux, 而且官方只是支持命令行编程,目地就是做linux 服务器端的开发. 既然是做linux服务器端的开发,那么普通的命令行运行程序,然后等待开一个黑窗口的方式就 太low了(目前就有个别语言大咖,经常在Windows 上开个黑窗口,看起来非常恶心),那么如果 避免这个尴尬的问题?     其实Linux 下也有类似windows 服务的功能,Linux Daemon 就是其中的一种方式,命令行运行后 直接返回,同时在后台建立一个同样的进程.接受客户端的

应该在Linux上使用的10种云解决方案

不久前,为用户提供一种备份远程机器上数据的简易方法还很稀奇.现在,我们已觉得这理所当然.Dropbox及其他公司简化了这项任务.苹果.谷歌和微软都提供各自的数据备份方法. 在Linux上,情况有点不一样.发行版并不提供各自的云服务来管理你的数据(不过Ubuntu过去拥有Ubuntu One).一些主流发行版并不提供尚可的Linux客户软件. 但是你并非不走运.许多流行的服务确实可以在Linux下使用.你还有办法可以部署自己的解决方案,对数据获得控制权. 专有服务 大多数商用的云存储服务依赖闭源代

超简单易用的 “在 pcduino 开发板上写 Linux 驱动控制板载 LED 的闪烁”

这里转载一篇 linux 下的驱动程序开发的很基础和有用的文章 在pcduino开发板上写驱动控制板载LED的闪烁 ,实际是一个linux的驱动,该篇文章基础且够用:后续找到 android 下的驱动开发相关文章,再补充进来,希望该文作者能再接再励,感谢于先. 这里用 原创 模式,以便能推荐给更多的爱好者,转载是无法推荐的,敬请谅解. 以下仅是对原作者文章的整版复制,由于工作较忙,尚无时间细整理其中的代码,急用的可通过上面的链接跳转至原作者博客.      由于关于pcduino的资料比较少,所

如何在64位版本Linux上开发运行32位应用程序

最近换了Linux系统,由i686换成了x86-64,导致在进行开发的时候出用不了原来SDK中32位的开发工具.于是,博主找到如下文章,博主亲测实用: 如何在64位版本Linux上开发运行32位应用程序  内容如下: 很多程序员(特别是别的公司的)跟我抱怨说他们32位软件无法在我们的64位Linux系统上正常运行,而在他们32位机上正常,其实这个很好解决,一般 是64位系统安装后没有默认安装glibc的32位版本,通过简单的执行以下命令即可实现在64位Linux系统上开发运行32位应用程序,而不

使用epoll在linux上开发高性能应用服务器

概述 epoll是linux提供一种多路复用的技术,类似各个平台都支持的select,只是epoll在内核的实现做了 更多地优化,可以支持比select更多的文件描述符,当然也支持 socket这种网络的文件描述符.linux上 的大并发的接入服务器,目前的实现方式肯定都通过epoll实现. epoll和线程 有很多开发人员用epoll的时候,会开多个线程来进行数据通信,比如一个线程专门accept(我个人早 些年在FreeBSD用kqueue的时候,由于对内部机制没有基本了解也这样搞),一个线

10年资深架构师谈Linux上容器背后的虚拟化解决方案

写在前面   目前主流的虚拟化技术有类似Intel VT这样的纯粹底层硬件虚拟化技术,也有类似Xen这样的半虚拟化技术,它并不是一个真正的虚拟机,而是相当于自己运行了一个内核的实例,可以自由的加载内核模块,虚拟的内存和IO,稳定而且可预测.   也有kvm这样的完全虚拟化技术,所有的kvm类型的虚拟技术都可以装各种linux的发行版和各 种win的发行版,当然,还有最近比较流行的容器类技术,例如lxc,docker都是基于操作系统之上的进程级别的隔离技术,本次分享主要围绕上层的虚拟化技术来剖析L

如何在Linux上构建 RAID 10阵列

如何在Linux上构建 RAID 10阵列 RAID 10阵列(又名RAID 1+0 或先镜像后分区)通过结合RAID 0 (读写操作在多个磁盘上同时并行执行)和RAID 1(数据被完全相同地写入到两个或更多的磁盘)两者的特点实现高性能和高容错性的磁盘I/O. 这篇文章会指导你如何使用五块相同的8GB磁盘来组成一个软件RAID 10阵列.因为组成一个RAID 10阵列至少需要4块磁盘(比如,两个镜像各有一对分区组合),而且需要添加一块额外的备用磁盘以防某块主要的磁盘出错.本文也会分享一些工具,在

c c++-现在Linux上的C/C++程序开发的就业前景如何?

问题描述 现在Linux上的C/C++程序开发的就业前景如何? 请有心人,详细讲下现!在Linux上的C/C++程序开发的就业前景如何?马上要毕业的本科生,出来在各个地区的工资状况如何?....谢谢 解决方案 毕业生就业,第一次选择至关重要.关系到未来的前途,因为是毕业生嘛!是新手,公司都会择优选,淘汰率很高的,你要是被公司放弃了.就没毕业生的优势了.所以尽力找中等的公司,寻找离家近的.心里心里有依靠 解决方案二: 语言不重要,思想才是王道.

嵌入式linux-怎样在开发板Linux上加载文件系统

问题描述 怎样在开发板Linux上加载文件系统 我已经将Linux内核移植到picozed 7015开发板,但是只有一些简单的命令,像ls,cd等,make 等都没有.我怀疑是文件系统没有加载的原因,不知道我的想法是否正确? 另外,怎么加载文件系统呢? 解决方案 不可能没有文件系统,你说的应该是bash(shell) 解决方案二: 加载文件系统需要先编译成 img, 再mount 挂载 解决方案三: linux 加载usb文件系统