使用Web Api服务进行多次呼叫 [英] Multiple call using Web Api Service

查看:86
本文介绍了使用Web Api服务进行多次呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这可能有两种情况。



1.单次设备请求多次

2.多次设备请求多次



在两种场景中我无法处理多个请求,我不知道实际问题是什么,但它一直给我403响应。



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



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



代码

控制器:

I am using Web Api Service to Pass the Data to my Mobile Devices.

There can be 2 scenario for this.

1. Single Device Requesting Multiple Times
2. Multiple Device Requesting Multiple Times

In Both the Scenario i am not able to handle multiple Request, I don't know what is the actual problem but it's keep giving me the 403 Response.

I need to handle Multiple request within seconds as I am Dealing with more then 1000 Devices at the same time and side by side i also need to respond them within seconds because we don't want that our devices wait for the Response for more then few Seconds.

Currently I am using Azure platform for the Web Api services and working with MVC 4.0 Using LINQ. If you want my code then i will provide you the Code (I am using repository Pattern in my Project)

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





存储库:



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







任何帮助都会得到很多赞赏。




Any Help in this would be Much Appreciated.

推荐答案







根据你的描述和你的代码我明白了



1.您的服务器超出了最大请求限制。 (原因)

2.你在插入时使用了交易*( The Killer

3.你正在处理一个相当大的代码块用于随机访问目的。(范例



交易将锁定表,直到它结束其工作,当我们调用commit时,它会将数据从其事务日志发送到最终表。因此,在一个操作结束之前,我们已经有一些100-300个请求等待同一个表,并且随着时间的推移其余的请求将进入超时并且服务器变得繁忙,并且您得到 403 [ ^ ]。



现在该如何消除它。



在我继续之前你必须管理每个系统都有它自己的执行能力,直到它能够或能够执行它。



为什么我们需要事务?如果你的SQL提出某种与数据相关的错误lback操作,否则它将提交它。

您可以在插入数据之前提供验证,因为您害怕引发错误。

创建 Staging 表首先在验证后插入数据并运行一些异步作业将数据从分段移动到最终。



它会给你至少60%-70性能提升%。



现在可以节省各种各样的人 WCF [ ^ ]是更好的方法,因为您要使用多种设备类型,然后REST全服务将是合适的。



这里是一篇很好的文章,从REST服务开始:创建RESTful WCF服务API:循序渐进指南 [ ^ ]



谢谢,

Suvabrata
Hi,


As per your description and your code I have understand that

1. Your server exceeds the max request limit. (Reason)
2. You have used Transaction while Insert * (The Killer)
3. You are handling a quite a big code block for random access purpose.( Paradigm

Transaction will lock the table until it end its work and when we call commit it will marge data from its transaction log to final Table. So till one operation ends we already have some 100-300 request pending for same table and as time pass rest of requests are going to Timeout and Server became busy and you get a error of 403[^].

Now how to eliminate that.

Before I proceed something you must admin every system has its own capability to perform so it will perform till it can or able.

Why we required Transaction ? If your SQL raise some kind of data related error it will rollback the operation else it will commit it.
You can provide validation before going to insert the data where you have fear to raise error.
Create a Staging Table first insert data to that after validation and run some asynchronous job to move data from staging to Final.

It will give you at least 60%-70% performance increase.

Now to save a wide range of people WCF[^] is more better approach as you are going for multiple device types then REST Full Service will be appropriate.

here Is nice article to start with REST service : Create RESTful WCF Service API: Step By Step Guide[^]

Thanks,
Suvabrata


这篇关于使用Web Api服务进行多次呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