大家都知道C# Webservice技術的出現將各種開發技術和語言完全的融合了,下面就這種融合在C#和delphi之間的交互做一次全面的體現,前者是目前最好的開發平臺,後者依然是小型c/s系統的最佳選擇.
1.使用C#創建一個Webservice服務。
使用vs2005的模板創建C# Webservice非常容易。原文件如下:
- [WebService(Namespace = \"http:
- //localhost/webserver/\")]
- [WebServiceBinding(ConformsTo =
- WsiProfiles.BasicProfile1_1)]
- public class Service :
- System.Web.Services.WebService
- {
- public Service () {
- //如果使用設計的組件,請取消註釋以下行
- InitializeComponent();
- }
- #region Component Designer
- generated code
- private void InitializeComponent()
- {
- }
- //Web 服務設計器所必需的
- private IContainer components = null;
- protected override void Dispose
- (bool disposing)
- {
- if (disposing && components != null)
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #endregion
- //這個是自動生成的一個webservice函數,
- 可以不要。
- [WebMethod]
- public string HelloWorld() {
- return \"Hello World\";
- }
- //這個纔是我們自己創建的,
- [WebMethod]
- public int addnumber(int a, int b)
- {
- return = a + b;
- }
- }
2.使用delphi創建一個dll(非com的dll),該dll調用上面的webserivce服務。
使用delphi調用C#的webservice過程也很容易,但是對於新手可能比較麻煩(我就是這麼過來的)
第一步:創建一個dll單元:
- {$R *.res}
- function GetNum(a,b:integer):
- integer stdcall; [Page]
- var
- ireturn :Integer;
- begin
- ireturn:=GetServiceSoap().addnumber(a,b);
- //這個GetServiceSoap是什麼,看後面...
- result:=ireturn;
- end;
- exports
- GetNum name ’GetNum’;
- begin
- end.
- //如果是delphi調用該dll必須使用下面的代碼。
- C#調用則不需要了(C#還是要牛一些,呵呵)
- initialization
- coinitialize(nil);
- finalization
- counInitializ
- 第二步:將webserivce的WSDL導入到該dll工程中,
- 如何導,方法至少有兩種,我說簡單的一種:
- file->new->other->WebService->WSDL Importer,
- (將C#的WSDL輸入)然後delphi會自動給你生成了一個pas文件,
- function GetServiceSoap(UseWSDL: Boolean;
- Addr: string; HTTPRIO: THTTPRIO): ServiceSoap;
- const
- defWSDL = ’http://localhost/webserver/
- Service.asmx?WSDL’;
- defURL= ’http://localhost/webserver/
- Service.asmx’;
- defSvc= ’Service’;
- defPrt= ’ServiceSoap’;
- 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
- //RIO.HTTPWebNode.UseUTF8InHeader:=
- True; //在此添加一句,修改編碼方案。
- Result := (RIO as ServiceSoap);
- 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
- InvRegistry.RegisterInterface
- (TypeInfo(ServiceSoap), ’
- http://localhost/webserver/’, ’
- utf-8’); [Page]
- InvRegistry.RegisterDefaultSOAPAction
- (TypeInfo(ServiceSoap), ’
- http://localhost/webserver/%operationName%’);
- //對於無法識別傳入的參數的問題,需要手工加上這一句>......
- InvRegistry.RegisterInvokeOptions(TypeInfo
- (ServiceSoap), ioDocument);
- 這段代碼基本上你不用關心,但是,有兩個地方需要特別注意:
- 1.RIO.HTTPWebNode.UseUTF8InHeader:=True;
- 對於中文參數必須加上
- 2.InvRegistry.RegisterInvokeOptions
- (TypeInfo(ServiceSoap), ioDocument);
如果傳入的參數不能被webservice識別時,多半是因爲你沒有加上這一句。
3.使用delphi調用上面的dll 就一個函數,沒有什麼好說的:
- procedure TForm1.Button1Click(Sender: TObject);
- type
- GetNumTotal=function(a,b:integer):integer;stdcall;
- var
- Th:Thandle;
- Tf:GetNumTotal;
- Tp:TFarProc;
- begin
- Th:=LoadLibrary(’mywebservice.dll’); {裝載DLL}
- if Th〉0 then
- try
- Tp:=GetProcAddress(Th,PChar(’GetNum’));
- if Tp〈 〉nil
- then begin
- Tf:=GetNumTotal(Tp);
- Edit1.Text:=IntToStr(Tf(1,3)); {調用GetNumTotal函數}
- end
- else
- ShowMessage(’GetNumTotal函數沒有找到’);
- finally
- FreeLibrary(Th); {釋放DLL}
- end
- else
- ShowMessage(’mywebservice.dll沒有找到’);
- end;
4.使用C#調用上面的dll
- [DllImport(\"mywebservice.dll\",
- EntryPoint=\"GetNum\")]
- publicstaticexternintGetNum
- (inta,intb);
- privatevoidbutton1_Click
- (objectsender,EventArgse)
- {
- inta,b,i;
- a=10;
- b=20;
- i=GetNum(a,b);//第一次比較慢
- (webserivce的唯一弊端!!!!)
- textBox1.Text=i.ToString();
- }