最近发现在delphi里调用asp.net做的webservices中的方法有一个毛病,不知道如何解决:在delphi中在单独的线程内调用一个sebservices方法,如果这个方法在执行时要堵很久,在以这种方式调同样一个方法没什么问题,到了第三次的时候就不对了,调用会被阻塞直到前面的调用完成之后才会调用我写了一个函数,在这个函数里面启动一个新的线程在这个线程开始后调用这个要堵很久的webservices的方法,调用结束后弹出一个信息,我在一个按钮里面调用这个方法,连续点6次,然后等了一会,弹出信息来了只有两个,确认之后又等一会又来了两个,再确认再等然后是最后的两个,从这个测试中可以看出delphi好像同时只能调用两个webservices方法.我再用.net的c#写了一个一样的客户端,按6次按钮之后等会就会有6个确认依次弹出,证明.net没有这个毛病.两个是调用的同一个服务端,证明不是服务端的问题.我用的是delphi7,用THTTPRIO来调webservices,一个实例和调一次创建一个实例没有区别有没有谁知道这个该怎么解决请指点一二?

解决方案 »

  1.   

    应该不是WebServices的问题,可能是多线程的问题,看看线程有没有控制好,代码贴出来看看
      

  2.   

    是不是線程沒有控制好,你把wsdl或代碼貼出來看看
      

  3.   

    客户端代码:按钮事件procedure TForm1.Button4Click(Sender: TObject);
    begin
      TSingleThreadTask.ProcessTask(AsynCall_3);
    end;线程过程回调函数procedure TForm1.AsynCall_3;
    var
      prio: THTTPRIO;
    begin
      CoInitialize(nil);
      try
        prio := THTTPRIO.Create(self);
        try
          prio.URL := 'http://localhost/WebTest2/WebTest2.asmx';
          (prio as WebTest2Soap).call_7(1);
          self.Timer2.Enabled := true; //主线程中ShowMessage信息
        finally
          prio.Free;
        end;
      finally
        CoUnInitialize;
      end;
      EndThread(0);
    end;
    线程类封装unit SingleThreadTask;interfaceuses SysUtils, Classes, ExtCtrls;type
      TProcessTaskCallBack = procedure of Object;
      TProcessTaskCallBack2 = procedure (obj: TThread) of Object;
      //独立线程过程
      TSingleThreadTask = class(TThread)
      private
        CallBack: TProcessTaskCallBack;
        CallBack2: TProcessTaskCallBack2;
      protected
        procedure Execute; override;
      public
        //输出函数
        class procedure ProcessTask(CallBack: TProcessTaskCallBack); overload;
        class function ProcessTask(CallBack: TProcessTaskCallBack2): TThread; overload;
      end;implementation//执行过程
    class procedure TSingleThreadTask.ProcessTask
    (
      CallBack: TProcessTaskCallBack
    );
    var
      obj: TSingleThreadTask;
    begin
      obj := TSingleThreadTask.Create(true);
      obj.CallBack := CallBack;
      obj.Resume;
    end;
    //执行过程
    class function TSingleThreadTask.ProcessTask
    (
      CallBack: TProcessTaskCallBack2
    ): TThread;
    var
      obj: TSingleThreadTask;
    begin
      obj := TSingleThreadTask.Create(true);
      obj.CallBack2 := CallBack;
      obj.Resume;
      Result := obj;
    end;
    //
    procedure TSingleThreadTask.Execute;
    begin
      //执行任务
      try
        if Assigned(self.CallBack) then
          self.CallBack
        else if Assigned(self.CallBack2) then
          self.CallBack2(self);
      finally
        //自释放
        self.Free;
      end;
    end;
    end.
     服务端 WebTest2.asmx.csusing System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml.Linq;
    using System.Threading;namespace WebTest2
    {
        /// <summary>
        /// WebTest2 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
        // [System.Web.Script.Services.ScriptService]
        public class WebTest2 : System.Web.Services.WebService
        {        [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }        [WebMethod]
            public void Call_1()
            {
                Thread.Sleep(20 * 1000);
            }        [WebMethod]
            public void Call_2()
            {
            }        [WebMethod]
            public void Call_3()
            {
            }        [WebMethod(EnableSession = true)]
            public void Call_4()
            {
                Thread.Sleep(20 * 1000);
            }
            [WebMethod(EnableSession = true)]
            public void Call_5()
            {
            }
            [WebMethod(EnableSession = true)]
            public void Call_6()
            {
            }
            [WebMethod]
            public void Call_7(int nMode)
            {
                if (nMode == 1)
                    Thread.Sleep(20 * 1000);
            }
        }
    }
      

  4.   

    逐步跟蹤檢查一下你的線程代碼,可以先直接調用其webservices(暫不封裝線程)看看比較一下
    本地localhost,應該不是一些端口或協議之類的
      

  5.   

    看看這樣能否呼出?阻塞多久而後再檢查線程。。unit service;interfaceuses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;const
      IS_OPTN = $0001;
      IS_REF = $0080;
    type  // ************************************************************************ //
      // The following types, referred to in the WSDL document are not being represented
      // in this file. They are either aliases[@] of other types represented or were referred
      // to but never[!] declared in the document. The types from the latter category
      // typically map to predefined/known XML or Borland types; however, they could also  
      // indicate incorrect WSDL documents that failed to declare or import a schema type.
      // ************************************************************************ //
      // !:string - "http://www.w3.org/2001/XMLSchema"[Gbl]  // ************************************************************************ //
      // Namespace : Kye
      // soapAction: Kye/HelloWorld
      // transport : http://schemas.xmlsoap.org/soap/http
      // style : document
      // binding : ServiceSoap
      // service : Service
      // port : ServiceSoap
      // URL : http://172.20.100.10/test/service.asmx
      // ************************************************************************ //
      ServiceSoap = interface(IInvokable)
      ['{B82FAFBA-EAB8-AD5C-2535-FB6046355616}']
      function HelloWorld(const name_: WideString): WideString; stdcall;
      end;function GetServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): ServiceSoap;
    implementation
      uses SysUtils;function GetServiceSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): ServiceSoap;
    const
      defWSDL = 'http://172.20.100.10/test/service.asmx?wsdl';
      defURL = 'http://172.20.100.10/test/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
      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), 'Kye', 'utf-8');
      InvRegistry.RegisterDefaultSOAPAction(TypeInfo(ServiceSoap), 'Kye/HelloWorld');
      InvRegistry.RegisterInvokeOptions(TypeInfo(ServiceSoap), ioDocument);
      InvRegistry.RegisterExternalParamName(TypeInfo(ServiceSoap), 'HelloWorld', 'name_', 'name');end.
      

  6.   

    demo調用:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,service, StdCtrls;type
      TForm1 = class(TForm)
      btn1: TButton;
      edt1: TEdit;
      Label1: TLabel;
      procedure btn1Click(Sender: TObject);
      private
      aa:ServiceSoap;
      { Private declarations }
      public
      { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
    begin
      aa:=GetServiceSoap(False,'',nil);
      ShowMessage(aa.HelloWorld(edt1.Text));
    end;end.
     
      

  7.   

    to kye_jufei您的服务器一般是什么时候能访问?
      

  8.   

    线程写的有问题,没有创建函数,还有,怎么没见你创建线程就这样调用了TSingleThreadTask.ProcessTask(AsynCall_3);
      

  9.   

    to amu16888TSingleThreadTask.ProcessTask 这个是这个类的一个静态函数,是可以直接调的,在这个静态函数里面是创建了继承自TThread的类TSingleThreadTask的,问题不在这
      

  10.   

    http://news.baidu.com/我想写个程序。大概的功能是:
    1.类似定制版的浏览器。读取指定页面。http://news.baidu.com/然后软件上有个登陆窗口。不是在网页上登陆。直接在软件上用“按钮+文本框”登陆百度。
    2.读取某网站的页面内容。获取他最新更新的新闻。并且自动刷新网页。在网页上有新闻的时候。弹出消息对话框。告诉我有最新新闻了。能弹出对话框的同时 要是能播放个音乐什么的最好了 。
    3.读取网页指定区域。的指定内容。比如网站上的新闻列表。读出来在软件的Webbrowser控件界面上显示。出来。不是直接是浏览器。不需要读取这个页面。只要网页上的一部分内容就好了。
    4.我不要再软件上让人看到百度广告等等垃圾消息。只要新闻。每隔20秒自动刷新一次。弹窗对话框或者语音。提示有新的新闻出来了。看看有没有高手有现成的或者类似的源码的。
    发给我邮箱:1617822579(at)qq.com 或者联系我qq# 1617822579
      

  11.   

    装Delphi2010试了一下,一样的代码它没有这个毛病,看来是Delphi7版本上是这种,没法指望在这个版本来解决这个问题了,结贴吧。