I am creating a WebService webmethod, which takes an input parameter (order data), validates the order data and returns a response.
But right before it returns the response, it must start processing the order data (saving it to database tables etc.) in a new thread. But after it start, it must immediately return the response to the caller. How could I implement this (asynchronously,
using localOrder.RegisterOrderActions())?
[WebMethod]
public BLL.LDM.OrderResponseMessageType CreateOrderForActions(BLL.LDM.Order Order)
{
//create Order object
BLL.Order localOrder = new BLL.Order(Order);
//create and fill response message
BLL.LDM.OrderResponseMessageType responseMessage = localOrder.CreateOrder();
//send response message
return responseMessage;
//this should start right before returning, not causing the return to wait until RegisterOrderForActions is finished...
localOrder.RegisterOrderActions();
}
Probably I found it, using the following code. Is this the right way to do it?
WebMethods returns to the caller and a new thread was kicked off to do additional work.
[WebMethod]
public BLL.LDM.OrderResponseMessageType CreateOrderForActions(BLL.LDM.Order Order)
{
//create Order object
BLL.Order localOrder = new BLL.Order(Order);
//create and fill response message
BLL.LDM.OrderResponseMessageType responseMessage = localOrder.CreateOrder();
//use threading
WaitCallback wcb = new WaitCallback(delegate { localOrder.RegisterOrderActions(); });
ThreadPool.QueueUserWorkItem(wcb);
//send response message
return responseMessage;
}
Marked as answer by hetnet on Apr 28, 2009 07:36 PM
There are some things to be aware of. First of all, your callback may not use any instance members of the web service. This is because, after returning, the instance will have Dispose called on it. It is illegal to reference members once the class they're
in has been Disposed.
Second, be careful not to access any data that could be modified by a different thread. Such data will require locking in order to prevent problems.
hetnet
Member
201 Points
107 Posts
How to start an async task right before returning from a WebMethod?
Apr 23, 2009 03:30 PM|LINK
I am creating a WebService webmethod, which takes an input parameter (order data), validates the order data and returns a response.
But right before it returns the response, it must start processing the order data (saving it to database tables etc.) in a new thread. But after it start, it must immediately return the response to the caller. How could I implement this (asynchronously, using localOrder.RegisterOrderActions())?
[WebMethod] public BLL.LDM.OrderResponseMessageType CreateOrderForActions(BLL.LDM.Order Order) { //create Order object BLL.Order localOrder = new BLL.Order(Order); //create and fill response message BLL.LDM.OrderResponseMessageType responseMessage = localOrder.CreateOrder(); //send response message return responseMessage; //this should start right before returning, not causing the return to wait until RegisterOrderForActions is finished... localOrder.RegisterOrderActions(); }kavita_khand...
Star
9767 Points
1930 Posts
Re: How to start an async task right before returning from a WebMethod?
Apr 24, 2009 12:14 PM|LINK
Have u ever considred using Threading?
I would love to change the world, but they wont give me the source code.
hetnet
Member
201 Points
107 Posts
Re: How to start an async task right before returning from a WebMethod?
Apr 24, 2009 12:59 PM|LINK
Probably I found it, using the following code. Is this the right way to do it?
WebMethods returns to the caller and a new thread was kicked off to do additional work.
[WebMethod] public BLL.LDM.OrderResponseMessageType CreateOrderForActions(BLL.LDM.Order Order) { //create Order object BLL.Order localOrder = new BLL.Order(Order); //create and fill response message BLL.LDM.OrderResponseMessageType responseMessage = localOrder.CreateOrder(); //use threading WaitCallback wcb = new WaitCallback(delegate { localOrder.RegisterOrderActions(); }); ThreadPool.QueueUserWorkItem(wcb); //send response message return responseMessage; }johnwsaunder...
Star
11262 Points
1981 Posts
Re: How to start an async task right before returning from a WebMethod?
Apr 27, 2009 12:19 PM|LINK
There are some things to be aware of. First of all, your callback may not use any instance members of the web service. This is because, after returning, the instance will have Dispose called on it. It is illegal to reference members once the class they're in has been Disposed.
Second, be careful not to access any data that could be modified by a different thread. Such data will require locking in order to prevent problems.