Calling an action method on predefined time - asp.net-mvc

I have an action method inside a controller class , that do a Sync . currently the user can manually call this action method, by clicking on a button from the view.
But my question is whether I can create a job or bath that runs on the host server let say each 1 hour and call this action method.
Currently I am using form authentication, and i am hosting my asp.net mvc web application on IIS 7
Thanks

There are a few ways of doing this.
Refactor the code out of MVC and put it inside a WCF service, configure this service to make use of http://quarts.net/ and setup the schedule to run. This service can then be hosted inside IIS.
You can also create a Windows Service (NT Service) that makes use of Quartz.Net. This service can then be installed on the production server.
You can create a batch file and use a windows task to fire off an exe that will run the job.

Related

How can background tasks be executed from a library in an ASP.NET MVC 5 app

In my job we are building Web Apps that rely on a common Enterprise class. This class has a method that sends a request to our server every time the app_start or app_end event triggers so we can monitor the status remotely. But we are now requiring that at least once a day the web app reports its status, a bit like telemetry. I don't know how to accomplish this, so far I have found some options, but some have limitations:
Use hangfire. I don't like this since it requires to setup a Database or add more tables and install a new Nuget package on each project, but could be my last option.
Use a Windows Service that reads databases. This could be less work but it can't access the Web App web.config code
Use a Javascript tasks that sends an AJAX request. This requires to have an open web browser and is a big risk.
I'm looking for a server side approach that could allow to set to trigger an event or function at 1am.
I would got with Hangifire.
It is dead easy to setup and very reliable.
You don't need to setup the database, you might want to check memory storage:
https://github.com/perrich/Hangfire.MemoryStorage
Also check:
What is the equivalent to CRON jobs in ASP.NET? - C#
You can use FluentScheduler instead of Hangfire (it is more lightweight).
Instead of a Javascript task that sends an AJAX request you can use a WebJob or an Azure Function.

Ninject Mvc Exception Handling at Controller Constructor

I have a MVC website that works in local but not on remote server.
The erroneous part is the MVC Webapi which uses ninject on controller's constructors.
The post request reaches the webapi while the webapi is working on local computer, and everything is fine.
However, same post request does not reach any method inside same API working on remote computer, and I need to know why.
My guess is that one of the dependencies fail while being injected by ninject.
Saving exception info inside a text file on remote server will do for now. I will implement a thorough solution later on.
Its been a long time, but as i remember , it turned out to be the proxy setting on remote machine.
Multiple api's were talking to eachother via http .
A proxy setting on remote machine , required authorization for these api's to talk to eachother .
Changing the code resolved the problem , otherwise using the proxy with proper authorization was another solution.
Thanks

Integrating Service stack with MVC and AngularJs

Using web.API with MVC seems very easy to setup, you leave the WebApiConfig.cs as default, create a new web.API controller, and then in your javascript you can call $http.get("api/...").
I want to be able to do the same using service stack instead of web.API. What makes it slightly more difficult is that the service stack api is in its own project. At the minute I can call the endpoints within MVC using the jsonServiceClient. However calling client side with Angular requires me to call the full FQDN which causes cross site scripting issues.
How can I configure my MVC project so that I can call my service stack endpoints from AngularJs's $http.get method like this:
$http.get("api/...")
rather than:
$http.get("http://localhost:2540/api/...")
You can accomplish this by creating an alias in IIS under your web project and naming it API and pointing to your ServiceStack project.
You may run into issues in the future with this if you need to scale out your solution, but as long as you don't need to do that, you should be ok.

Run a controller method at a fixed schedule within MVC?

We have an existing method in an existing controller that we'd like to call at a specific schedule (eg "daily at 2am"). The application is an MVC3 application running on Azure as a web role and we don't want to create, maintain and pay for an entire new role (worker role) just to run one small piece of identical logic.
Is it possible to schedule a controller method to trigger off at a specific scheduled time in the future? Also, would the same technique work in regular ASP.NET webforms?
Assuming you can just call this controller action with a URL, you can just...
1) create a PowerShell script to "ping" the website:
http://learn-powershell.net/2011/02/11/using-powershell-to-query-web-site-information/
2) Schedule that PowerShell script via remote desktop in a Scheduled Task that runs at 2am
You could also write a deployment script that automates #2.
You could use Phil Haack's WebBackgrounder as described here.
I've successfully used Cron jobs in a Shared hosting environment where scheduled tasks/powershell wasn't available.
Here's a website explaining more about it

Creating the same SignalR Hub for all the clients

I'm just start testing SignalR for monitoring application . I have a control which make a call to the Hub by the client side. I noticed , each time the client make Connection.Hub.Start() it creates a new Hub instance in the server , I need to refresh my control all the time , so I don't want it to create new Hub for each one.
Is there a way to create single Hub for all clients or I'm missing something?
A Hub instance is created for each request, much like an ASP.NET Page instance is created for each request in WebForms, a Controller is created for each request in ASP.NET MVC or a WCF service instance is created for each service request when using InstanceMode.PerCall.
If you want to maintain shared state between Hub requests/instances you would need to use a static field or some other, more advanced form of state sharing (e.g. dependency injected singleton).

Resources