使.net Web API队列请求操作为“单线程" [英] Make .net web api queue requests operate 'single threaded'

查看:47
本文介绍了使.net Web API队列请求操作为“单线程"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个c#.net Web API服务调用代码,该代码不能一次处理多个数据库请求.该系统用于在线支付需求相对较小的账单.

We've got a c# .net Web API service calling code that is not able to handle more than one database request at a time. The system is for online payments of bills with a relatively small demand.

我们无权控制进行更改的代码,而该更改将解决此问题.使用相同代码的另一个小组使用WCF API和服务配置将并发请求限制为1,从而有效地解决了该问题.现在,我们必须重新编写WCF或弄清楚如何使Web API起作用.

We don't have control of the code to make the change which would fix the issue. Another group using the same code used WCF API and a service configuration to limit concurrent requests to 1, effectively fixing the issue. We now have to re-write in WCF or figure out how to make Web API work.

是否有一种方法可以使Web API排队并一次处理一个请求,同时仍使最终用户可以实时"操作这些事情?

Is there a way to make the Web API queue up requests and process them one at a time, while still keeping things operating in 'real-time' for the end users?

对不起,如果我含糊不清,我代表实际的程序员问我,希望能提供帮助.

Sorry if this is vague, I am asking on behalf of the actual programmer in hopes of assisting.

推荐答案

我知道的唯一方法是自己动手".IE.:

Well only way I'm aware of is "do it yourself". I.E. :

  • 将正在处理请求的部分移动到某个共享组件(如果还没有这样的组件)
  • 在持久性存储(db,cloud ...)中创建一个用于排队操作的表,在其中存储传入的请求(应该很容易做到,因为该请求必须以某种方式可序列化-我会选择xml进行持久性存储)
  • 更改您的服务,因此无需处理请求,它只会验证请求并进行验证.将请求存储到持久性存储(db)中并返回
  • 创建用于异步处理请求的新程序集(如果您使用的是Windows基础结构,通常是窗口服务),它将一次从持久性存储(db)提取请求,处理请求并记录故障

或者,您可以将服务更改为使用fire&忘记带有锁定"语句的模式以限制对付款部分的访问-例如:

Alternatively you can change the service to use fire & forget pattern with "lock" statement to limit access to payment part - like:

private static object s_SyncRoot = new object();

...
new Task(() =>
{
  lock (s_SyncRoot)
  {
    // process payment - single thread
  }
}).Start();
return "ok";
...

哪个是快速的&易于实现-但是您牺牲了一点可靠性-例如,参见 Fire and不用ASP.NET MVC .同样,它的扩展性也不太好(如果出现偷看请求的情况,并且您打开的并行线程过多,则会有效地降低您的API服务

Which is quick & easy to implement - however you're sacrificing reliability a bit - see for example Fire and Forget with ASP.NET MVC. Also it doesn't scale well (if there will be peek of requests & you'll open too many parallel threads it can effectively take down your API service

这篇关于使.net Web API队列请求操作为“单线程"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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