简体   繁体   English

如何在ASP.NET MVC 3站点中执行后台作业?

[英]How to do background jobs in an ASP.NET MVC 3 site?

I'm currently working on an e-commerce site and there's one feature that i'm not very sure how to implement. 我目前正在开发一个电子商务网站,有一个功能我不太确定如何实现。 Most of the time you just add product(s) to your cart and buy them, that's probably the simplest workflow. 大多数情况下,您只需将产品添加到购物车并购买,这可能是最简单的工作流程。 What i'm asking is a little different, what if there's a time limit for a product to buy ? 我要问的是有点不同,如果购买产品有时间限制怎么办? I mean some sites give you an exact time limit to buy a product (like Soccer Manager), in those sites you can't hold a product forever, there's a 15 minutes limit for it and if you dont buy in that period, item will be released from your cart. 我的意思是一些网站给你一个确切的时间限制购买产品(如足球经理),在那些你不能永远持有产品的网站,它有15分钟的限制,如果你不买在那个时期,项目将从您的购物车中释放。 (and most probably someone else will jump on it) (而且很可能其他人会跳上它)

Now, as an ASP.NET MVC programmer i'd love to implement this feature but as i said, i'm not sure how to do it. 现在,作为ASP.NET MVC程序员,我喜欢实现这个功能,但正如我所说,我不知道该怎么做。 I think when i add item to cart i need to hold the time (something like ItemAddedAt) and i need to release that item in x minutes so something needs to run x minutes later to release that product. 我想当我将物品添加到购物车时我需要保留时间(类似于ItemAddedAt),我需要在x分钟内释放该物品,因此需要在x分钟后运行以释放该产品。 Globally thinking, i think i need a service, when i add an item, i also need to subscribe it to this service and service runs a timer/job in the background. 在全球范围内思考,我想我需要一项服务,当我添加一个项目时,我还需要订阅这个服务,服务在后台运行一个计时器/工作。 What i dont know/have no experience is this part, how to do that in an ASP.NET MVC project, is there a sample project, article, library or something like that ? 我不知道/没有经验的是这部分,如何在ASP.NET MVC项目中做到这一点,是否有一个示例项目,文章,库或类似的东西?

Of course i dont know if my logic is right for this problem, i need some guidance, if possible some source code to work on. 当然我不知道我的逻辑是否适合这个问题,我需要一些指导,如果可能的话,可以使用一些源代码。

AFAIK there are no standard way to declare/program tasks within an MVC project. AFAIK没有在MVC项目中声明/编程任务的标准方法。 The recommended way to accomplish what you want would be to create a new Console Application project within your solution, and use the Windows Task Scheduler to execute every X minutes, releasing any products that have been more than X minutes in any cart. 完成所需操作的推荐方法是在解决方案中创建一个新的控制台应用程序项目,并使用Windows任务计划程序执行每X分钟,在任何购物车中发布任何超过X分钟的产品。

For this to work, you will need to reference your MVC project from the new one (to get access to all the models) or, even better, create a Class Library project, move your Model/database classes there, and reference it from the MVC and Console projects. 为了实现这一点,您需要从新的MVC项目(访问所有模型)中引用您的MVC项目,或者更好的是,创建一个类库项目,在那里移动您的模型/数据库类,并从中引用它MVC和控制台项目。


All that being said, there is actually a small "hack" that can be used to get programmed tasks in an MVC project. 总而言之,实际上有一个小的“黑客”可用于在MVC项目中获得编程任务。 You can use the following code: 您可以使用以下代码:

HttpContext.Current.Cache.Add("Task", "1", null, 
            DateTime.MaxValue, TimeSpan.FromMinutes(5), 
            CacheItemPriority.Normal, new CacheItemRemovedCallback(CheckCarts));

That line, that could be called, for example, from the Global.asax, would add a "Task" entry to the cache. 例如,可以从Global.asax调用的那一行将向缓存添加“任务”条目。 The value stored ("1") is not important, the important thing is that the cache entry expires in five minutes and, when expired, calls the "CheckCarts" method (defined in the Global.asax, or in the class were you execute this code). 存储的值(“1”)并不重要,重要的是缓存条目在五分钟后到期,并在过期时调用“CheckCarts”方法(在Global.asax中定义,或者在类中执行)这段代码)。

public void CheckCarts(string key, object value, CacheItemRemovedReason reason) {
     // Insert your code here to check for expired carts
     (...)

     // We add the entry again to the cache, so that this method will be called again in 5 minutes.
     HttpContext.Current.Cache.Add("Task", "1", null, 
            DateTime.MaxValue, TimeSpan.FromMinutes(5), 
            CacheItemPriority.Normal, new CacheItemRemovedCallback(CheckCarts));
}

When the cache expires, the CheckCarts method is called, your code does whatever it has to do, and in the end adds itself to the Cache again, to be called in another 5 minutes. 当缓存过期时,会调用CheckCarts方法,您的代码会执行它必须执行的操作,并最终再次将自身添加到缓存中,以便在另外5分钟内调用。

This scenario just screams signalR. 这种情况只是尖叫信号R. It would allow you to do what you're asking in a simple way. 它可以让你以简单的方式做你想要的事情。

If the items in your cart have an expiry date you could have your view poll if any items have expired. 如果购物车中的商品有到期日,您可以在任何商品已过期的情况下进行查看。 If they have you could run the removal code for that item and update your ui. 如果有,您可以运行该项目的删除代码并更新您的UI。

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

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