分享

解决webservice 异步调用在有的pc客户端是好的,但是在有的pc客户端上用就不行的...

 tong 2007-08-08

最近遇到怪事了.

webservice 异步调用 begininvoke/endinvoke 在有的pc客户端是好的,但是在有的pc客户端上用就不行.问题出在service.begininvoke调用以后,回调函数不返回.

但是,在所有的客户端测试同步调用方式,所有的机器都是可以正常返回数据的.

(.net 20 环境)

请问,这是不是客户端某些环境没设置好造成的???
有没有人以前遇到这样的问题啊????
谢谢!!!!


源代码
//Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using asynwebservicecalltest.eMailServices;

namespace asynwebservicecalltest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

      //异步调用webservice 
      private void button1_Click(object sender, EventArgs e)
        {
                BeginGetCSRStatusTest(CRStatsCalculatedTest);
           
        }

       
        private void BeginGetCSRStatusTest(AsyncCallback callback)
        {
            Service service = new Service();
            service.Url = "http:///Reporting/Service.asmx";
           
            service.BegingetCSRStats("1000","junxu3", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),callback, null);
           
        }

        private string EndGetCSRStatusTest(IAsyncResult res)
        {
            Service service = new Service();
            service.Url = "http:///Reporting/Service.asmx";

            System.Text.StringBuilder CSRStatusDisplay = new System.Text.StringBuilder();
            CSRStat[] CSRStatArray = service.EndgetCSRStats(res);
            foreach (CSRStat stat in CSRStatArray)
            {
                CSRStatusDisplay.Append(stat.Activity).Append("-").Append(stat.Count).Append("  ");
            }
            return CSRStatusDisplay.ToString();

        }

       
        void CRStatsCalculatedTest(IAsyncResult res)
        {
            ShowCRStats(EndGetCSRStatusTest(res));
           
        }

        delegate void ShowCRStatsDelegate(string stats);

        void ShowCRStats(string stats)
        {
            if (this.InvokeRequired == false)
            {
                label1.Text = stats;
            }
            else
            {
                ShowCRStatsDelegate showCRStats = new ShowCRStatsDelegate(ShowCRStats);
                this.BeginInvoke(showCRStats, new object[] { stats });
            }
        }

      //同步调用webservice
        private void button2_Click(object sender, EventArgs e)
        {
            StringBuilder CSRStatusDisplay = new StringBuilder();
            CSRStat[] CSRStatArray = GetCSRStatus();
            foreach (CSRStat stat in CSRStatArray)
            {
                CSRStatusDisplay.Append(stat.Activity).Append("-").Append(stat.Count).Append("  ");
            }
            label1.Text = CSRStatusDisplay.ToString();
        }


        private static CSRStat[] GetCSRStatus()
        {
            CSRStat[] CSRStatArray = null;

            Service service = new Service();
            service.Url = "http:///Reporting/Service.asmx";
      
           
            CSRStatArray = service.getCSRStats("1000", "junxu3", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return CSRStatArray;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            label1.Text = "";
        }
    }
}

//Referrance.cs

/// <remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http:///getCSRStats", RequestNamespace="http:///", ResponseNamespace="http:///", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

        public CSRStat[] getCSRStats(string ApplicationId, string NTLogin, string currentLocalTime) {
            object[] results = this.Invoke("getCSRStats", new object[] {
                        ApplicationId,
                        NTLogin,
                        currentLocalTime});
            return ((CSRStat[])(results[0]));
        }

        /// <remarks/>
        public System.IAsyncResult BegingetCSRStats(string ApplicationId, string NTLogin, string currentLocalTime, System.AsyncCallback callback, object asyncState)
        {
            return this.BeginInvoke("getCSRStats", new object[] {
                        ApplicationId,
                        NTLogin,
                        currentLocalTime}, callback, asyncState);
        }

        /// <remarks/>
        public CSRStat[] EndgetCSRStats(System.IAsyncResult asyncResult)
        {
            object[] results = this.EndInvoke(asyncResult);
            return ((CSRStat[])(results[0]));
        }

在这些有问题的客户端,表现为在service.begininvoke调用以后,回调函数不返回。也就是

void CRStatsCalculatedTest(IAsyncResult res)
        {
            ShowCRStats(EndGetCSRStatusTest(res));
           
        }

这段代码没有执行到。(奇怪的是,仅仅在某些客户端发生这种情况(这些客户端网络没问题))

(我还重新安装了一遍 .net framework 2.0,但是依然没用!!!)

 

 

好吧,写个最简单的sample试一下。

再问超奇怪难题:winform异步调用webservice的Completed事件在有些机器上每次都被触发,在有些机器每次都没有触发。但是如果同步调用webservice,所有机器都成功返回。

