1

As part of the project we have implemented ASP.Net Web API, which returns the Json data, which is consumed by the Javascript using Angular JS on the client.

Controller code is straight forward (Trimmed description):

public class CardController : ApiController
{
    // code
    [HttpGet]
    public CardDataGetUI GetCardDataUI(int userID, int dashBoardID, int cardID)
    {  
          // Access the application Cache object using HttpRuntime (System.Web.Caching)
            var blCache = HttpRuntime.Cache;

            // Create a user specific BL access key by concatenating the user ID
            string userBLAccessKey = WebAPIConstant.BlUserDashboardCard + userID;

            // Access the BL object stored in the Cache
            accessBL = (Bl)blCache[userBLAccessKey];

          // Other Code

           // Fetch the data for the control being passed
            cardDataUI = accessBL.GetCardDataUI(dashBoardID, cardID);

          return (cardDataUI)
    }
 }

The above mentioned GetCardDataUI delivers the card data for different type of control like chart, map and grid on a same UI screen, so what UI does is make an Asynchronous call to all in one go, currently I have BL (business layer) object being accessed from application wide cache, which is an issue for Multi threaded access, as they would share same object, so I have converted that to a local copy and initialized the one for each call to the controller. However that is also good enough till the each ajax call is having it's unique controller instance to call the method. However in this case it seems the http call they make have same instance thus modifying the input variable of each call thus leading to unexpected result and exception, since it is modifying the internal DS at run time. It is akin to calling the static method

Ideally I did not expected a multi-threaded call to the business layer, but it seems in Angular JS client has to make such calls, they cannot be synchronous.

  • Currently I have resolved the situation by introducing a lock in the controller, which certainly allows one thread at a time

However was looking for a solution like each Ajax call can have it's own controller instance, when it make the http get call.

We also have an option of modifying the above mentioned controller method like:

public CardDataGetUI[] GetCardDataUI(int userID, int dashBoardID, int[] cardID)
{
   // Code
}

In this case there will be one call for all cards and I will call the data fetch in a for loop, thus synchronizing the operation, but this is not much different from locking the controller, preferable will be a separate controller instance for each AJAX call

Any suggestion?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.