Re: Asynchronous Call Pattern Problem #2
- From: lucius <lucius@xxxxxxxxxxxxxxxx>
- Date: Tue, 17 Jul 2007 20:05:43 -0400
Here is a sample of the entire .asmx.cs, hopefully it will be a little
clearer.
Thanks for looking again.
In this hopefully cleaned-up example, webservice client will call
"MyReq" and pass a semicolon-delimited string. The string will be
broken up and new DataSets will be created. The DataSets will be
populated by an instance of the "Invoker" class. The population can be
a very lenghy operation. The webservice client will get an immediate
return of "nothing" and can do other work while the webservice server
does its job. When the webservice server (Invoker instance) is done,
the result will be put into Application Cache. The client can then use
a different webmethod (not shown in this code) to get the data from
Application Cache and use it.
I was hoping to implement Invoker's work asynchronously. But it
appears that the caller is being blocked and has to wait for the work
to complete.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Caching;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
[WebService( Namespace = "http://myisht.com/testws" )]
[WebServiceBinding( ConformsTo = WsiProfiles.BasicProfile1_1 )]
[ToolboxItem( false )]
public class MyOps : BaseWebService
{
public object prevInvokerState;
public InvokerStub transExecuteAsyncStub;
public delegate List<DataSet> InvokerStub( Invoker transInvoker ,
List<DataSet> setList );
public List<DataSet> TransInvokerExecute( Invoker transInvoker ,
List<DataSet> setList )
{
using ( transInvoker )
{
foreach ( DataSet dataSet in setList )
{
transInvoker.DoWork( dataSet );
// the above line might make a web service call to a Java WS in
Europe and populate the DataSet
// the above line might make a local SQL Server call and
populate the DataSet
// it does not matter what the call does, it will be lenghty and
I do not want the client to wait
// transInvoker.DoWork is not "running in the background", it is
blocking, making the client wait for it to finish.
}
}
return setList;
}
public class TransInvokerExecutionState
{
public object prevInvokerState;
public InvokerStub transExecuteAsyncStub;
}
[WebMethodAttribute]
public IAsyncResult BeginMyReq( string requestValues , AsyncCallback
asyncCallBack , object memoryStateStorage )
{
List<DataSet> setList = new List<DataSet>();
string[] requestData = requestValues.Split( ';' );
for ( int rdC = 0 ; rdC < requestData.Length ; rdC++ )
{
setList.Add( new DataSet(requestData[rdC]) );
}
// The DataSet has a name and Invoker will populate it based on the
name
Invoker transInvoker = new Invoker();
transExecuteAsyncStub = new InvokerStub( TransInvokerExecute );
TransInvokerExecutionState transLocalState = new
TransInvokerExecutionState();
transLocalState.prevInvokerState = memoryStateStorage;
transLocalState.transExecuteAsyncStub = transExecuteAsyncStub;
return transExecuteAsyncStub.BeginInvoke( transInvoker , setList ,
asyncCallBack , transLocalState );
}
[WebMethodAttribute]
public void EndMyReq( IAsyncResult completedResult )
{
TransInvokerExecutionState transLocalState =
(TransInvokerExecutionState)completedResult.AsyncState;
List<DataSet> completedList =
(List<DataSet>)transLocalState.transExecuteAsyncStub.EndInvoke(
completedResult );
Global.AddToCache( completedList );
}
}
.
- References:
- Asynchronous Call Pattern Problem #2
- From: lucius
- Re: Asynchronous Call Pattern Problem #2
- From: John Saunders [MVP]
- Asynchronous Call Pattern Problem #2
- Prev by Date: Re: Include Verbose Description?
- Next by Date: Re: Asynchronous Call Pattern Problem #2
- Previous by thread: Re: Asynchronous Call Pattern Problem #2
- Next by thread: Invalid hexidecimal characters
- Index(es):
Loading