具体情况如下:

有2台pc机:client001,client002, 同样的环境和同样的代码.
(.net 2.0 framework + WinXP professional 环境)


client001 调用异步webservice,每次都失败(completed event不触发,代码也没有报任何错误); 调用同步webservice,每次都成功返回.
client002 调用异步webservice,每次都成功返回; 调用同步webservice,每次都成功返回.

问题是client001 为什么会调用异步,每次都失败???

到底是:
1. windowXP 问题?
2. .net framwork 问题?
3. config文件配置问题?
4. 网络配置问题?
5. 还是其他软硬件问题?

这是我的一些研究线索:
1 我比较了很多这2台机器的各种参数和配置情况,也没有查出什么不同来。
2 我也跟踪了web service server端的反应,不管是client001还是client002,每次调用不管成功失败,server端的代码是肯定执行到的。


我的测试代码很简单,就是个HelloWorld例子。(如下)


//这是客户端代码 client.cs

//异步调用webservice
private void button6_Click(object sender, EventArgs e)
        {
            webservicetest.Service service = new webservicetest.Service();
            service.Url = "http://d-sjn-khathi/webservicetest/Service.asmx";

            service.HelloWorldCompleted += new HelloWorldCompletedEventHandler(service_HelloWorldCompleted);

            service.HelloWorldAsync();
           
           
        }

//异步调用webservice的事件处理
        void service_HelloWorldCompleted(object sender, HelloWorldCompletedEventArgs e)
        {

            label1.Text = e.Result;
        }


//这是同步调用webservice
        private void button7_Click(object sender, EventArgs e)
        {
            webservicetest.Service service = new webservicetest.Service();
            service.Url = "http://d-sjn-khathi/webservicetest/Service.asmx";

            label1.Text = service.HelloWorld();
        }

 

//这是web service 服务器端代码 service.cs


using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http:///")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

 }

 

还是不行!!!

那么,跟踪一下.net networking trace log

用了.net的 network trace功能在有问题的客户机上抓了一下包,得到了如下output:

