Click here to Skip to main content
15,307,797 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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.

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 :
C#
[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 :
C#
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
        });


//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.
Posted
Updated 8-Jul-14 0:05am
v2

1 solution

Hi,


As per your description and your code I have understand that

1. Your server exceeds the max request limit. (Reason)
2. You have used Transaction while Insert * (The Killer)
3. You are handling a quite a big code block for random access purpose.( Paradigm

Transaction will lock the table until it end its work and when we call commit it will marge data from its transaction log to final Table. So till one operation ends we already have some 100-300 request pending for same table and as time pass rest of requests are going to Timeout and Server became busy and you get a error of 403[^].

Now how to eliminate that.

Before I proceed something you must admin every system has its own capability to perform so it will perform till it can or able.

Why we required Transaction ? If your SQL raise some kind of data related error it will rollback the operation else it will commit it.
You can provide validation before going to insert the data where you have fear to raise error.
Create a Staging Table first insert data to that after validation and run some asynchronous job to move data from staging to Final.

It will give you at least 60%-70% performance increase.

Now to save a wide range of people WCF[^] is more better approach as you are going for multiple device types then REST Full Service will be appropriate.

here Is nice article to start with REST service : Create RESTful WCF Service API: Step By Step Guide[^]

Thanks,
Suvabrata
   
Comments
ashuslove 8-Jul-14 2:26am
   
Yeah Thanks for the Answer but how can i eliminate the Transactions as i am inserting data into Multiple Table and Need to roll back the Transaction in case of any error occurred while performing the operations!
A Nice option is to use the Staging Table will think on that.
Suvabrata Roy 8-Jul-14 2:38am
   
That's why I have told you to create some validation before insert and make same staging Tables for those final tables when user call your function insert data to your staging tables and then create some asynchronous job to move data from staging to Final.

Do need more help on it?
ashuslove 8-Jul-14 2:44am
   
I can go for the validations before inserting the data but i am playing with the LINQ and it's all about Foreign Key Constraints. How can i validate it?
Suvabrata Roy 8-Jul-14 2:50am
   
If you are using LINQ then forget about the Foreign Key just use Cascade Insert Like :http://msdn.microsoft.com/en-us/library/bb386931(v=vs.110).aspx

You can Insert in different staging tables without foreign key and then you use that asynchronous job to do the rest for performance boost
ashuslove 8-Jul-14 3:01am
   
Many Thanks i am trying that now only. :)
Suvabrata Roy 8-Jul-14 3:02am
   
Welcome...
ashuslove 8-Jul-14 6:05am
   
Hi Roy, I have checked that and what i am getting is i can request multiple times but after certain request it is giving me the 500 Response and i am getting error in My Log Handler let me post that in my question for you. plz refer it once.
Suvabrata Roy 8-Jul-14 8:45am
   
500 error code refer to some internal error. Its mean you code behind throwing some exception while run and it not cached any ware so please check your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900