2

I have ASP.NET web service project. I must "switch off" (return void or null) the all methods in this service if current time is between 00:00 and 01:00. What is the best way to do it? may be global.asax file?

Wachburn
  • 2,842
  • 5
  • 36
  • 59
  • 1
    Can you simply take the services offline (e.g. by turning off the web-server), which will cause the client to receive an exception? – RB. Oct 08 '12 at 08:21
  • 1
    @RB. I can't turn off the hardware. Web servise's methods just must do nothing – Wachburn Oct 08 '12 at 08:32
  • There's lots of different ways to take the web-services offline. What I was trying to ask is "Is it ok if calls to your web-services receive exceptions, rather than a void/null response"? – RB. Oct 08 '12 at 09:24

1 Answers1

2

You can do that on Global.asax, on Application_BeginRequest, just check if is a webservice and then if not allow that hours you just cut it.

protected void Application_BeginRequest(Object sender, EventArgs e)
{
   string cTheFile = HttpContext.Current.Request.Path;
   string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);
   // here you can check what file you wish to cut, ether by file, ether by extention
   if (sExtentionOfThisFile.Equals(".asmx", StringComparison.InvariantCultureIgnoreCase))
   {
     // and here is the time limit.
     if(DateTime.UtcNow.Hour >= 0 && DateTime.UtcNow.Hour <= 1)
     {
        HttpContext.Current.Response.TrySkipIisCustomErrors = true;
        HttpContext.Current.Response.StatusCode = 403;
        HttpContext.Current.Response.End();
        return ;    
    }    
  }
}
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • This is the idea, you can stop by check the extention all your services, or you can just grab the one file only, the one that keep a specific class of web service. – Aristos Oct 08 '12 at 08:35
  • This is the best way of doing this globally, to have the methods still work & return nothing is unlikely without going into each method. – Ryan McDonough Oct 08 '12 at 08:42
  • @RyanMcDonough Why ? with that code they return nothing. Anyway it can also use an inside flag and in each method return what ever he like if its out of the time... or from here he can rewrite the path and move to some other method that return nothing... – Aristos Oct 08 '12 at 08:44
  • I imagined when he said return void or null he meant that the service would still be accessible, methods could still be accessed but they'd return a null when called rather than 403? I see your way as the best way to do it. – Ryan McDonough Oct 08 '12 at 08:57