0

I need to run a program that fetches info from a REST API every 10 seconds and then posts it to an Action in my MVC 5 Web App.

So far, the best solution I've come up with is to run the Web App and then, parallel to it, a Console Application that works as this middleware, fetching the API and Posting to the Action on the Web App.

I figured a better solution would be to execute the code that powers the Console App directly inside the MVC 5 App, this way only the Web App would be executed, and the code could work on the background, fetching and posting the data.

I noticed that when you create a new project, and select Console Application, you can add it to an existing Solution. Would this enable the behavior described above?

How can I achive this behavior on the ASP.NET MVC 5 framework?

Jorge Cuevas
  • 755
  • 3
  • 10
  • 30
  • Why not have your console application fetch the data from the REST API AND do the data processing for you? Your MVC app is acting like a middleman in this scenario. – Crackerjack Mar 25 '17 at 19:03
  • @Crackerjack It is not, the MVC app requires to receive the data through the action so that it can stream the changes using Websockets. The data flow is as follows: Fetch API, post JSON to Web App, Save changes to the database, and finally stream the changes to all the clients in real time. – Jorge Cuevas Mar 25 '17 at 19:12
  • Does your console app have access to the database? If so your flow would be Fetch API, parse JSON, Save changes to the database from console app then. If not you can definitely add your console app to your solution and then use the WebClient class to post data to your MVC app, see http://stackoverflow.com/questions/5401501/how-to-post-data-to-specific-url-using-webclient-in-c-sharp for an example – Crackerjack Mar 26 '17 at 17:26
  • @Crackerjack What I ended up doing yesterday was almost exactly what your second approach suggests, I created a console app in my solution, but instead of using the WebClient Class I created a WebRequest. Would the WebClient class be a more efficient solution? – Jorge Cuevas Mar 26 '17 at 21:25
  • 1
    I think WebClient will give you more flexibility down the road, but using WebRequest just to post data is a perfectly fine solution. – Crackerjack Mar 27 '17 at 17:25

0 Answers0