2013-07-04 17 views
4

我的託管公司的網絡服務器不停地抱怨該類沒有標記爲[Serializable]。MVC 4爲什麼我必須在服務器上序列化,但不是在本地?

當我在本地主機上運行它時,它工作正常,沒有問題。只要我上傳到服務器,它會要求我序列化它?


實施例類:

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

我在鹼控制器使用。當重定向到另一個方法時,這個類存儲在TempData中,這是原因嗎?但是,爲什麼在服務器上而不是在本地計算機上呢?

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

控制器壓倒一切的這一個,如果需要,然後重定向到另一個動作只是增加了通知和模型。

回答

3

在你的web.config,有像

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

的截面這個指定應用程式會採取控制器的會話屬性是相同的過程,在應用程序中的數據結構。在服務器上,它很可能是,部分看起來像

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

此標記指定您正在使用ASP.Net的StateServer,從您的應用程序一個單獨的進程,它負責存儲會話數據(這是可能你的TempData調用中有什麼)。由於它是另一個進程,.NET獲取數據的方式是通過序列化您試圖存儲的內容並反序列化您嘗試檢索的內容。因此,在生產設置中存儲在Session中的對象需要標記爲[Serializable]。

+0

有沒有一種方法讓本地主機模仿在服務器中的行爲?所以,如果我不設置一個類可序列化它會拋出相同的錯誤? – NicoTek

2

我猜它與你的會話狀態提供者有關。對於本地來說,這可能是內存中的,但是在你的主機上,它會使用一些進程外機制來支持多服務器。

相關問題