2014-01-14 60 views
0

我有一個MVC5前端和一個WebAPI版本2後端。當我去我的前端編輯記錄,我讓我的表單字段更改,然後單擊「保存」,我立刻得到錯誤:編輯(PUT)MVC5/WebAPI2中的記錄會導致錯誤「無法序列化會話狀態」。

enter image description here

此錯誤ONLY當我編輯記錄發生。它創建一個全新的記錄時工作正常。

這裏是我的堆棧跟蹤:

[SerializationException: Type 'AppTools.HelperClasses.LoggerHelper' in Assembly 'AppTools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.] 
    System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +10494323 
    System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +230 
    System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +121 
    System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +178 
    System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +51 
    System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +540 
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +131 
    System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1666 

[HttpException (0x80004005): Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.] 
    System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1754 
    System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object value, BinaryWriter writer) +34 
    System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter writer) +628 
    System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item, Stream stream) +240 
    System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item, Int32 initialStreamSize, Byte[]& buf, Int32& length, Boolean compressionEnabled) +62 
    System.Web.SessionState.OutOfProcSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +111 
    System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +798 
    System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69 

這裏是我的WebAPI PUT方法:

 [HttpPut] 
     public HttpResponseMessage Put(int id, BulletinBoard bulletinBoard) 
     { 
      if (!ModelState.IsValid) 
      { 
       return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); 
      } 

      db.Entry(bulletinBoard).State = EntityState.Modified; 

      try 
      { 
       db.SaveChanges(); 
      } 
      catch (DbUpdateConcurrencyException ex) 
      { 
       return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); 
      } 

      return Request.CreateResponse(HttpStatusCode.OK); 
     } 

這裏是我的前端(MVC5)編輯操作方法:

 [HttpPost] 
     public ActionResult Edit(int id, BulletinBoard bulletinBoard) 
     { 
      bulletinBoard.ModifiedDate = DateTime.Now; 
      bulletinBoard.ModifiedBy = HttpContext.User.Identity.Name;   

      response = client.PutAsJsonAsync(string.Format("api/bulletinboard/put/{0}", id), bulletinBoard).Result; 
      //var test = response.Content.ReadAsStringAsync(); 

      //HttpContext.Response.Write(test); 

      if (response.IsSuccessStatusCode) 
      { 
       return RedirectToAction("Index"); 
      } 
      else 
      { 
       LoggerHelper.GetLogger().InsertError(new Exception(string.Format(
        "Cannot create a new message due to HTTP Response Status Code not being successful: {0}", response.StatusCode))); 
       return View("Problem"); 
      } 
     } 

請幫助我明白爲什麼我得到這個錯誤!

+0

哪裏有id?我的意思是來自Url或其發佈請求的一部分 –

+0

@VolodymyrBilyachat該ID來自URL的一部分。 –

+0

btw你有[SerializationException:類型'AppTools.HelperClasses.LoggerHelper'在程序集'AppTools,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'未標記爲可序列化。]其中使用LoggerHelper? –

回答

0

可能的問題是,你的模型試圖自從你有簽名不進行身份更新

public HttpResponseMessage Put(int id, BulletinBoard bulletinBoard) 

因此多數民衆贊成意味着bulletinBoard可能沒有設置好的ID,那麼你必須

if (!ModelState.IsValid) 
{ 
    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); 
} 

然後可能你的模型是無效的,並且響應應該返回狀態狀態碼:400錯誤請求,但是由於你有一些不可序列化的日誌異常,所以它會拋出異常

解決它試試

public HttpResponseMessage Put(int id, BulletinBoard bulletinBoard) { 
bulletinBoard.Id = id; 
+0

不,沒有做到。我會嘗試再次使用Fiddler。 –

相關問題