By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 2,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

Something strange happened when asynchronously calling a web servi

P: n/a
I've just studied the "how to" web service and the async pattern in donnet.I
make a test with these knowledges,but I got a strange result. Here is my
test.
1.Write a simple "Add" service named MathWS( the class name),and put it in a
..asmx file named math.asmx in the "D:/winnt/interpub/wwwroot/mathws"
directory. "mathws" is my new-built directory.
2.run the http://localhost/mathws/math.asmx,find it works well,and run run
the http://localhost/mathws/math.asmx?wsdl,view source,copy the wsdl source
code to a file name math.wsdl,and I also put it in the mathws directory.
3.open command line window,key in command "disco
http://localhost/mathws/math.wsdl",ok,now a file named mathws.wsdl is
created under my now directory(the directory where I run disco command).
4.Still in the directory I type in "wsdl mathws.wsdl", a mathws.cs file is
created and it also has a MathWS class.This is my proxy class file which
include a synchronous "Add" method and two asynchronous asynchronous
BeginAdd and EndAdd method.the latter is used for callback.They accept two
int numbers and return their sum.
5.Now I am ready to write my client class two call the web service.Since the
EndAdd method return a int value instead of a "void one",so I write a class
"MathWSChild" which derives from the MathWS class,and add a new method just
like this:
public class MathWSChild:MathWS{
public void OutputResult(System.IAsyncResult asyncResult){
Console.WriteLine(this.EndAdd(asyncResult));
//*****Console.WriteLine("hello.");*******//
}
}
Now,I can use this method as the callback method.and the whole calling codes
maybe like this:
using System;

public class MathClient{

public static void Main(){
MathWSChild mathWSChild = new MathWSChild();
AsyncCallback cb = new AsyncCallback

(mathWSChild.OutputResult);
IAsyncResult asyncResult = mathWSChild.BeginAdd

(120,130,cb,new Object());
Console.ReadLine();
}

}
public class MathWSChild:MathWS{
public void OutputResult(System.IAsyncResult asyncResult){
Console.WriteLine(this.EndAdd(asyncResult));
/*****Console.WriteLine("hello.");*******/
}
}

I save these codes in a file mathclient.cs ,and now i have two files in
client directory,mathws.cs which includes my proxy class and the
mathclient.cs which includes the calling codes.

Maybe it will work well? After some necessary compiling ,just like this:
csc /t:library mathws.cs
csc /r:mathws.dll mathclient.cs

Now,I typed "mathclient.exe" in command line,
the "_" flashed,I know the code"Console.ReadLine();" worked , but nothing
else is outputed.when i cllick "Enter",the program ends.
I 've thought there is some buffer problems which I met when I studied the
async pattern,so I add " Console.Out.Flush()" after the
Console.WriteLine(this.EndAdd(asyncResult)); in the OutputResult method,but
no help.
finally, I add a line of code just like I wrote in the comment part in my
codes:
/*****Console.WriteLine("hello.");*******/
A miracle happened, two lines are outputed:

250
hello.
_

Of course the "Console.WriteLine("hello.");" maybe just a
"Console.WriteLine("");"
but I wonder why this could happened,and how to resolve this problem?After
all,I want to see the "250" without saying "hello" to the computer.

Thanks greatly for your help.

Nov 23 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.