Groups | Blog | Home
all groups > asp.net webservices > june 2007 >

asp.net webservices : Asynchronous Call Pattern Help Needed


lucius
6/24/2007 11:49:26 PM


I am trying to make a simple Asynchronous Web Service work under .NET
2.0.
My code looks like this so far, can someone help me think through
this?
I want a web service client to make a request with a unique ID through
the Test2 WebMethod, let it set up an object that will do a lot of
long-running work through its "Execute" method, and with the result
ending up in Application Cache, and the caller can repeatedly call
GetTest2 to
get the result when it comes.




delegate MyCommandClass GetAsyncResults( string reqId );


[WebMethodAttribute]
public IAsyncResult BeginTest2( string sessionId , AsyncCallback cb ,
object state )
{
MyCommandClass ff = new MyCommandClass();
ff.SessId = sessionId;
GetAsyncResults oAsync = new GetAsyncResults( "Execute()" );
return oAsync.BeginInvoke( sessionId , cb , state );
}

[WebMethodAttribute]
public void EndTest2( IAsyncResult ress )
{
AsyncResult ar = (AsyncResult)ress;
GetAsyncResults results = (GetAsyncResults)ar.AsyncDelegate;
MyCommandClass myC = (MyCommandClass)results.EndInvoke( ress
);
Application[ myC.SessId ] = myC;
}

[WebMethodAttribute]
public string GetTest2( string sess )
{
MyCommandClass theClass = (MyCommandClass)Application[ sess ];
}




In real life, the actual implementation will be very different, right
now I am just tring to get my head around async web service work.

lucius
6/25/2007 9:18:50 AM

I am not sure what you mean by

" .NET webservice actually doesn't support distributed callback "

because it seems to be done in several places with examples I have
found on blogs and CodeProject web site. I just want my implementation
to work. Can you please show me how to fix things with the sample I
have provided? I think I am very close to making it work.

If you are sure what I am doing cannot be done, please provide a
working code sample how to implement what I need to do.

Thanks.



On Mon, 25 Jun 2007 11:55:49 GMT, stcheng@online.microsoft.com (Steven
[quoted text, click to view]
stcheng NO[at]SPAM online.microsoft.com
6/25/2007 11:55:49 AM
Hi lucius,

From your descritption, I understand that you want to build an ASP.NET
webservice which will support an asynchronous service processing pattern.
The client can explicitly call webmethod to start a asynchronous
server-side long-run task, and constantly ping the task status and get
result at the end, correct?

Based on the code snippet you provided, you define your webservice's
webmethod as a .NET async method's signature(with AsyncCallback as
parameter and return IAsyncResult). However, I think you do not need to do
so since .NET webservice actually doesn't support distributed callback
mechnism. So if you want to provide an asynchronous webservice pattern, you
can simply expose all those webservice webmethods that are requied to make
your task be able to process asynchronouly but they're still implemented as
normal synchronous webmethods. For example, you can have the following web
methods:

**InitializeTask() which setup a server-side context and return a sessionID
to client

**StartTask(sessionID, TaskInfo) which can start a background and long-run
task at server-side and associate it with a certain session

**QueryTaskStatus(sessionID, TaskID) query task processing status

**GetResult......


All these methods are normal ASP.NET webmethods but the combining of them
provide a asynchronous pattern service procesing model. How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.


lucius
6/25/2007 12:55:31 PM



I have made some changes to my asynchronous handling, and it does
compile, but hitting [F5] in Visual Studio to test gives this
exception:



What is my mistake? Here is the code now (I have a seperate class def
for MyClassAbc, it is a great-grandchild inheritor of
System.Data.DataTable).



public object objectState;
public MyclassExecuteAsyncStub executeAsyncStub;
public delegate MyClassAbc MyclassExecuteAsyncStub( MyClassAbc
myClassAbc );

public MyClassAbc MyclassExecute( MyClassAbc myClassAbc )
{
myClassAbc.Execute();
return myClassAbc;
}

public class AbcState
{
public object objectState;
public MyclassExecuteAsyncStub executeAsyncStub;
}

[WebMethodAttribute]
public IAsyncResult BeginTheWork( string workId , AsyncCallback cb ,
object s )
{
MyClassAbc abc = new MyClassAbc();
abc.WorkID = workId;
MyclassExecuteAsyncStub stub = new MyclassExecuteAsyncStub(
MyclassExecute );
AbcState ms = new AbcState();
ms.objectState = s;
ms.executeAsyncStub = stub;
return stub.BeginInvoke( abc , cb , ms );
}

[WebMethodAttribute]
public MyClassAbc EndTheWork( IAsyncResult call )
{
AbcState ms = (AbcState)call.AsyncState;
return (MyClassAbc)ms.executeAsyncStub.EndInvoke( call );
}


lucius
6/26/2007 3:16:11 PM


Please consider this issue "closed". Your response was not exactly
helpful, and my comments appeared to be ignored after that, but I did
finally arrive at a working solution using an asynchronous Begin/End
call pattern for my long-running web services.

stcheng NO[at]SPAM online.microsoft.com
6/28/2007 12:43:40 PM
Hi Lucius,

For the webservice call, it is different from a normal local asynchronous
method call(such as delegate.BeginInvoke). If you return an IAsyncResult
object in webservice webmethod, at client-side you can not correctly call
endInvoke method or recieve notification when method complete because there
is no implemetnation for such asynchronous call to work in distributed
scenario. That's why I suggest you implement all your webmethod calls as
normal synchronous method but combine them to provide asynchronous service.
Anyway, as you've worked out a solution on this, you're welcome to share
the code here so that anyone else have the similar issue may also benefit
it.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


AddThis Social Bookmark Button