System.Net Verbose: 0 : [2052] WebRequest::Create(http://d-sjn-khathi/webservicetest/Service.asmx)
System.Net Verbose: 0 : [2052] HttpWebRequest#55144039::HttpWebRequest(http://d-sjn-khathi/webservicetest/Service.asmx#-662575407)
System.Net Verbose: 0 : [2052] Exiting HttpWebRequest#55144039::HttpWebRequest()
System.Net Verbose: 0 : [2052] Exiting WebRequest::Create() -> HttpWebRequest#55144039
System.Net Verbose: 0 : [2052] HttpWebRequest#55144039::BeginGetRequestStream()
System.Net Information: 0 : [2052] Associating HttpWebRequest#55144039 with ServicePoint#26534308
System.Net Information: 0 : [2052] Associating Connection#37482183 with HttpWebRequest#55144039
System.Net Verbose: 0 : [2052] Exiting HttpWebRequest#55144039::BeginGetRequestStream() -> ContextAwareResult#1795329
System.Net Information: 0 : [0892] Associating HttpWebRequest#55144039 with ConnectStream#4032828
System.Net Verbose: 0 : [0892] HttpWebRequest#55144039::EndGetRequestStream()
System.Net Verbose: 0 : [0892] Exiting HttpWebRequest#55144039::EndGetRequestStream() -> ConnectStream#4032828
System.Net Verbose: 0 : [0892] ConnectStream#4032828::Write()
System.Net Verbose: 0 : [0892] Data from ConnectStream#4032828::Write
System.Net Verbose: 0 : [0892] 00000000 : 3C 3F 78 6D 6C 20 76 65-72 73 69 6F 6E 3D 22 31 : <?xml version="1
System.Net Verbose: 0 : [0892] 00000010 : 2E 30 22 20 65 6E 63 6F-64 69 6E 67 3D 22 75 74 : .0" encoding="ut
System.Net Verbose: 0 : [0892] 00000020 : 66 2D 38 22 3F 3E 3C 73-6F 61 70 3A 45 6E 76 65 : f-8"?><soap:Enve
System.Net Verbose: 0 : [0892] 00000030 : 6C 6F 70 65 20 78 6D 6C-6E 73 3A 73 6F 61 70 3D : lope xmlns:soap=
System.Net Verbose: 0 : [0892] 00000040 : 22 68 74 74 70 3A 2F 2F-73 63 68 65 6D 61 73 2E : "http://schemas.
System.Net Verbose: 0 : [0892] 00000050 : 78 6D 6C 73 6F 61 70 2E-6F 72 67 2F 73 6F 61 70 : xmlsoap.org/soap
System.Net Verbose: 0 : [0892] 00000060 : 2F 65 6E 76 65 6C 6F 70-65 2F 22 20 78 6D 6C 6E : /envelope/" xmln
System.Net Verbose: 0 : [0892] 00000070 : 73 3A 78 73 69 3D 22 68-74 74 70 3A 2F 2F 77 77 : s:xsi="http://ww
System.Net Verbose: 0 : [0892] 00000080 : 77 2E 77 33 2E 6F 72 67-2F 32 30 30 31 2F 58 4D : w.w3.org/2001/XM
System.Net Verbose: 0 : [0892] 00000090 : 4C 53 63 68 65 6D 61 2D-69 6E 73 74 61 6E 63 65 : LSchema-instance
System.Net Verbose: 0 : [0892] 000000A0 : 22 20 78 6D 6C 6E 73 3A-78 73 64 3D 22 68 74 74 : " xmlns:xsd="htt
System.Net Verbose: 0 : [0892] 000000B0 : 70 3A 2F 2F 77 77 77 2E-77 33 2E 6F 72 67 2F 32 : p://www.w3.org/2
System.Net Verbose: 0 : [0892] 000000C0 : 30 30 31 2F 58 4D 4C 53-63 68 65 6D 61 22 3E 3C : 001/XMLSchema"><
System.Net Verbose: 0 : [0892] 000000D0 : 73 6F 61 70 3A 42 6F 64-79 3E 3C 48 65 6C 6C 6F : soap:Body><Hello
System.Net Verbose: 0 : [0892] 000000E0 : 57 6F 72 6C 64 20 78 6D-6C 6E 73 3D 22 68 74 74 : World xmlns="htt
System.Net Verbose: 0 : [0892] 000000F0 : 70 3A 2F 2F 74 65 6D 70-75 72 69 2E 6F 72 67 2F : p:///
System.Net Verbose: 0 : [0892] 00000100 : 22 20 2F 3E                                     : " />
System.Net Verbose: 0 : [0892] Exiting ConnectStream#4032828::Write()
System.Net Verbose: 0 : [0892] ConnectStream#4032828::Write()
System.Net Verbose: 0 : [0892] Data from ConnectStream#4032828::Write
System.Net Verbose: 0 : [0892] 00000000 : 3C 2F 73 6F 61 70 3A 42-6F 64 79 3E 3C 2F 73 6F : </soap:Body></so
System.Net Verbose: 0 : [0892] 00000010 : 61 70 3A 45 6E 76 65 6C-6F 70 65 3E             : ap:Envelope>
System.Net Verbose: 0 : [0892] Exiting ConnectStream#4032828::Write()
System.Net Verbose: 0 : [0892] ConnectStream#4032828::Close()
System.Net Verbose: 0 : [0892] Exiting ConnectStream#4032828::Close()
System.Net Verbose: 0 : [0892] HttpWebRequest#55144039::BeginGetResponse()
System.Net Information: 0 : [0892] HttpWebRequest#55144039 - Request: POST /webservicetest/Service.asmx HTTP/1.1

System.Net Information: 0 : [0892] ConnectStream#4032828 - Sending headers
{
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.42)
Content-Type: text/xml; charset=utf-8
SOAPAction: "http:///HelloWorld"
Host: d-sjn-khathi
Content-Length: 288
Expect: 100-continue
Connection: Keep-Alive
}.
System.Net Verbose: 0 : [0892] Exiting HttpWebRequest#55144039::BeginGetResponse() -> ContextAwareResult#33711845
System.Net Information: 0 : [0892] Connection#37482183 - Received status line: Version=1.1, StatusCode=100, StatusDescription=Continue.
System.Net Information: 0 : [0892] Connection#37482183 - Received headers
{
Date: Thu, 05 Apr 2007 02:53:20 GMT
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
}.
System.Net Verbose: 0 : [0892] Data from ConnectStream#4032828::ResubmitWrite
System.Net Verbose: 0 : [0892] 00000000 : 3C 3F 78 6D 6C 20 76 65-72 73 69 6F 6E 3D 22 31 : <?xml version="1
System.Net Verbose: 0 : [0892] 00000010 : 2E 30 22 20 65 6E 63 6F-64 69 6E 67 3D 22 75 74 : .0" encoding="ut
System.Net Verbose: 0 : [0892] 00000020 : 66 2D 38 22 3F 3E 3C 73-6F 61 70 3A 45 6E 76 65 : f-8"?><soap:Enve
System.Net Verbose: 0 : [0892] 00000030 : 6C 6F 70 65 20 78 6D 6C-6E 73 3A 73 6F 61 70 3D : lope xmlns:soap=
System.Net Verbose: 0 : [0892] 00000040 : 22 68 74 74 70 3A 2F 2F-73 63 68 65 6D 61 73 2E : "http://schemas.
System.Net Verbose: 0 : [0892] 00000050 : 78 6D 6C 73 6F 61 70 2E-6F 72 67 2F 73 6F 61 70 : xmlsoap.org/soap
System.Net Verbose: 0 : [0892] 00000060 : 2F 65 6E 76 65 6C 6F 70-65 2F 22 20 78 6D 6C 6E : /envelope/" xmln
System.Net Verbose: 0 : [0892] 00000070 : 73 3A 78 73 69 3D 22 68-74 74 70 3A 2F 2F 77 77 : s:xsi="http://ww
System.Net Verbose: 0 : [0892] 00000080 : 77 2E 77 33 2E 6F 72 67-2F 32 30 30 31 2F 58 4D : w.w3.org/2001/XM
System.Net Verbose: 0 : [0892] 00000090 : 4C 53 63 68 65 6D 61 2D-69 6E 73 74 61 6E 63 65 : LSchema-instance
System.Net Verbose: 0 : [0892] 000000A0 : 22 20 78 6D 6C 6E 73 3A-78 73 64 3D 22 68 74 74 : " xmlns:xsd="htt
System.Net Verbose: 0 : [0892] 000000B0 : 70 3A 2F 2F 77 77 77 2E-77 33 2E 6F 72 67 2F 32 : p://www.w3.org/2
System.Net Verbose: 0 : [0892] 000000C0 : 30 30 31 2F 58 4D 4C 53-63 68 65 6D 61 22 3E 3C : 001/XMLSchema"><
System.Net Verbose: 0 : [0892] 000000D0 : 73 6F 61 70 3A 42 6F 64-79 3E 3C 48 65 6C 6C 6F : soap:Body><Hello
System.Net Verbose: 0 : [0892] 000000E0 : 57 6F 72 6C 64 20 78 6D-6C 6E 73 3D 22 68 74 74 : World xmlns="htt
System.Net Verbose: 0 : [0892] 000000F0 : 70 3A 2F 2F 74 65 6D 70-75 72 69 2E 6F 72 67 2F : p:///
System.Net Verbose: 0 : [0892] 00000100 : 22 20 2F 3E 3C 2F 73 6F-61 70 3A 42 6F 64 79 3E : " /></soap:Body>
System.Net Verbose: 0 : [0892] 00000110 : 3C 2F 73 6F 61 70 3A 45-6E 76 65 6C 6F 70 65 3E : </soap:Envelope>


