Web servive asynchronous call fails

N

nirk

Hi
I created a webservice with a simple we method to increment a number passed from the client(a console app) .
Then i call this web service method asynchronus mode from the client.
I create a callback function and call EndXXX(XXX denotes the function name) in the callback function.
But the function call never ends. means the web service is not working in async mode
I have added the web refernce properly because i was able to succesfully call the same function in synchronous mode

Where am i going wrong

Thanks in advance
Nirk
 
J

Jan Tielens

Ok, can you post some code how you implemented your async. call?

Maybe you can read following article:
http://weblogs.asp.net/rosherove/archive/2003/10/26/33545.aspx

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan


nirk said:
Hi
I created a webservice with a simple we method to increment a number
passed from the client(a console app) .
Then i call this web service method asynchronus mode from the client.
I create a callback function and call EndXXX(XXX denotes the function
name) in the callback function.
But the function call never ends. means the web service is not working in async mode
I have added the web refernce properly because i was able to succesfully
call the same function in synchronous mode
 
N

nirk

Hi Jan
The web service code is one of yur articles in the code project.Client i created in VS.net envoirment.

CLIENT CODE
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static bool bEnd= false;


public static void PrimeCallback(IAsyncResult arResult)
{
// Obtains the last parameter of the delegate call.

Console.WriteLine ("Inside PrimeCallback");
// get the "this" pointer that was passed...
WSPrime wsp=(WSPrime)arResult.AsyncState;

// call "EndPrime" and get the result..
int iCount=wsp.EndPrime(arResult);

/// show the result of the webservice call..
Console.WriteLine("Async. WebService call found {0} primes.", iCount);

// set flag so that application can terminate...
bEnd=true;

}
[STAThread]
static void Main(string[] args)
{

try
{
int intTemp;
IAsyncResult ar;
Console.WriteLine("Calling WebService Asynchronously...\n");
AsyncCallback ascb = new AsyncCallback (Class1.PrimeCallback);
//for (int i =2;i<=10;i++)
{
WSPrime wsp = new WSPrime();
//intTemp = wsp.Prime (10);
wsp.BeginPrime(10,ascb,wsp);

}


Console.WriteLine ("waiting for WebService");
while(bEnd == false);
Console.WriteLine ("Over" );
Console.ReadLine ();
}

//
// TODO: Add code to start application here
//
catch(System.Exception exception)
{
Console.WriteLine (exception.Message);
}
}


};

WEBSERVICE CODE
using System.Web.Services;

public class WSPrime : WebService
{
// This method returns the number of prime number lying between
// 2 and num.
[WebMethod]
public int Prime(int num)
{
int iCount=0;
for(int i=2;i<num;i++)
{
bool bPrime=true;
for(int j=2;j<i;j++)
{
// is this number prime ?
if (i%j==0)
{
// nope.. it isn't...
bPrime=false;
break;
}
}

if (bPrime==true)
iCount++;
}

// return the count..
return iCount;
}
}
 
N

nirk

H
The solution did not help either
If i compile the webservice client(Class1.cs), by referencing the webservice proxy assembly, as given belo
csc /r:wsprime.dll Class1.c
where wsprime.dll is same folder in which Class1.cs is there i am easily able to get the asynchronous call but from the actual exe(the console application) i fail to do

Pls advice
 
S

Sami Vaaraniemi

I see a couple of problems in the code.

You have a busy loop in the main thread: while(bEnd == false); This probably
does not prevent the asynchronous call from completing because the thread
pool threads run by default with the same priority as your main thread.
Nevertheless a busy loop like this is considered bad. Add a call to
Thread.Sleep to the loop.

A possible problem that could make your program hang is that there is an
exception when you call the EndPrime method. Since you don't handle the
exception and it occurs in the threadpool thread, it just disappears
silently. To find out what the problem is, add a try/catch to your
asynchronous callback:

public static void PrimeCallback(IAsyncResult arResult)
{
try
{
// Obtains the last parameter of the delegate call.

Console.WriteLine ("Inside PrimeCallback");
// get the "this" pointer that was passed...
WSPrime wsp=(WSPrime)arResult.AsyncState;

// call "EndPrime" and get the result..
int iCount=wsp.EndPrime(arResult);

/// show the result of the webservice call..
Console.WriteLine("Async. WebService call found {0} primes.", iCount);

// set flag so that application can terminate...
bEnd=true;
}
catch (Exception ex)
{
Console.WriteLine("Web Service call failed: " + ex.ToString());
bEnd = true;
}
}

If there is an exception, at least this helps you see what the problem is.
Sami

nirk said:
Hi Jan
The web service code is one of yur articles in the code project.Client i created in VS.net envoirment.

CLIENT CODE
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static bool bEnd= false;


public static void PrimeCallback(IAsyncResult arResult)
{
// Obtains the last parameter of the delegate call.

Console.WriteLine ("Inside PrimeCallback");
// get the "this" pointer that was passed...
WSPrime wsp=(WSPrime)arResult.AsyncState;

// call "EndPrime" and get the result..
int iCount=wsp.EndPrime(arResult);

/// show the result of the webservice call..
Console.WriteLine("Async. WebService call found {0} primes.", iCount);

// set flag so that application can terminate...
bEnd=true;

}
[STAThread]
static void Main(string[] args)
{

try
{
int intTemp;
IAsyncResult ar;
Console.WriteLine("Calling WebService Asynchronously...\n");
AsyncCallback ascb = new AsyncCallback (Class1.PrimeCallback);
//for (int i =2;i<=10;i++)
{
WSPrime wsp = new WSPrime();
//intTemp = wsp.Prime (10);
wsp.BeginPrime(10,ascb,wsp);

}


Console.WriteLine ("waiting for WebService");
while(bEnd == false);
Console.WriteLine ("Over" );
Console.ReadLine ();
}

//
// TODO: Add code to start application here
//
catch(System.Exception exception)
{
Console.WriteLine (exception.Message);
}
}


};

WEBSERVICE CODE
using System.Web.Services;

public class WSPrime : WebService
{
// This method returns the number of prime number lying between
// 2 and num.
[WebMethod]
public int Prime(int num)
{
int iCount=0;
for(int i=2;i<num;i++)
{
bool bPrime=true;
for(int j=2;j<i;j++)
{
// is this number prime ?
if (i%j==0)
{
// nope.. it isn't...
bPrime=false;
break;
}
}

if (bPrime==true)
iCount++;
}

// return the count..
return iCount;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top