C# 5.0 Features : Async Programining

5 Ways of Doing Asyncronous programming.

  1. Async Vs Sync.
    1. Syncronous web request 
      1. makes threads waiting for the request to return. 
        1. This position aggrevates when the services are slow or services are timing out due to some problem in services.
      2. for example : lets say service is a waiter in a restaurent and request is client ordering for food then. incase of syncronous service the waiter will keep waiting while the client eats food and leave from there.
    2. Async Web request
      1. makes threads request for the service call and it gets relieved as soon as call is made to the service. so, that it can cater to other requests.
        1. And when the service returns the data a free thread will pick up the returned value and will return the result to the client origionally requested for it.
      2. for example : lets say service is a waiter in a restaurent and request is client ordering for food then. incase of asyncronous service the waiter will go to  client2  while the client1 eats food and will getback once client1 needs anything else. hence same waiter is able to cater to more clients in the restaurant.
  2. How C# 5.0 await Async really works.
    1. How Sync call works in Asp.net MVC
        1. public void Index()
        2. {
          1. var WebClient = new WebClient()
          2. string[] result = WebClient.DownloadStrings();
          3. return View("Index", new ViewStringModel
                    {
                        results = result
                    });
          4. }
    2. How Async works in Asp.net MVC 3.
      1. In this case you need to inherit your controller from AsyncController.
      2. you have to split your action method into two parts.
        1. one is suffixed with Async and other one is suffixed with completed.
        2. For example 
          1. public void IndexAsync()
          2. {
            1. var WebClient = new WebClient();
            2. AsyncManager.OutstandingOperations.Increment();
            3. WebClient.DownloadStringCompleted+=(sender,evt)
            4. {
              1. AsyncManager. Parameters["result"] = evt.Result;
              2. AsyncManager.OutstandingOperations.Decrement();
            5. }
            6. WebClient.DownloadStringAsync();
          3. }
          4. public ActionResult IndexCompleted(string[]  result) {
                    return View("Index", new ViewStringModel
                    {
                        results = result
                    });
                }
    3. How Async works in Asp.net MVC 4 with c# 5.0
      1. In this case you need to modify the method in thei manner
          1. public  async Task  Index()
          2. {
              1. var WebClient = new WebClient()
              2. var result = await WebClient. DownloadStringAsync();
              3. return View("Index", new ViewStringModel
                        {
                            results = result
                        });
          3. }
  3. Managing task with TPL.
  4. Push Events --- How they work.
  5. Simplifying with SignalR.

Comments

Popular posts from this blog

Authorize.net Integration eCommerce Payment Gateway ( Direct Post Method New! )

Get Organised with OneNote

Test your Sql Query Online