I'm trying to use .NET 4.5's new regular expression match timeout, specifically the global variant via AppDomain.CurrentDomain.SetData
with the "REGEX_DEFAULT_MATCH_TIMEOUT"
property (the variant whereby you pass a TimeSpan
to the regex constructor works fine).
When I create a new console application with this main method:
static void Main(string[] args)
{
AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT",
TimeSpan.FromSeconds(3));
var m = System.Text.RegularExpressions.Regex.Match(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "^(x+x+)+y$");
}
it works as expected: After three seconds, it throws a RegexMatchTimeoutException
.
However if I create an empty MVC 4 app, add a HomeController
and this action method:
public ActionResult Index()
{
AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT",
TimeSpan.FromSeconds(3));
var m = System.Text.RegularExpressions.Regex.Match(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "^(x+x+)+y$");
return View();
}
and visit http://localhost:XXXXX/
no exception is thrown and the match attempt continues. (If you wait long enough, it'll eventually finish, and then complain about the missing view. That takes veeery long though.)
Calling SetData
in Global.asax
's Application_Start()
instead of within the controller action doesn't make the timeout happen either.