详解C# Webservice与Delphi交互

开发 后端
Delphi是目前小型C/S系统的最佳选择,本文向您介绍使用C# Webservice与Delphi交互开发的一些实例,希望对您有所帮助。

大家都知道C# Webservice技术的出现将各种开发技术和语言完全的融合了,下面就这种融合在C#和delphi之间的交互做一次全面的体现,前者是目前***的开发平台,后者依然是小型c/s系统的***选择.

1.使用C#创建一个Webservice服务。

使用vs2005的模板创建C# Webservice非常容易。原文件如下:

  1. [WebService(Namespace = \"http:  
  2. //localhost/webserver/\")]   
  3. [WebServiceBinding(ConformsTo =   
  4. WsiProfiles.BasicProfile1_1)]   
  5.     
  6. public class Service :   
  7. System.Web.Services.WebService   
  8. {   
  9. public Service () {   
  10. //如果使用设计的组件,请取消注释以下行    
  11. InitializeComponent();   
  12. }   
  13.    
  14. #region  Component  Designer    
  15. generated  code   
  16. private void InitializeComponent()   
  17. {   
  18.     
  19. }   
  20. //Web  服务设计器所必需的    
  21. private IContainer components = null;   
  22.  
  23. protected override void Dispose  
  24. (bool disposing)   
  25. {   
  26. if (disposing && components != null)   
  27. {   
  28. components.Dispose();   
  29. }   
  30. base.Dispose(disposing);   
  31. }   
  32. #endregion   
  33. //这个是自动生成的一个webservice函数,  
  34. 可以不要。   
  35. [WebMethod]   
  36. public string HelloWorld() {   
  37. return \"Hello World\";   
  38. }   
  39. //这个才是我们自己创建的,   
  40. [WebMethod]     
  41. public int addnumber(int a, int b)   
  42. {   
  43. return = a + b;   
  44. }   

2.使用delphi创建一个dll(非com的dll),该dll调用上面的webserivce服务。

 使用delphi调用C#的webservice过程也很容易,但是对于新手可能比较麻烦(我就是这么过来的)

***步:创建一个dll单元:

  1. {$R *.res}   
  2. function GetNum(a,b:integer):  
  3. integer stdcall; [Page]  
  4. var   
  5. ireturn :Integer;   
  6. begin   
  7. ireturn:=GetServiceSoap().addnumber(a,b);   
  8. //这个GetServiceSoap是什么,看后面...   
  9. result:=ireturn;   
  10. end;  
  11. exports   
  12. GetNum name ’GetNum’;   
  13. begin   
  14. end.   
  15. //如果是delphi调用该dll必须使用下面的代码。  
  16. C#调用则不需要了(C#还是要牛一些,呵呵)   
  17. initialization   
  18. coinitialize(nil);   
  19. finalization   
  20. counInitializ   
  21. 第二步:将webserivce的WSDL导入到该dll工程中,  
  22. 如何导,方法至少有两种,我说简单的一种:   
  23. file->new->other->WebService->WSDL Importer,  
  24. (将C#的WSDL输入)然后delphi会自动给你生成了一个pas文件,   
  25. function GetServiceSoap(UseWSDL: Boolean;   
  26. Addr: string; HTTPRIO: THTTPRIO): ServiceSoap;   
  27. const   
  28. defWSDL = ’http://localhost/webserver/  
  29. Service.asmx?WSDL’;   
  30. defURL= ’http://localhost/webserver/  
  31. Service.asmx’;   
  32. defSvc= ’Service’;   
  33. defPrt= ’ServiceSoap’;   
  34. var   
  35. RIO: THTTPRIO;   
  36. begin   
  37. Result := nil;   
  38. if (Addr = ’’) then   
  39. begin   
  40. if UseWSDL then   
  41. Addr := defWSDL   
  42. else   
  43. Addr := defURL;   
  44. end;   
  45. if HTTPRIO = nil then   
  46. RIO := THTTPRIO.Create(nil)   
  47. else   
  48. RIO := HTTPRIO;   
  49. try   
  50. //RIO.HTTPWebNode.UseUTF8InHeader:=  
  51. True; //在此添加一句,修改编码方案。   
  52. Result := (RIO as ServiceSoap);   
  53. if UseWSDL then   
  54. begin   
  55. RIO.WSDLLocation := Addr;   
  56. RIO.Service := defSvc;  
  57. RIO.Port := defPrt;   
  58. end else   
  59. RIO.URL := Addr;   
  60. finally   
  61. if (Result = nil) and   
  62. (HTTPRIO = nil) then   
  63. RIO.Free;   
  64. end;   
  65. end;   
  66.  
  67. initialization   
  68. InvRegistry.RegisterInterface  
  69. (TypeInfo(ServiceSoap), ’  
  70. http://localhost/webserver/’, ’  
  71. utf-8’); [Page]  
  72. InvRegistry.RegisterDefaultSOAPAction  
  73. (TypeInfo(ServiceSoap), ’  
  74. http://localhost/webserver/%operationName%’);   
  75. //对于无法识别传入的参数的问题,需要手工加上这一句>......   
  76. InvRegistry.RegisterInvokeOptions(TypeInfo  
  77. (ServiceSoap), ioDocument);   
  78.    
  79. 这段代码基本上你不用关心,但是,有两个地方需要特别注意:   
  80. 1.RIO.HTTPWebNode.UseUTF8InHeader:=True;  
  81. 对于中文参数必须加上   
  82. 2.InvRegistry.RegisterInvokeOptions  
  83. (TypeInfo(ServiceSoap), ioDocument);  

如果传入的参数不能被webservice识别时,多半是因为你没有加上这一句。

3.使用delphi调用上面的dll 就一个函数,没有什么好说的:

  1. procedure TForm1.Button1Click(Sender: TObject);   
  2. type   
  3. GetNumTotal=function(a,b:integer):integer;stdcall;   
  4. var   
  5. Th:Thandle;   
  6. Tf:GetNumTotal;   
  7. Tp:TFarProc;   
  8. begin   
  9.  Th:=LoadLibrary(’mywebservice.dll’); {装载DLL}   
  10. if Th〉0 then   
  11. try   
  12. Tp:=GetProcAddress(Th,PChar(’GetNum’));   
  13. if Tp〈 〉nil   
  14. then begin   
  15. Tf:=GetNumTotal(Tp);   
  16. Edit1.Text:=IntToStr(Tf(1,3)); {调用GetNumTotal函数}   
  17. end   
  18. else   
  19. ShowMessage(’GetNumTotal函数没有找到’);   
  20. finally   
  21. FreeLibrary(Th); {释放DLL}   
  22. end   
  23. else   
  24. ShowMessage(’mywebservice.dll没有找到’);   
  25. end;   

4.使用C#调用上面的dll

  1. [DllImport(\"mywebservice.dll\",  
  2. EntryPoint=\"GetNum\")]  
  3. publicstaticexternintGetNum  
  4. (inta,intb);  
  5. privatevoidbutton1_Click  
  6. (objectsender,EventArgse)  
  7. {  
  8. inta,b,i;  
  9. a=10;  
  10. b=20;  
  11. i=GetNum(a,b);//***次比较慢  
  12. (webserivce的唯一弊端!!!!)  
  13. textBox1.Text=i.ToString();  
  14. }  

【编辑推荐】

  1. C# WebService发布与调用浅析
  2. 简明教程 C# Webservice实例
  3. C#中定义装箱和拆箱详解
  4. 浅谈C#类型系统
  5. 三种不同的C#异常类型
责任编辑:冰荷 来源: wewill
相关推荐

2009-08-06 17:57:14

C# webServiC# WebServi

2009-08-06 16:44:03

C#创建WebServ

2009-12-16 09:38:36

2009-08-06 17:45:08

C# Webservi

2009-08-11 14:26:56

C#动态调用WebSe

2015-07-09 10:44:48

C#WebService

2009-08-14 10:11:47

C#允许服务与桌面交互

2009-08-06 17:12:13

C# WebServi

2009-08-10 13:05:06

C# DLLC# Delphi开发

2009-08-24 16:40:18

C#与VB7

2009-08-12 18:14:00

C# WebServi

2011-08-25 17:25:55

LUADelphi

2009-09-01 15:24:59

C++、C#和JAVA

2009-08-18 14:36:36

C# 操作Excel

2009-09-07 05:40:16

C#窗体位置C#窗体大小

2009-08-14 17:09:48

C#引用类型

2009-07-30 18:20:21

C#继承

2009-09-01 16:07:04

C#命名规约

2009-08-24 11:23:41

C# TimeLabe

2009-09-07 16:13:56

C# MessageB
点赞
收藏

51CTO技术栈公众号