简体   繁体   English

如何强制ASP.NET分别处理用户的请求?

[英]How to Force ASP.NET to Handle User's Request respectively?

Is it possible to Handle simultaneous user requests respectively in ASP.NET WebApi? 是否可以分别在ASP.NET WebApi中处理并发用户请求? I don't want to ignore user request with throttling and I don't want to add user session cause I have User UUID. 我不想忽略用户请求,也不想添加用户会话,因为我有用户UUID。 Due to multi-threading issue I want user request to be added inside a queue and whenever it's previous request finished next one happen. 由于多线程问题,我希望将用户请求添加到队列中,并且每当上一个请求完成时,下一个请求就会发生。

Edit : I Have an Android app and using retrofit. 编辑:我有一个Android应用程序,并使用翻新。 It's possible that the client send two request at the same time and IIS will handle it in two different threads. 客户端可能同时发送两个请求,而IIS将在两个不同的线程中处理该请求。 I want user request with special UUID be handled respectively in the pipeline and not at the same time. 我希望具有特殊UUID的用户请求分别在管道中而不是在同一时间处理。

Edit : I want this in ASP.NET https://camo.githubusercontent.com/ab3d320b740b881b173a7f19b6e02a01326ad1dc/68747470733a2f2f7374726f6e676c6f6f702e636f6d2f77702d636f6e74656e742f75706c6f6164732f323031342f30312f746872656164696e675f6a6176612e706e67 编辑:我想要在ASP.NET中使用https://camo.githubusercontent.com/ab3d320b740b881b173a7f19b6e02a01326ad1dc/68747470733a2f2f7374726f6e676c6f6f702e636f6d2f77702d636f6e74656e742f75ec6f6164736673163

In order to queue up requests from a particular client you could use a named lock: 为了排队来自特定客户端的请求,您可以使用命名锁:

private readonly ConcurrentDictionary<string, object> lockDict = new ConcurrentDictionary<string, object>();

and then inside your server side handler get the user id and ensure that only a single thread can process it at a time: 然后在服务器端处理程序中获取用户ID,并确保一次只有一个线程可以处理它:

void ProcessRequest()
{
    string userId = GetUserIdFromRequest();
    lock (lockDict.GetOrAdd(userId, s => new object()))
    {
        // do the actual processing here and return response to the client.
    }
}

Note however that this approach will only work on a single server environment. 但是请注意,这种方法仅适用于单个服务器环境。 If you have server farms you will need to use a distributed named lock implementation instead. 如果您有服务器场,则需要使用分布式命名锁实现。

Also note that if you have hundreds of concurrent requests from the same client and those requests take time to execute this will block them up and jeopardize your worker threads on the server eventually impacting other requests. 还要注意,如果您有来自同一客户端的数百个并发请求,而这些请求要花一些时间才能执行,则这将阻塞它们,并危及服务器上的工作线程,最终影响其他请求。

Personally I would recommend you reviewing your current design and think a little bit more about a stateless architecture. 我个人建议您回顾当前的设计,并多考虑一些无状态架构。

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

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