开发者

ASP.NET MVC3 web and background processes

I have a web project written in ASP.NET MVC3. Part of my web application is used for some image saving and it 开发者_StackOverflow中文版runs pretty slow. So after trying to save 100 images (some other work is done with them too) it goes slowly and the user has to wait a lot in the browser.

I was thinking about desiging a background service (process) which I would send some data to and it will run in the background not slowing the use of the website. To be concrete, I need a service which works with a single queue of strings. Web application can send a new string in the queue anytime, and the service has to do some work with the strings in the queue.

I have no idea where to start or what to use. Any suggestions? :)


it seems like you know the basic process you want, get a request from the application, pass data off to a wcf service, return the appropriate view. the wcf works in the background. it is more tricky if you're wanting to notify the user that made the request that the background processing is complete, but you could always have the wcf service write to a common message table a message for the user that the mvc app checks.


There could be a number of different ways to design something like this.

One way could be to host a WCF service in a Windows Service that manages background jobs and has a service interface with methods like:

Guid StartJob(...) //returns new jobId<br/>
SomeInfoObject CheckJobStatus(Guid jobId)

This would allow your web application to poll.. using Ajax, you could display a progress bar to the user if you were so inclined (the client would make Ajax requests to your web app, which would in turn call the service for progress info).

Another way would be to write task information to a database to schedule new jobs. A windows service (or even scheduled task that kicks off a process) could be used to complete the jobs and update the database as jobs complete.


Any form of queue would work here. Check out RabbitMQ or MSMQ. Basically all you want is somewhere to store a message that you want something else to work on.

You can use a BackgroundWorker or something - make it static and create it on the Application_Start in the global.asax. I think though that you are best off having some form of mechanism that isn't going to get torn down on a website restart or anything.

Another option is just to put the value into a db then you can easily track the status and feedback on it.


Why not consider Asynchronous request handling before going the custom background service approach.

In MVC 3 you can build Asynchrouns Controller pretty easily, by implementing AsyncController. Sometimes if we do not have a clear vision as to how to proceed and confronted with myriad choices, reusing the goodies from the CLR is a better choice. Asynchronous Controller leverages the IIS worker thread pool, transforming all heavy long running tasks to background worker threads freeing the other threads and therefore alleviating the performance problem you're having. Coding-wise it is not that cumbersome.

For an example you can check out this link:

Using an Asynchronous Controller in ASP.NET MVC

Again my suggestion is to explore the native MVC Asynchronous approach before looking elsewhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