c#-使用Web Api服务进行多次通话
作者:互联网
我正在使用Web Api服务将数据传递到我的移动设备.
可能有2种情况.
>单个设备多次请求
>多个设备多次请求
在两种情况下,我都无法处理多个请求,我不知道实际的问题是什么,但是它一直给我403响应和500响应.
我需要在几秒钟内处理多个请求,因为我要同时处理超过1000台设备,并排并排,所以我还需要在几秒钟内响应它们,因为我们不希望我们的设备等待响应那么多的设备秒.
目前,我正在将Azure平台用于Web Api服务,并使用LINQ与MVC 4.0一起工作.如果您想要我的代码,那么我将为您提供代码(我在我的项目中使用存储库模式)
码
控制器:
[HttpPost]
public JObject GetData(dynamic data)
{
JObject p = new JObject();
try
{
MyModelWithObjectInData transactionbatchData = JsonConvert.DeserializeObject<MyModelWithObjectInData>(data.ToString());
return _batchDataService.batchAllDataResponse(transactionbatchData);//Call 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
});
我已经使用下面的代码来跟踪通过服务发出的所有请求,但是我在其中遇到了错误.
//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;
});
}
在这方面的任何帮助将不胜感激.
解决方法:
提供从api服务进行数据调用的代码段将非常方便.它们是处理并发查询的几种方法.如果您尚未探索它,那么在这种情况下,最好使用async and await方法.但这是从一个模糊的角度来看,我需要查看您的代码以为您提供明确的答案.
附加信息“如果您不熟悉异步编程,或者不了解异步方法如何使用await关键字来进行可能长时间运行的工作而不阻塞调用者的线程,则应阅读Asynchronous Programming with Async and Await (C# and Visual Basic)中的介绍.”
希望这些信息能使您走上正确的道路.
最好.
标签:azure,asp-net-web-api,asp-net-mvc-4,linq,c 来源: https://codeday.me/bug/20191121/2052479.html