Monday, June 6, 2011

Hybrid approach of using Sync and Async actions in AsyncController

Hi All,

Today i am going to discuss about an intersting aspect or a window of opportunity which opens up when we use and AsycnController, As you might know, an AsyncController class can contain methods /actions which are of type Synchronous or Asynchronous. i.e In an AsyncController class we can have both Synchronous and Asynchronous methods.




Having said that, we also need to stick to one rule, that is if you have a Synchronous method with the name MethodCompleted – then you cant call that method since it wont be having the matching MethodAsync and if you try to call MethodCompleted without the matching MethodAsync the call will be blocked.




Inspite of all these restriction, if you still want to make use of the same ActionName – MethodCompleted – then you can have a method with some different name but decorate the action with the ActionName(“MethodCompleted”)




ie
[ActionName(“MethodCompleted”)]
public ActionResult SomeMethodName()
{
}




One another note, you cant explicitly call http://localhost/MethodAsync either through an ActionLink or By using any other Redirections – we always need to do those action invoking by using http://localhost/Method

I hope this helps!.




Regards,




-Vinayak

Timeouts in Actions of AsyncController

Hi All,

Welcome back to the blog post. Today we will look at the Timeouts in action when using Async actions.

By default an action will be timed out after 45 seconds in Asp.Net mvc, If a method takes more than 45 seconds then it will throw an TimeoutException.
In order to control this behaviour Asp.Net team provide us 2 filters – [AsycnTimeout] and [NoAsyncTimeout].
If you make use of [NoAsyncTimeout] attribute to an action, then TimeoutException will never be thrown from an action.
AsyncTimeout works by taking the expiry time parameter as an input( in Milli seconds), As you know, if the action takes more than the specified time, it throws TimeoutException.

eg:
[AsyncTimeout(50000)] /* Expires after 50 seconds */
public void SampleAsync()
{
}
[NoAsyncTimeout] //Never expires
public void SampleAsync()
{
}

I hope this helps!


Regards,


-Vinayak

Usage of filters in AsyncControllers

Hi,

Today i would like discuss about usage of filters in AsynControllers.

Filters such as [Authorize], [OutputCache], [ActionName] ,[AcceptVerbs] should be kept on top of ActionAsycn () method than ActionCompleted(). By mistakley if you keep the filter on top of ActionCompleted() – then it will be ignored

Now i would like to discuss one more important aspect, when we use ActionName along with the Asycn Methods. Then also we need to share the same prefix for the method name, i.e ActionAsycn and ActionCompleted.
One small twist, when we use the ActionName filte for any method, as usual the url should follow the ActionName path than the method name,
for instance,
[ActionName(“Path”)]
public void SampleAsync()
{
}

public ActionResult SampleCompleted()
{
}

the request like – http://localhost//Sample – will result into Http 404.

The real path is – http://localhost/Path .

One more last thing, as you know the view name should be Path.aspx than Sample.aspx.

I hope this helps.

Regards,
-Vinayak

Friday, June 3, 2011

Choosing AsyncController vs Controller in Asp.Net mvc

Hi All,
I hope last post was really handy to those who want to implement AsyncController in Asp.Net mvc, today lets see what are all the parameters helps in deciding Synchronous and Asynchronous Controller.

Parameters
1) When you have very simple method - Then prefer Controller class than AsyncController
2) When the method is more oriented towards CPU intensive task - prefer Controller class
3) When the method is more oriented towards I/O or network intensive task - prefere AsyncController class
4) When you prefer simplicity than efficiency prefere Controller class
5) When you perefer maintainability prefer Controller class.
6) When you want your request thread to be Non blocked - prefere AsyncController

I hope this helps you to make better decission duriing your design phase..

Regards,
-Vinayak

AsyncController in Asp.Net MVC








Guys,
Here goes the first entry of 'About my experience with Asp.Net mvc and Windows Azure series'.
I had an requirement, where in for most of the actions, i was suppose to fetch the data from the database through WCF Services but not at the cost of blocking/reducing the throughput of Asp.net mvc application....


Then i was thinking like in order to have Non blocking/high throughput - i need to make use of some asynchronous api from .Net to do the task such as either ThreadPool's QueueUserwork items , or Delegates or some asynchronous reader kind of api's....But then i surfed internet and found that there is a specific way by which this can be achieved in Asp.Net mvc......
Here are the steps by which we can get it working....


1. First create the set of WCF Services which will get the data from the database.
2. By right clicking the Asp.Net MVC project, add the service reference pointing to the wcf project.
3. During the step 2 - click on the advanced button and check 'generate asynchronous operations'
4. Inside the Asp.Net mvc project, ensure that controller class is derived from AsyncController than the default Controller class
5. For the method, Ensure that ActionName ends with 'ActionAsync' - for instance 'indexAsync'.
6. Ensure that the return type of 'ActionAsync' is void than the default ActionResult, because this method executes asynchronously.
7. Create one more method corresponding to the ActionAsync with the name - 'ActionCompleted' for instance 'indexCompleted' where in the result is returned once the action executes successfully.
8. Inside indexAsync - make a call to the wcf proxy on the asynchronous methods...for instance proxy.GetDataAsync();
9. For the callback of wcf proxy ie proxy.GetDataCompleted() - hook up the event handling by using the special class from asp.net mvc - 'AsyncManager' as shown in the following image.
10. AsyncManager's parameter class help to pass the value to the method
11. As you could see in the image, we also need to increment and decrement the async operation count - which we should be doing by using AsyncManager class.


Thats it, we achieved what we intended to.....


I hope this helps!...


If you have any queries , please do let me know..


Regards,
-Vinayak







Experience with the new technology stack from Microsoft

Hi Guys,

Please excuse me for not writing for such a long time. Was completely stuck with one of the new development project consisting of Asp.Net MVC 3.0 and Windows Azure....Now i have decided to share my experience with the latest technology stack from Microsoft( which i have used in this development assignment)....so tune to blog to hear more about in coming series......

Regards,
-Vinayak