Incorrectly saved instance state in Action Filter

I just updated my project from ASP.NET MVC1.0 to ASP.NET MVC4.0

One thing that scares me is the next sentence in the MVC3.0 upgrade documentation

In previous versions of ASP.NET MVC, action filters are created per request, except in a few cases. Such behavior was never guaranteed, but only the implementation detail and filter contract should have been considered stateless. In ASP.NET MVC 3, filters are cached more aggressively. Therefore, any custom action filters that improperly save the state of an instance may be violated.

I think there is no easy way to check for errors caused by improperly maintaining the state of an instance. If I have a problem, I am sure that I will only find it in production.

What does the improperly saved instance state mean here?

I have this code:

public override void OnActionExecuting(ActionExecutingContext filterContext) { ProductModel productModel = new ProductModel() filterContext.ActionParameters["model"] = productModel; } 

Is this a sample of an improperly saved instance state?

+4
source share
1 answer

No, your code snippet is fine. An incorrectly saved state will be some class level properties, for example:

 public class StatefulActionFilter : ActionFilter { private readonly IPrincipal currentPrincipal = Thread.CurrentPrincipal; public override void OnActionExecuting(ActionExecutingContext filterContext) { ... // Using currentPrincipal here would be bad, since it may refer to the principal of a previous request. ... } } 
+5
source

Source: https://habr.com/ru/post/1389242/


All Articles