My colleagues and I recently encountered a very strange bug in an ASP.NET MVC web-site which seemed to be to do with NHibernate. The error suggested that we were trying to use a session that had already been closed. We could not find any sign of a closed session anywhere within the controllers in our project. Eventually we pinned the error down to a repository (containing an NHibernate session) referenced in an ActionFilterAttribute. The guy that wrote the code assumed that the repository would be referenced freshly by the attribute each time it was used, but of course it wasn’t; the .NET engine was helpfully caching the attribute between calls so that some users were getting sessions intended for other users.
The moral of the story is to make sure your ActionFilterAttributes are always stateless.
The following simple demonstration highlights this. Here is an ActionFilterAttribute which stores the date and time in a field on construction:
using System; using System.Web.Mvc; namespace WebTest.Attributes { public class TestActionFilterAttribute : ActionFilterAttribute { private readonly DateTime _now; public TestActionFilterAttribute() { _now = DateTime.Now; } public override void OnActionExecuting(ActionExecutingContext filterContext) { // Put the date into a ViewBag property: filterContext.Controller.ViewBag.DateTime = _now; } } }
This is then used in a controller as follows:
using System; using System.Text; using System.Web.Mvc; using WebTest.Attributes; namespace WebTest.Controllers { public class HomeController : Controller { [TestActionFilter] public ActionResult Index() { const string dateTimeFormat = "yyyy-MM-dd HH:mm:ss.fffffff"; var text = new StringBuilder(); text.AppendFormat("DateTime called directly: {0}<br/>", DateTime.Now.ToString(dateTimeFormat)); text.AppendFormat("DateTime taken from ActionFilterAttribute: {0}<br/>", ViewBag.DateTime.ToString(dateTimeFormat)); return Content(text.ToString()); } } }
If you refresh the page a few times you’ll notice that the time coming from the ViewBag (which originates from the attribute) remains the same while the current time continues to increase, proving that the attribute isn’t being instantiated for each web call. You have been warned!