开发者

How can I request local pages in the background of an ASP.NET MVC app?

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开发者_如何学C avoid storing the app's root path in the database (as FogBugz does with its "Maintenance Path"), but is this best option?


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();
    ...
}


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