MVC 4 Why do I have to serialize on the server, but not locally?

The web server of my hosting company complains that the class is not marked as [Serializable].

When I run it on my local host, it works fine, no problem. As soon as I upload it to the server, it asks it to serialize it?


Class Example:

public class Notification { public string Message { get; set; } public NotificationType NotificationType { get; set; } public static Notification CreateSuccessNotification(string message) { return new Notification { Message = message, NotificationType = NotificationType.Success}; } public static Notification CreateErrorNotification(string message) { return new Notification { Message = message, NotificationType = NotificationType.Error }; } } 

This is what I use in the base controller. This class is stored in TempData when redirecting to another method, could this be the reason? But still, why on the server, and not on my local computer?

 public abstract class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { _notifications = TempData["Notifications"] == null ? new List<Notification>() : (List<Notification>)TempData["Notifications"]; _model = TempData["Model"]; base.OnActionExecuting(filterContext); } protected override void OnResultExecuting(ResultExecutingContext filterContext) { if (filterContext.Result is RedirectToRouteResult) { if (_model != null) TempData["Model"] = _model; if (_notifications.Count > 0) TempData["Notifications"] = _notifications; } base.OnResultExecuting(filterContext); } } 

The controller overriding this simply adds notifications and a model if necessary, and then redirects to another action.

+4
source share
2 answers

Your web.config has a section like

<sessionState mode="InProc" cookieless="false" timeout="60" />

this indicates that your application will use the Session Controller property as a data structure in the same process as your application. On the server, it is very likely that the partition looks like

<sessionState mode="StateServer" stateConnectionString="tcpip=someIPAddress" cookieless="false" timeout="60" />

This tag indicates that you are using ASP.Net StateServer, a separate process from your application that is responsible for storing session data (which probably refers to a TempData call). Since this is another process, the way that .NET gets data there and outside is to serialize what you are trying to save and deserialize what you are trying to extract. Therefore, objects stored in a session in your production installation must be marked [Serializable].

+3
source

I assume this is to be done with your session state provider. For local, this is probably in memory, but on your host it will use some of the process mechanism to support multiple servers.

+2
source

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


All Articles