Reputation: 4906
I am using Web Api
Service to Pass the Data to my Mobile Devices.
There can be 2 scenario for this.
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.
Upvotes: 3
Views: 6019
Reputation: 4906
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.
Upvotes: 0
Reputation: 156
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.
Upvotes: 3