dotnet调用delphi写的cgi

本文介绍如何使用Delphi开发WebService并通过.NET调用。主要包括创建Delphi WebService项目、部署到IIS以及在.NET环境中添加Web引用并调用的具体步骤。
作  者: ilovedotnetthandelph ()
等  级: 
信 誉 值: 100
所属社区: Delphi 网络通信/分布式开发
问题点数: 0
回复次数: 1
发表时间: 2006-5-11 12:31:14

最近写了一个关于dotnet调用delphi写的cgi的程序,参考了网上很多网友发的帖子,这里我将大家的内容加上我自己的心得写一篇,让后来像我这样的小虾少走点弯路。

一、用Delphi开发Web Service程序
打开Delphi7.0的开发环境,依次选择New | Other | WebServices | SOAP Server Application,然后选择CGI Stand-alone executable,系统将自动包含进开发Web Service所需要的类。在弹出的confirm窗口中点击确定,并在Add new services窗口中输入services name:test,系统自动生成工程,将新建工程保存为testservices。
编辑文件testImpl、testIntf生成调用接口,编辑后的文件如下,其中红色字体为自己添加的代码,蓝色字体是申明的接口。
testIntf:
{ Invokable interface Itest }

unit testIntf;

interface

uses InvokeRegistry, Types, XSBuiltIns;

type

  { Invokable interfaces must derive from IInvokable }
  Itest = interface(IInvokable)
  ['{FE47D43F-59DF-4AF9-ACD4-BA3882C01B31}']

    { Methods of Invokable interface must not use the default }
    { calling convention; stdcall is recommended }
    function gettext():widestring;stdcall;//申明在自定义接口中所定义的方法
  end;

implementation


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

end.

testImpl:
{ Invokable implementation File for Ttest which implements Itest }

unit testImpl;

interface

uses InvokeRegistry, Types, XSBuiltIns, testIntf;

type

  { Ttest }
  Ttest = class(TInvokableClass, Itest)
  public
   function gettext():widestring;stdcall;//申明在自定义接口中所定义的方法
  end;

implementation
function Ttest.gettext: widestring; stdcall;//实现自定义方法
begin
Result:='Success';
end;

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

end.

这样就生成一个返回字符串类型的接口方法。
二、把服务程序放在IIS Web服务器上提供给各种客户程序调用。
在IIS的默认站点里面新建一个虚拟目录delphi-cgi,并将该虚拟目录的执行权限设置为“脚本和可执行文件”以使该目录具有执行ISAPI扩展的权限,将虚拟目录指向该工程的文件夹。
三、在dotnet中调用该webservies。
在dotnet中添加web引用,在添加web引用窗口的URL栏中填入delphi的webservies的URL地址:http://localhost/delphi-cgi/testservices.exe/wsdl/Itest(如图1),点击转到,系统会自动发现接口(如图2),在web引用名栏中输入testservices,点击添加引用,系统将添加该引用(如图3)。
在WebForm1.aspx上添加button、textbox,在WebForm1.aspx.cs中添加代码:
using WebService1. testservices;
在事件Button1_Click中添加代码:
WebService1.testservices.Itestservice tt=new WebService1.testservices.Itestservice();
TextBox1.Text=tt.gettext();
运行程序在文本框中会显示字符“success”。

图看不到。

### 如何从 .NET 环境中调用 Python 代码 在 IronPython 的帮助下,可以从 .NET 环境中轻松调用 Python 代码。IronPython 是一种针对 .NET 平台设计的 Python 实现方式,它允许开发者利用 .NET 库生态系统的功能[^2]。 以下是具体方法: #### 使用 `ScriptEngine` `ScriptScope` 可以通过创建一个 `ScriptEngine` 对象来加载并执行 Python 代码。以下是一个简单的 C# 示例,展示如何运行一段 Python 代码并将结果返回到 .NET 中: ```csharp using System; using IronPython.Hosting; using Microsoft.Scripting.Hosting; class Program { static void Main() { ScriptEngine engine = Python.CreateEngine(); ScriptSource source = engine.CreateScriptSourceFromString( "def add(a, b):\n" + " return a + b\n", Microsoft.Scripting.SourceCodeKind.Statements); ScriptScope scope = engine.CreateScope(); source.Execute(scope); dynamic addFunction = scope.GetVariable("add"); Console.WriteLine(addFunction(5, 7)); // 输出 12 } } ``` 上述代码展示了如何定义一个名为 `add` 的函数,并将其作为动态对象传递给 C# 主程序以供进一步使用。 #### 处理异常传播机制 当从 .NET 调用 Python 代码时,需要注意异常处理的方式。由于 IronPython 基于 .NET 的异常机制实现了 Python 的异常模型,因此可以捕获由 Python 抛出的异常并在 .NET 层面进行处理[^1]。例如,在上面的例子中,如果 Python 函数抛出了错误,则可以在 C# 中通过标准的 try-catch 结构捕捉这些异常。 #### 配置环境支持跨平台开发 对于希望在非 Windows 系统上部署的应用程序来说,可以选择两种主要的技术路线——.NET Core 或 Mono 来提供必要的运行时支持[^3]。这两种技术都能够在 Linux 上成功运行基于 IronPython 构建的应用程序。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值