sydneyos
sydneyos

Reputation: 4557

Can I call an MVC Action from a Windows Service?

I have an MVC Application in which users can send a large number of emails (each of which is customized, so no bulk send option). Rather than have the user wait for this, I'm queuing the emails to be sent on a schedule. What I would like to do is have a windows service make a periodic call to my application to trigger the emails. However, I really don't want to create a whole new web service project just for this one action. Is there any way to just wrap the behavior in an MVC Action and have the service call this action in a way similar to calling a web service (without needing to launch a browser window)? If not, is it possible to host a web service method inside an mvc application? If so, any good references on how to do this would be greatly appreciated. My searches have not been fruitful.

Upvotes: 3

Views: 2772

Answers (2)

stimms
stimms

Reputation: 44064

You might consider using the cache expiry to launch a task actually in your web application which would mean no need for any external process. https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/

Upvotes: 1

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171431

Sure you can just do a GET on the URL of your MVC app using the WebClient class. Something like:

string myUrl = "http://www.MySite.com/getemail";
WebClient wc = new WebClient();
string request = wc.DownloadString(myUrl);

Upvotes: 4

Related Questions