gpt4 book ai didi

c# - 使用Web Api服务进行多次调用

转载 作者:行者123 更新时间:2023-11-30 16:11:11 24 4
gpt4 key购买 nike

我正在使用 Web Api 服务将数据传递到我的移动设备。

可能有两种情况。

  1. 单个设备多次请求
  2. 多个设备多次请求

在这两种情况下,我都无法处理多个请求,我不知道实际问题是什么,但它一直给我 403 Response500 Response.

我需要在内处理多个请求,因为我同时并排处理超过 1000 个设备,我还需要在几秒钟内响应它们因为我们不希望我们的设备等待响应的时间超过几秒。

目前,我正在使用 Azure 平台作为 Web Api 服务,并使用 LINQ 来使用 MVC 4.0。如果您想要我的代码,那么我将为您提供代码(我在我的项目中使用存储库模式)

代码

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
}
}

存储库:

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) 中的介绍。”

希望这些信息能让您走上正确的道路。

最好。

关于c# - 使用Web Api服务进行多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24623821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com