简体   繁体   English

Web API服务 - 如何使服务器上的请求同时执行

[英]Web API Service - How to make the requests at the server to be executed concurrently

I am using a WebApi rest service controller, hosted by IIS 7.5, as i understood from this post: 我正在使用由IIS 7.5托管的WebApi休息服务控制器,我从这篇文章中了解到:

Are all the web requests executed in parallel and handled asynchronously? 是否所有Web请求都是并行执行的并且是异步处理的?

A webApi service, by default, executes all its incoming requests in parallel, but only if the current multiple requests (at a certain time) came from different sessions. 默认情况下, webApi服务并行执行所有传入请求, 前提当前多个请求 (在特定时间)来自不同的会话。 That is to say, if single client will send some simultaneously requests to server, all of them will be executed sequentially and won't be executed concurrently . 也就是说,如果单个客户端将同时向服务器发送一些请求,则所有这些请求将按顺序执行,并且不会同时执行

This behavior is a real problem for us, because in some cases, our client sends bunch of requests from different client's listeners, asynchronously (by browser), and all of them will actually be queued instead of being executed concurrently at the server. 这种行为对我们来说是一个真正的问题 ,因为在某些情况下,我们的客户端会异步地(通过浏览器)从不同客户端的侦听器发送大量请求,并且所有这些请求实际上都会排队,而不是在服务器上同时执行 Therefore, in some cases, we experiencing a serious performance issues which are really noticeable at the client's web page. 因此,在某些情况下,我们遇到了严重的性能问题 ,这些问题在客户的网页上非常明显。

How can we solve this problem? 我们怎样才能解决这个问题? I understand we can maybe disable session state but that isn't a normal thing to do. 我知道我们可以禁用会话状态,但这不是正常的事情。

Actually, disabling session state is the normal solution for web APIs. 实际上,禁用会话状态是Web API的常规解决方案。 If you need it for some/all of your calls, you can call HttpContext.SetSessionStateBehavior (eg, from Application_BeginRequest ). 如果您需要一些/所有呼叫,可以调用HttpContext.SetSessionStateBehavior (例如,从Application_BeginRequest )。 Multiple read-only session state requests can run concurrently. 多个只读会话状态请求可以并发运行。

Do you try async Task ? 你尝试异步任务吗? Here is sample Controller: 这是示例控制器:

public class SendJobController : ApiController
    {
        public async Task<ResponseEntity<SendJobResponse>> Post([FromBody] SendJobRequest request)
        {
            return await PostAsync(request);
        }

        private async Task<ResponseEntity<SendJobResponse>> PostAsync(SendJobRequest request)
        {
            Task<ResponseEntity<SendJobResponse>> t = new Task<ResponseEntity<SendJobResponse>>(() =>
            {
                ResponseEntity<SendJobResponse> _response = new ResponseEntity<SendJobResponse>();

                try
                {
                    //  
                    // some long process
                    // 
                    _response.responseStatus = "OK"; 
                    _response.responseMessage = "Success";
                    _response.responseObject = new SendJobResponse() { JobId = 1 };

                }
                catch (Exception ex)
                {
                    _response.responseStatus = "ERROR"; 
                    _response.responseMessage = ex.Message;
                }

                return _response;
            });

            t.Start();
            return await t;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM