下面是一个小例子,但得不到想要的结果,请高手们帮忙分析!
{服务端}
{接口定义部分代码}
{ Invokable interface IMYTest }unit MYTestIntf;interfaceuses InvokeRegistry, Types, XSBuiltIns;type  { Invokable interfaces must derive from IInvokable }
  IMYTest = interface(IInvokable)
  ['{EA425D3A-DC44-4778-92EA-24E376B19792}']
    function GetCount():Integer;stdcall;
    function SetCount(value: Integer):Integer;stdcall;
    { Methods of Invokable interface must not use the default }
    { calling convention; stdcall is recommended }
  end;implementationinitialization
  { Invokable interfaces must be registered }
  InvRegistry.RegisterInterface(TypeInfo(IMYTest));end.{接口实现部分代码}
{ Invokable implementation File for TMYTest which implements IMYTest }unit MYTestImpl;interfaceuses InvokeRegistry, Types, XSBuiltIns, MYTestIntf;type  { TMYTest }
  TMYTest = class(TInvokableClass, IMYTest)
  private
    v_Count:Integer;
  public
    function GetCount():Integer;stdcall;
    function SetCount(value: Integer):Integer;stdcall;
  end;implementation{ TMYTest }function TMYTest.GetCount: Integer;
begin
  result := v_Count;
end;function TMYTest.SetCount(value:Integer): Integer;
begin
  v_Count := value;
  result := v_Count;
end;initialization
  { Invokable classes must be registered }
  InvRegistry.RegisterInvokableClass(TMYTest);end.{客户端}unit UMain;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UIMyTest, InvokeRegistry, StdCtrls, Rio, SOAPHTTPClient;type
  TForm1 = class(TForm)
    Edit1: TEdit;
    HTTPRIO1: THTTPRIO;
    SetButton1: TButton;
    GetButton1: TButton;
    procedure SetButton1Click(Sender: TObject);
    procedure GetButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.SetButton1Click(Sender: TObject);
var Obj: IMyTest;
    i: Integer;
begin
  Obj := HTTPRIO1 as IMyTest;
  i := Obj.SetCount(StrToInt(Edit1.Text));
  ShowMessage(IntToStr(i));
  Obj := nil;
end;procedure TForm1.GetButton1Click(Sender: TObject);
var obj: IMyTest;
    i: Integer;
begin
  Obj := HTTPRIO1 as IMyTest;
  i := Obj.GetCount;
  ShowMessage(IntToStr(i));
  Obj := nil;
end;end.要实现在EDIT1中输入一个数字,然后点击<SetButton>后将值传到服务端,然后存储到一个变量中,当客户端点击<GetButton>时,服务端将变量的值传回客户端显示,但每次<GetButton>传回的值都是0.而不是刚刚SET的值,哪位可以解决该问题?
分不够可加!

解决方案 »

  1.   

    var obj: IMyTest;
    把创建的obj对象保存下来!就可以了。
    你设置了,就把obj=nil,不是把这个对象给释放啦!那还跟你保存这个值呢?
    Get时,等于是重新创建一个新的服务器对象,当然值为0啦!
      

  2.   

    老兄,nil去掉,
    但是GetButton1方法中你又创建了一个新的obj: IMyTest,这个不是你要的那个对象啦!
    所以还是0
      

  3.   

    唉!
    在TForm1的private声明 Obj: IMyTest;
    把setbutton,getbutton中的obj声明都去掉
    SetButton1把obj=nil去掉
    getbutton1写成
    procedure TForm1.GetButton1Click(Sender: TObject);
    var
        i: Integer;
    begin
      i := Obj.GetCount;
      ShowMessage(IntToStr(i));
      Obj := nil;
    end;
      

  4.   

    汗S,看来你还是没有明白WebService。WebService,顾名思义,它是一个Web应用,而一个标准的Web应用,是无状态的,你必须自己处理状态的持久化,比如保存到数据库,或通过cookie记录在客户端。在Web应用中,只有“请求”和“响应”,而一个SOAP“调用”就是被这样分解的。所以你调用SetCount的时候,只是发出一个Web请求,然后服务端创建一个实例去响应你的请求,执行完后,服务端返回一个响应,然后服务端实例就被释放了。当你下次调用GetCount的时候,又是一个请求,服务端又创建一个新的实例--注意:是新的--所以就是你现在的结果了。这样的结果是正确的。
      

  5.   

    webserice 是无状态的,基于http协议的。