[英]Multiple call using Web Api Service
I am using Web Api
Service to Pass the Data to my Mobile Devices. 我正在使用
Web Api
服务将数据传递给我的移动设备。
There can be 2 scenario for this. 这可能有2种情况。
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
. 在两个场景中我都无法处理多个请求,我不知道实际问题是什么,但它一直给我
403 Response
和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. 我需要在几秒钟内处理多个请求 ,因为我同时处理超过1000个设备并且并排我还需要在几秒钟内响应它们因为我们不希望我们的设备等待响应多于几个秒。
Currently I am using Azure platform for the Web Api services and working with MVC 4.0 Using LINQ. 目前,我正在使用Azure平台进行Web Api服务,并使用LINQ使用MVC 4.0。 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. 任何帮助都会得到很多赞赏。
Providing a code snippet of your data calls from your api service would be most handy. 从api服务提供数据调用的代码片段将是最方便的。 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.
如果你还没有探索过它,那么在这种情况下最好利用async和await方法。 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) ." 如果你是新来的异步编程或不理解异步方法是如何使用的await关键字做可能会长时间运行的工作,而不会阻塞调用者线程附加信息”,你应该阅读引入异步编程与异步和等待(C#和Visual Basic) 。“
Hopefully, this information puts you on the right track. 希望这些信息能让您走上正轨。
Best. 最好。
I found the Solution using Async
Method to Handled Recurring Request of Services 我找到了使用
Async
方法处理重复的服务请求的解决方案
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. 我的控制器无法启动上下文文件作为请求它已经启动和“并发请求”死锁我的对象,因为我从设备获得多个请求,以便我修改了他的代码以在
SendAsync
方法中记录所有服务和等待帮助我等到任务完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.