最近遇到怪事了.
webservice 異步調(diào)用 begininvoke/endinvoke 在有的pc客戶端是好的,但是在有的pc客戶端上用就不行.問題出在service.begininvoke調(diào)用以后,回調(diào)函數(shù)不返回.
但是,在所有的客戶端測試同步調(diào)用方式,所有的機(jī)器都是可以正常返回?cái)?shù)據(jù)的.
(.net 20 環(huán)境)
請問,這是不是客戶端某些環(huán)境沒設(shè)置好造成的???
有沒有人以前遇到這樣的問題啊????
謝謝!!!!
源代碼
//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();
}
//異步調(diào)用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 });
}
}
//同步調(diào)用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]));
}
在這些有問題的客戶端,表現(xiàn)為在service.begininvoke調(diào)用以后,回調(diào)函數(shù)不返回。也就是
void CRStatsCalculatedTest(IAsyncResult res)
{
ShowCRStats(EndGetCSRStatusTest(res));
}
這段代碼沒有執(zhí)行到。(奇怪的是,僅僅在某些客戶端發(fā)生這種情況(這些客戶端網(wǎng)絡(luò)沒問題))
(我還重新安裝了一遍 .net framework 2.0,但是依然沒用!!!)
好吧,寫個(gè)最簡單的sample試一下。
再問超奇怪難題:winform異步調(diào)用webservice的Completed事件在有些機(jī)器上每次都被觸發(fā),在有些機(jī)器每次都沒有觸發(fā)。但是如果同步調(diào)用webservice,所有機(jī)器都成功返回。
具體情況如下:
有2臺pc機(jī):client001,client002, 同樣的環(huán)境和同樣的代碼.
(.net 2.0 framework + WinXP professional 環(huán)境)
client001 調(diào)用異步webservice,每次都失敗(completed event不觸發(fā),代碼也沒有報(bào)任何錯(cuò)誤); 調(diào)用同步webservice,每次都成功返回.
client002 調(diào)用異步webservice,每次都成功返回; 調(diào)用同步webservice,每次都成功返回.
問題是client001 為什么會調(diào)用異步,每次都失敗???
到底是:
1. windowXP 問題?
2. .net framwork 問題?
3. config文件配置問題?
4. 網(wǎng)絡(luò)配置問題?
5. 還是其他軟硬件問題?
這是我的一些研究線索:
1 我比較了很多這2臺機(jī)器的各種參數(shù)和配置情況,也沒有查出什么不同來。
2 我也跟蹤了web service server端的反應(yīng),不管是client001還是client002,每次調(diào)用不管成功失敗,server端的代碼是肯定執(zhí)行到的。
我的測試代碼很簡單,就是個(gè)HelloWorld例子。(如下)
//這是客戶端代碼 client.cs
//異步調(diào)用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();
}
//異步調(diào)用webservice的事件處理
void service_HelloWorldCompleted(object sender, HelloWorldCompletedEventArgs e)
{
label1.Text = e.Result;
}
//這是同步調(diào)用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 服務(wù)器端代碼 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功能在有問題的客戶機(jī)上抓了一下包,得到了如下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包沒有返回,這個(gè)說明了什么?怎么解決?
(只返回了 Version=1.1, StatusCode=100, StatusDescription=Continue.,然后就沒有了)
后來,我又用了 ‘Enthereal‘ 在這臺有問題的pc上抓了一下網(wǎng)絡(luò)數(shù)據(jù)包,
我發(fā)現(xiàn),http://1.1 200 OK 這個(gè)層面上是返回的。
但是,在 .NET network tracing log 中沒有被記錄。
也就是說,response是到達(dá)客戶端的,但是沒有到達(dá).net中。
這是什么原因阿?
再后來,有個(gè)新發(fā)現(xiàn)。
我每次重新啟動(dòng)這臺pc后,然后第一次調(diào)用這個(gè)異步webservice方法,總是會在.net network trace log 中發(fā)現(xiàn)一下錯(cuò)誤信息:
System.Net.Sockets Error: 0 : [0452] Exception in the Socket#37489757::BeginReceive - The handle is invalid
然后再試著第2次,第3次調(diào)用,卻沒有再發(fā)現(xiàn)上面的錯(cuò)誤日志。
然后我再一次重新啟動(dòng)這臺pc,緊接著調(diào)用這個(gè)異步webservice方法,上面的錯(cuò)誤信息又被記錄了。
然后再試著第2次,第3次調(diào)用,還是沒有再發(fā)現(xiàn)上面的錯(cuò)誤日志。
然后再一次重新啟動(dòng),
。。。
但是,無論有沒有記錄以上錯(cuò)誤,每一次調(diào)用都是不成功的。
搜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
問題開始明了了!?。?img doc360img-src='http://blog.csdn.net/Editor/FCKeditor/editor/images/smiley/msn/teeth_smile.gif' alt="" src="http://blog.csdn.net/Editor/FCKeditor/editor/images/smiley/msn/teeth_smile.gif">
問題的原因:
可能有某個(gè)第三方的軟件修改或損壞了winsock層 (LSP - Layered Service Provider ),導(dǎo)致webservice 異步調(diào)用callback不返回?。?/p>
解決方案如下:
1。用msinfo32 命令查一下有沒有第三方的軟件修改了winsock2的注冊項(xiàng),如果有找出是什么軟件。
http://support.microsoft.com/kb/811259
2。卸載這個(gè)軟件
3。再試一下
4。問題解決