关闭.net服务中的所有功能

时间:2012-10-08 08:18:40

标签: asp.net web-services

我有ASP.NET Web服务项目。我必须"关掉" (如果当前时间在00:00和01:00之间,则返回void或null)此服务中的所有方法。最好的方法是什么?可能是global.asax文件?

1 个答案:

答案 0 :(得分:2)

你可以在 Global.asax 上, Application_BeginRequest 上执行此操作,只需检查是否是网络服务,然后如果不允许那些时间你就切断它。

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 ;    
    }    
  }
}