tags:

views:

40

answers:

2

My ASP.NET MVC app needs to run a set of tasks at startup and in the background at a regular interval. I have implemented each task as a controller action and listed the app-relative path to the action in the database. I implemented a TaskRunner process that gets the urls from the database and requests each one at a regular interval using WebRequest.Create, but this throws a UriFormatException. I cannot use this answer or any code that plucks values from HttpContext.Current.Request without getting an HttpException with the message "Request is not available in this context". The Request object is not available because my code uses System.Threading.Timer to do background processing, as recommended here. Here are my questions:

  1. Is there really no way to make local web requests within an ASP.NET web app?
  2. Is there really no way to dynamically ascertain the root path to the web app even using static dependencies in ASP.NET?
  3. I was trying to avoid storing the app's root path in the database (as FogBugz does with its "Maintenance Path"), but is this best option?
A: 

ASP.NET MVC can be made to implement a RESTful API that can be called by outside services:

http://omaralzabir.com/create_rest_api_using_asp_net_mvc_that_speaks_both_json_and_plain_xml/

The article above talks about JSON, but you can implement the tasks you need run regularly as RESTful operations, executed simply by calling their URIs.

Dave Swersky
Thanks Dave. I have not read the article on the other side of the link, so forgive me if this is way off, but I cannot construct a URI because I don't know the app's base URI. Am I missing something?
flipdoubt
+1  A: 

You should have access to the current request in the Application_Start handler. Could you not simply inject the values you need to obtain for your TaskRunner class when you create it and simply reuse it from there instead of trying to get them each time?

protected void Application_Start()
{
    ...
    var baseUri = this.Context.Request.Url.GetLeftPart( UriPartial.Authority );
    var runner = new TaskRunner( baseUri );
    runner.Run();
    ...
}
tvanfosson
Just a note -- this won't work with a multitenant site if the actions differ for each tenant since it only grabs data from the first request.
tvanfosson
This works using the VS development server but I get the same `HttpException` when start the app in IIS 7.
flipdoubt