好像 HTTP/1.1 200 OK包没有返回,这个说明了什么?怎么解决?

(只返回了 Version=1.1, StatusCode=100, StatusDescription=Continue.,然后就没有了)

后来,我又用了 ‘Enthereal‘ 在这台有问题的pc上抓了一下网络数据包,
我发现,http://1.1 200 OK 这个层面上是返回的。

但是,在 .NET network tracing log 中没有被记录。

 
也就是说,response是到达客户端的,但是没有到达.net中。
这是什么原因阿?

 

 

再后来,有个新发现。

我每次重新启动这台pc后,然后第一次调用这个异步webservice方法,总是会在.net network trace log 中发现一下错误信息:

System.Net.Sockets Error: 0 : [0452] Exception in the Socket#37489757::BeginReceive - The handle is invalid
 
然后再试着第2次,第3次调用,却没有再发现上面的错误日志。
然后我再一次重新启动这台pc,紧接着调用这个异步webservice方法,上面的错误信息又被记录了。
然后再试着第2次,第3次调用,还是没有再发现上面的错误日志。
然后再一次重新启动,
。。。


但是,无论有没有记录以上错误,每一次调用都是不成功的。

搜google!!! (SocketException The handle is invalid)

找到几篇文章:

 How to determine and recover from Winsock2 corruption

http://support.microsoft.com/kb/811259

 

LSP-Fix

http://www./lspfix.htm

  

SocketException: Handle is invalid??

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=287242&SiteID=1

 

问题开始明了了!!!

 

问题的原因: 

可能有某个第三方的软件修改或损坏了winsock层 (LSP - Layered Service Provider ),导致webservice 异步调用callback不返回!!

解决方案如下:

1。用msinfo32 命令查一下有没有第三方的软件修改了winsock2的注册项,如果有找出是什么软件。

http://support.microsoft.com/kb/811259

2。卸载这个软件

3。再试一下

4。问题解决

 


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多