Patrick
Patrick

Reputation: 5592

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

The webserver on my hosting company keeps complaining that the class is not marked as [Serializable].

When i run it on my localhost it works fine, no problem. As soon as i upload it to the server it asks me to serialize it?


Example class:

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

That i use in a base controller. This class is stored in TempData when a redirect to another method occurs, can 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 one just adds the notifications and a model if needed and then to a redirect to another action.

Upvotes: 4

Views: 1234

Answers (2)

Mike Goodwin
Mike Goodwin

Reputation: 8880

I guess its to do with your session state provider. For local this is probably in-memory, but on your host it would be using some out of process mechanism for multi server support.

Upvotes: 2

welegan
welegan

Reputation: 3043

In your web.config, there is a section like

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

this specifies that your app will take the Session property of Controller to be a data structure within the same process as your application. On the server, it is very likely that the section looks like

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

This tag specifies that you are using ASP.Net StateServer, a separate process from your application that is responsible for storing Session data (which is probably what is in your TempData call). Since it is another process, the way .NET gets data into there and out is by serializing what you are trying to store and deserializing what you are trying to retrieve. Hence, objects stored in Session in your production setup will need to be marked [Serializable].

Upvotes: 3

Related Questions