【问题标题】:Multiple call using Web Api Service使用 Web Api 服务进行多次调用
【发布时间】:2014-08-28 17:11:35
【问题描述】:

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

这可能有两种情况。

  1. 单个设备多次请求
  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;
      });
}

对此的任何帮助将不胜感激。

【问题讨论】:

  • 人们似乎甚至在您将任何代码放入 int 之前就知道您的问题是什么 - 有 +2 upvotes - 他们应该提供一些信息......另一方面,403 看起来就像你不知道如何使用 POST 调用服务,或者您只是对名为 GetData 的 POST 方法感到困惑......
  • @Alexei 我担心问题不是赞成票,这只是我使用 POST 从设备获取数据并使用相同功能响应它们的方法的名称。跨度>
  • 有趣的部分是你如何称呼它(也可能是 Fiddler 所说的) - 403 不太可能是由你的存储库引起的,所以这部分并不是很有趣(你可能已经尝试用存根替换它无论如何都要进行实验......)
  • 我已经更改了您现在可能想要查看的添加的额外代码,如果您能对此提出任何建议。
  • 我完全不知道你的问题是什么...要求 )。弄清楚“Getting Error”到底是什么——即查看调用堆栈。如果使用家庭构建库,请考虑使用现有的日志库。祝你好运。

标签: c# linq asp.net-mvc-4 azure asp.net-web-api


【解决方案1】:

我找到了使用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 方法中记录所有服务的代码并等待帮助我等到任务完成。

【讨论】:

    【解决方案2】:

    从您的 api 服务提供数据调用的代码 sn-p 将是最方便的。它们是处理并发查询的几种方法;如果您还没有探索过它,async and await 方法最好在这种情况下加以利用。但这是从模糊的角度来看,我需要查看您的代码才能为您提供明确的答案。

    附加信息“如果您是异步编程的新手,或者不了解异步方法如何使用 await 关键字来执行可能长时间运行的工作而不阻塞调用者的线程,您应该阅读 Asynchronous Programming with Async and Await (C# and Visual Basic) 中的介绍。”

    希望这些信息能让您走上正轨。

    最好的。

    【讨论】:

    • 感谢您的回答,我已经用代码更新了我的问题,请看一次..
    • 我不相信这个问题是因为你的数据抽象方法。我相信您的问题与 CORS(跨源资源共享)有关,因为您返回了 403 Forbidden 错误。服务器正在确认请求,但由于权限原因不允许交换数据。本文提供了允许CORS with mvc 4 and web api.的分步配置
    • 好吧,让我检查一下,你能建议我当前的代码是否能够同时处理多个请求,因为我担心失败!
    • 我已经更改了您现在可能想要查看的额外代码,如果您能对此提出任何建议。
    • 查看您的最新代码 sn-p 似乎在您的方法中的任何地方都找不到等待类型。 await 运算符应用于异步方法中的任务,以暂停方法的执行,直到等待的任务完成。 bool result = await _serviceLogService.InsertLogs(request, myObjs);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多