3

I am using Web Api Service to Pass the Data to my Mobile Devices.

There can be 2 scenario for this.

  1. Single Device Requesting Multiple Times
  2. Multiple Device Requesting Multiple Times

In Both the Scenario i am not able to handle multiple Request, I don't know what is the actual problem but it's keep giving me the 403 Response and 500 Response.

I need to handle Multiple request within seconds as I am Dealing with more then 1000 Devices at the same time and side by side i also need to respond them within seconds because we don't want that our devices wait for the Response for more then few Seconds.

Currently I am using Azure platform for the Web Api services and working with MVC 4.0 Using LINQ. If you want my code then i will provide you the Code (I am using repository Pattern in my Project)

Code

Controller :

[HttpPost]
public JObject GetData(dynamic data)
{
   JObject p = new JObject();

   try
    {
      MyModelWithObjectInData transactionbatchData = JsonConvert.DeserializeObject<MyModelWithObjectInData>(data.ToString());
      return _batchDataService.batchAllDataResponse(transactionbatchData);//Call Repository 
    }
}

Repository :

public JObject batchAllDataResponse(MyModelWithObjectInData oData)
{
   JObject p = new JObject();
   var serviceResponseModel = new MyModelWithObjectInData();
   using (var transaction = new TransactionScope())
   {
     //Insert in Tables
   }

//Select Inserted Records
var batchData = (from p in datacontext.Table1
                 where p.PK == oData.ID select p).FirstOrDefault();

 if (batchData != null)
    {
      serviceResponseModel.GetBatchDataModel.Add(new BatchDataModel //List Residing in MyModelWithObjectInData Class File
       {
            //add values to list
       });       
    }

//Performing 3 Operations to Add Data in Different List (All 3 are Selecting the Values from Different Tables) as i need to Give Response with 3 Different List.

 return p = JObject.FromObject(new
        {
            batchData = serviceResponseModel.GetBatchDataModel,
            otherdata1 = serviceResponseModel.otherdata1, //list Residing in my MyModelWithObjectInData Model 
            otherdata2 = serviceResponseModel.otherdata2 //list Residing in my MyModelWithObjectInData Model
        });

I have used below code to track all the request coming through the Service but i am getting error within this.

//here i am getting error
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
   //var content = request.Content.ReadAsStringAsync().Result;
   ServiceTypes myObjs = JsonConvert.DeserializeObject<ServiceTypes>(request.Content.ReadAsStringAsync().Result);

    bool result = _serviceLogService.InsertLogs(request, myObjs); //Getting Error while inserting data here on multiple request
    return base.SendAsync(request, cancellationToken).ContinueWith((task) =>
      {
        HttpResponseMessage response = task.Result;
        return response;
      });
}

Any Help in this would be Much Appreciated.

cracker
  • 4,900
  • 3
  • 23
  • 41
  • People seem to know what your question is about even before you put any code in int - with +2 upvotes - they should have provided some information... On other hand [403](http://en.wikipedia.org/wiki/HTTP_403) simply looks like you don't know how to call service with POST or you just confusing yourself with POST method called GetData.... – Alexei Levenkov Jul 08 '14 at 05:09
  • @Alexei I am worrying with the Problem not with the upvotes, It's just name of the method nothing else i am using POST to Get the Data from Device and also responding them using the same function. – cracker Jul 08 '14 at 05:13
  • The interesting part is how you call it (and possibly what Fiddler says) - 403 is unlikely caused by your repository, so that part is not really interesting (and you likely already tried to replace it with stub for experiments anyway...) – Alexei Levenkov Jul 08 '14 at 06:59
  • I have changed the added extra code you might want to look now, if you can suggest me any idea with this. – cracker Jul 08 '14 at 11:55
  • I'm completely lost on what your problem is... Some tips: clarify for yourself if all or just some request fail with 403, make sure your service runs fine locally (including sending expected volume of requests ). Figure out what exactly "Getting Error" is - i.e. look at callstack. Consider using existing logging libraries if using home-build one. Good luck. – Alexei Levenkov Jul 08 '14 at 14:37
  • As i am requesting multiple times on same service it is giving me the response of 500 now. The Exact Problem is "concurrent requests" that dead locking my objects from executing while performing the insert operations – cracker Jul 09 '14 at 04:18
  • 500 is clearly totally different from 403... Likely you have expected deadlock due to synchronous call to Wait/Result on task in ASP.Net. Now it looks like duplicate of http://stackoverflow.com/questions/13140523/await-vs-task-wait-deadlock which have also link to Stephen Cleary articles. – Alexei Levenkov Jul 09 '14 at 16:43

2 Answers2

3

Providing a code snippet of your data calls from your api service would be most handy. Their are a few ways to handle concurrent queries; if you have not explored it yet the async and await method would be best to leverage in this scenario. But this is from a vague stand point I would need to look at your code to provide you a definitive answer.

Additional information "If you're new to asynchronous programming or do not understand how an async method uses the await keyword to do potentially long-running work without blocking the caller’s thread, you should read the introduction in Asynchronous Programming with Async and Await (C# and Visual Basic)."

Hopefully, this information puts you on the right track.

Best.

Drisan James
  • 146
  • 9
  • Thanks for the Answer, and i have updated my question with the code plz look it once.. – cracker Jul 08 '14 at 05:06
  • I do not believe this issue is because of your data abstraction methods. I believe your issue is with CORS (Cross-Origin Resource Sharing) since you are returned a 403 Forbidden error. The server is acknowledging the request but, is not allowing data to be exchanged due to permissions. This article provides a step by step configuration of allowing [CORS with mvc 4 and web api.](http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api) – Drisan James Jul 08 '14 at 05:27
  • Ok let me check this then, and can you suggest me that my current code is able to handle multiple request at the same time or not as i am afraid with the failure!! – cracker Jul 08 '14 at 05:33
  • I have changed the extra code you might want to look now, if you can suggest me any idea with this. – cracker Jul 08 '14 at 11:56
  • 2
    Looking at your latest code snippet there does not appear to be an await type found anywhere within your method. **The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes.** `bool result = await _serviceLogService.InsertLogs(request, myObjs);` – Drisan James Jul 09 '14 at 16:25
0

I found the Solution using Async Method to Handled Recurring Request of Services

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
  //Logic to Track all the Request
  var response = await base.SendAsync(request, cancellationToken);
  return response;
}

My Controller in not able to initiate the context file as request it's already initiated and "concurrent requests" that dead locking my objects because i am getting multiple request from the devices so that i have modified he code to Logs all the Service within SendAsync Method and await helps me to wait until the task completed.

cracker
  • 4,900
  • 3
  • 23
  • 41