[英]Multiple call using Web Api Service
我正在使用Web Api
服務將數據傳遞給我的移動設備。
這可能有2種情況。
在兩個場景中我都無法處理多個請求,我不知道實際問題是什么,但它一直給我403 Response
和500 Response
。
我需要在幾秒鍾內處理多個請求 ,因為我同時處理超過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和await方法。 但這是一個模糊的觀點,我需要查看您的代碼,以便為您提供明確的答案。
如果你是新來的異步編程或不理解異步方法是如何使用的await關鍵字做可能會長時間運行的工作,而不會阻塞調用者線程附加信息”,你應該閱讀引入異步編程與異步和等待(C#和Visual Basic) 。“
希望這些信息能讓您走上正軌。
最好。
我找到了使用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;
}
我的控制器無法啟動上下文文件作為請求它已經啟動和“並發請求”死鎖我的對象,因為我從設備獲得多個請求,以便我修改了他的代碼以在SendAsync
方法中記錄所有服務和等待幫助我等到任務完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.