自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解C# Webservice與Delphi交互

開發(fā) 后端
Delphi是目前小型C/S系統(tǒng)的最佳選擇,本文向您介紹使用C# Webservice與Delphi交互開發(fā)的一些實(shí)例,希望對(duì)您有所幫助。

大家都知道C# Webservice技術(shù)的出現(xiàn)將各種開發(fā)技術(shù)和語(yǔ)言完全的融合了,下面就這種融合在C#和delphi之間的交互做一次全面的體現(xiàn),前者是目前***的開發(fā)平臺(tái),后者依然是小型c/s系統(tǒng)的***選擇.

1.使用C#創(chuàng)建一個(gè)Webservice服務(wù)。

使用vs2005的模板創(chuàng)建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. //如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行    
  11. InitializeComponent();   
  12. }   
  13.    
  14. #region  Component  Designer    
  15. generated  code   
  16. private void InitializeComponent()   
  17. {   
  18.     
  19. }   
  20. //Web  服務(wù)設(shè)計(jì)器所必需的    
  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. //這個(gè)是自動(dòng)生成的一個(gè)webservice函數(shù),  
  34. 可以不要。   
  35. [WebMethod]   
  36. public string HelloWorld() {   
  37. return \"Hello World\";   
  38. }   
  39. //這個(gè)才是我們自己創(chuàng)建的,   
  40. [WebMethod]     
  41. public int addnumber(int a, int b)   
  42. {   
  43. return = a + b;   
  44. }   

2.使用delphi創(chuàng)建一個(gè)dll(非com的dll),該dll調(diào)用上面的webserivce服務(wù)。

 使用delphi調(diào)用C#的webservice過程也很容易,但是對(duì)于新手可能比較麻煩(我就是這么過來的)

***步:創(chuàng)建一個(gè)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. //這個(gè)GetServiceSoap是什么,看后面...   
  9. result:=ireturn;   
  10. end;  
  11. exports   
  12. GetNum name ’GetNum’;   
  13. begin   
  14. end.   
  15. //如果是delphi調(diào)用該dll必須使用下面的代碼。  
  16. C#調(diào)用則不需要了(C#還是要牛一些,呵呵)   
  17. initialization   
  18. coinitialize(nil);   
  19. finalization   
  20. counInitializ   
  21. 第二步:將webserivce的WSDL導(dǎo)入到該dll工程中,  
  22. 如何導(dǎo),方法至少有兩種,我說簡(jiǎn)單的一種:   
  23. file->new->other->WebService->WSDL Importer,  
  24. (將C#的WSDL輸入)然后delphi會(huì)自動(dòng)給你生成了一個(gè)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. //對(duì)于無法識(shí)別傳入的參數(shù)的問題,需要手工加上這一句>......   
  76. InvRegistry.RegisterInvokeOptions(TypeInfo  
  77. (ServiceSoap), ioDocument);   
  78.    
  79. 這段代碼基本上你不用關(guān)心,但是,有兩個(gè)地方需要特別注意:   
  80. 1.RIO.HTTPWebNode.UseUTF8InHeader:=True;  
  81. 對(duì)于中文參數(shù)必須加上   
  82. 2.InvRegistry.RegisterInvokeOptions  
  83. (TypeInfo(ServiceSoap), ioDocument);  

如果傳入的參數(shù)不能被webservice識(shí)別時(shí),多半是因?yàn)槟銢]有加上這一句。

3.使用delphi調(diào)用上面的dll 就一個(gè)函數(shù),沒有什么好說的:

  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)); {調(diào)用GetNumTotal函數(shù)}   
  17. end   
  18. else   
  19. ShowMessage(’GetNumTotal函數(shù)沒有找到’);   
  20. finally   
  21. FreeLibrary(Th); {釋放DLL}   
  22. end   
  23. else   
  24. ShowMessage(’mywebservice.dll沒有找到’);   
  25. end;   

4.使用C#調(diào)用上面的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發(fā)布與調(diào)用淺析
  2. 簡(jiǎn)明教程 C# Webservice實(shí)例
  3. C#中定義裝箱和拆箱詳解
  4. 淺談C#類型系統(tǒng)
  5. 三種不同的C#異常類型
責(zé)任編輯:冰荷 來源: wewill
相關(guān)推薦

2009-08-06 17:57:14

C# webServiC# WebServi

2009-08-06 16:44:03

C#創(chuàng)建WebServ

2009-08-11 14:26:56

C#動(dòng)態(tài)調(diào)用WebSe

2009-08-06 17:45:08

C# Webservi

2015-07-09 10:44:48

C#WebService

2009-12-16 09:38:36

2009-08-14 10:11:47

C#允許服務(wù)與桌面交互

2009-08-06 17:12:13

C# WebServi

2009-08-24 16:40:18

C#與VB7

2009-08-10 13:05:06

C# DLLC# Delphi開發(fā)

2009-09-01 15:24:59

C++、C#和JAVA

2011-08-25 17:25:55

LUADelphi

2009-08-12 18:14:00

C# WebServi

2009-08-18 14:36:36

C# 操作Excel

2025-04-30 01:50:00

C#異步編程

2009-09-07 05:40:16

C#窗體位置C#窗體大小

2009-08-14 17:09:48

C#引用類型

2009-07-30 18:20:21

C#繼承

2009-08-24 11:23:41

C# TimeLabe

2009-09-07 16:13:56

C# MessageB
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)