我有一个依赖循环发送Rest请求的应用程序。根据请求的数量,这可能是我请求数据的15到75个产品。
API仅接受一个参数,我不能传递一组ProdID。因此,我陷入了这种循环情况。
我重写了所有方法以异步运行,以期提高性能,但是这样做使服务器不堪重负,并出现以下错误:
{"response":"BUSY","responseFor":"getInventory","responseMessage":"The system is too busy. Please try again later."}
根据他们的开发人员的说法,如果我可以使用一个会话ID,它将把数据返回给我,但是如果apache服务器认为我是每个请求的唯一用户,它将拒绝这些请求。
在这里您可以看到ID是唯一的。
Set-Cookie: JSESSIONID=459158DC7432D2C11FFB1047C6B4F6B5; Path=/ppro; Secure
Set-Cookie: JSESSIONID=B788E26663E378CF107E032595AA9B4A; Path=/ppro; Secure
我尝试将客户端传递给API请求,但是无论我做什么,它始终具有唯一的JESSIONID。任何帮助将不胜感激。我将发布一些代码。
这是请求开始的初始控制器:
public async Task<ActionResult> InventoryDueOut_Read([DataSourceRequest]DataSourceRequest request, string prodNumOverride, string lotNum, DateTime dateSelection)
{
var results = loadSchedulingRepo.GetOrdersDueOut(dateSelection);
var inventory = await backHaulService.GetInventoryForDueOutOrdersAsync(results, prodNumOverride, lotNum, dateSelection);
return Json(inventory.ToDataSourceResult(request));
}
此处服务开始执行Rest请求:
private RestClient _client;
public BackhaulService()
{
loadSchedulingRepo = new LoadSchedulingRepository();
_client = new RestClient("https://xxxxxx.xxxxxxxxxx.com");
_client.CookieContainer = new CookieContainer();
}
private async Task<List<InventoryProdAndSubsVM>> GetProductInventoryAsync(IEnumerable<BackhaulTopVM> prodCodes, DateTime userDate)
{
//Only take unique ProdCodes
var uniqueProductCodes = prodCodes.GroupBy(code => code.ProdNum).Select(g => g.FirstOrDefault());
//Create tasks to get inventory for the product codes.
var prodCodeTasks = uniqueProductCodes.Select(product =>
loadSchedulingRepo.GetInventoryForOrdersAPIAsync(product.ProdNum, userDate, _client)
);
//wait for all the tasks to complete
var inventory = await Task.WhenAll(prodCodeTasks);
//grab all items returned from the async tasks
var resultSet = inventory.SelectMany(items =>
items.Select(item => new InventoryProdAndSubsVM
{
Commodity = item.Commodity,
DueIn = item.DueIn,
OnHand = item.OnHand,
Committed = item.Committed,
ProdCode = item.ProdCode,
ProdNum = item.ProdNum,
ProdDesc = item.ProdDesc
})
).ToList();
return resultSet;
}
我们在这里调用API:
public async Task<IEnumerable<InventoryProdAndSubsVM>> GetInventoryForOrdersAPIAsync(string prodNum, DateTime? userDate, RestClient client)
{
var bhService = new BackhaulService();
var invResults = await invAPI.GetInventory(client, "0009", null, null, null, prodNum, "O");
//Update the resultset with the ExcludeFIFOAlloc so we know what lots to exlude from the result set on the inventory screen.
if (invResults != null)
{
var results = invResults.responseData.lots.Where(x => x.ExcludeFIFOAlloc == "N").GroupBy(l => l.productNum).Select(x => new InventoryProdAndSubsVM
{
ProdDesc = x.Max(y => y.productDesc),
ProdCode = x.Max(y => y.product),
ProdNum = x.Key,
Committed = x.Max(y => y.Committed),
//Committed = invResults.responseData.lots.Where(w => w.ReceiveDate != null).Sum(z => Convert.ToDecimal(z.commitQty)),
DueIn = invResults.responseData.lots.Where(w => w.ExpectedDate == userDate).Sum(z => Convert.ToDecimal(z.orderedQty)),
OnHand = x.Sum(y => y.OnHand),
Condition = x.Max(y => y.condition),
Commodity = x.Max(y => y.commodity)
});
return results;
}
return null;
}
在这里,我将RestClient传递给我的API包装器:
public class InventoryAPI
{
private RestRequest request;
public async Task<RootLotsObject> GetInventory(RestClient client, string company, string buyer, string vendCode, string lotNum, string prodNum, string status)
{
request = new RestRequest("ppro/services", Method.POST);
//var cancelTokenSource = new CancellationTokenSource();
request.AddParameter("appId", "APIINV");
request.AddParameter("command", "getInventory");
request.AddParameter("username", "jraynor");
if (company != null) { request.AddParameter("company", company); }
if (buyer != null) { request.AddParameter("buyer", buyer); }
if (vendCode != null) { request.AddParameter("vendor", vendCode); }
if (lotNum != null) { request.AddParameter("lotNum", lotNum); }
if (prodNum != null) { request.AddParameter("prodNum", prodNum); }
if (status != null) { request.AddParameter("status", status); }
//, cancelTokenSource.Token
IRestResponse<RootLotsObject> response = await client.ExecuteTaskAsync<RootLotsObject>(request);
request = null;
return response.Data;
}
}
我意识到这是很多相对复杂的代码,但是任何想法都是有帮助的。