2014-11-23 55 views
0

如果已經提出了此問題,我表示歉意。我對asp.net的東西相當陌生,而且我搜索了一些東西,但是我找不到解決方案,能夠適用於我的情況 - 或者能夠以我知道的方式應用。在序列化 u0027ASP.global_asax類型的對象時檢測到循環引用

無論如何,這是問題所在。

我正在爲學校建設一個項目。許多設置和代碼已經提供給我們。我們以以下格式構建asp.net web表單項目開始。

模型 的ViewModels 網站

模型是與ADO.Net數據對象的類庫。查看模型由與模型類交談的類組成,網站與ViewModels交談。非常坦率的。

在模型中,我們正在序列化所有內容 - 包括一個實體。下面是序列化

public static byte[] Serializer(Object inObject, bool bIsEntity = false) 
    { 
     byte[] ByteArrayObject; 

     if (bIsEntity == true) 
     { 
      MemoryStream strm = new MemoryStream(); 
      var serializer = new DataContractSerializer(inObject.GetType()); 
      serializer.WriteObject(strm, inObject); 
      ByteArrayObject = strm.ToArray(); 
     } 
     else 
     { 
      BinaryFormatter binFormatter = new BinaryFormatter(); 
      MemoryStream memStream = new MemoryStream(); 
      binFormatter.Serialize(memStream, inObject); 
      ByteArrayObject = memStream.ToArray(); 
     } 
     return ByteArrayObject; 
    } 

的代碼,代碼基本上只是連載一本字典,與實體 - 實體被單獨序列化,那麼整個事情。

我們基本上只是調用一些get方法並獲取數據對象。這一切都工作。

我們還使用一些jquery ajax方法調用來檢索模態的信息。所以,在網站的aspx.cs文件中,我們有這個..

[System.Web.Services.WebMethod] 
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)] 
    public static EmployeeViewModel GetEmployee(int empID) 
    { 
     EmployeeViewModel emp = new EmployeeViewModel(); 
     emp.GetByID(empID); 
     return emp; 
    } 

在那種格式下工作正常。有一個Ajax調用該函數,一切都完美。有問題的員工出現。

之後,我們基本上採用了這些代碼並創建了一個新的解決方案。在該解決方案中,我們現在有三個其他項目(4,但我們現在只使用3個 - 我們即將推出一個服務器項目)。它們分別是:

WCF服務 的ViewModels 網站

一切工作正常,直到Ajax調用。我知道GetByID方法有效,因爲我從網站項目中的單獨驅動程序調用它並打印出結果。在調用ajax函數時,我不斷收到javascript中的序列化錯誤。我們實際上有三種不同的解決方案 - 有電話,員工和問題。問題部分工作正常。它只有一些數據庫項目。 Employees和Calls都在上面提到的解決方案中工作,但是當我包含WCF時它們不起作用。

我得到的錯誤如下。首先我要給你一些其他可能很重要的方法。

這是在ModelServices(WCF服務)項目

public byte[] GetByID(int empID) 
    { 
     Dictionary<string, Object> retDict = new Dictionary<string, Object>(); 
     try 
     { 
      HelpdeskDBEntities dbContext = new HelpdeskDBEntities(); 
      dbContext.Configuration.ProxyCreationEnabled = false; 
      Employee EmployeeEntity = dbContext.Employees.FirstOrDefault(emp => emp.EmployeeID == empID); 
      if (EmployeeEntity != null) 
      { 
       retDict["firstname"] = EmployeeEntity.FirstName; 
       retDict["lastname"] = EmployeeEntity.LastName; 
       retDict["phoneno"] = EmployeeEntity.PhoneNo; 
       retDict["email"] = EmployeeEntity.Email; 
       retDict["departmentid"] = EmployeeEntity.DepartmentID; 
       retDict["employeeid"] = EmployeeEntity.EmployeeID; 
       retDict["title"] = EmployeeEntity.Title; 
       retDict["staffpicture"] = EmployeeEntity.StaffPicture; 

       retDict["entity"] = HelpdeskModelUtils.Serializer(EmployeeEntity, true); 
      } 
      else 
      { 
       retDict["error"] = "Employee Not Found"; 
      } 
     } 
     catch (Exception ex) 
     { 
      HelpdeskModelUtils.ErrorRoutine(ex, this.GetType().Name, HelpdeskModelUtils.GetCurrentMethod()); 
      retDict["error"] = ex.Message; 
     } 
     return HelpdeskModelUtils.Serializer(retDict); 
    } 

這是在WCF服務項目

public static byte[] Serializer(Object inObject, bool bIsEntity = false) 
    { 
     byte[] ByteArrayObject; 

     if (bIsEntity == true) 
     { 
      MemoryStream strm = new MemoryStream(); 
      var serializer = new DataContractSerializer(inObject.GetType()); 
      serializer.WriteObject(strm, inObject); 
      ByteArrayObject = strm.ToArray(); 
     } 
     else 
     { 
      BinaryFormatter binFormatter = new BinaryFormatter(); 
      MemoryStream memStream = new MemoryStream(); 
      binFormatter.Serialize(memStream, inObject); 
      ByteArrayObject = memStream.ToArray(); 
     } 
     return ByteArrayObject; 
    } 

    public static Object Deserializer(byte[] ByteArrayIn) 
    { 
     BinaryFormatter binFormater = new BinaryFormatter(); 
     MemoryStream memStream = new MemoryStream(ByteArrayIn); 
     Object returnObject = binFormater.Deserialize(memStream); 
     return returnObject; 
    } 

這是視圖模型方法的串行器和解串方法

public void GetByID(int empId) 
    { 
     try 
     { 
      EmployeeServiceReference.EmployeeModelServiceClient model = new EmployeeServiceReference.EmployeeModelServiceClient(); 
      Dictionary<string, Object> dictionaryEmployee = 
       (Dictionary<string, Object>)HelpdeskViewModelUtils.Deserializer(model.GetByID(empId)); 

      if (!dictionaryEmployee.ContainsKey("error")) 
      { 
       _firstName = (string)dictionaryEmployee["firstname"]; 
       _lastName = (string)dictionaryEmployee["lastname"]; 
       _title = (string)dictionaryEmployee["title"]; 
       _departmentID = (int)dictionaryEmployee["departmentid"]; 
       _phoneno = (string)dictionaryEmployee["phoneno"]; 
       _email = (string)dictionaryEmployee["email"]; 
       _entity = (byte[])dictionaryEmployee["entity"]; 
       _employeeID = (int)dictionaryEmployee["employeeid"]; 
      } 
      else 
      { 
       _lastName = "error"; 
      } 
     } 
     catch (Exception ex) 
     { 
      HelpdeskViewModelUtils.ErrorRoutine(ex, this.GetType().Name, HelpdeskViewModelUtils.GetCurrentMethod()); 
      throw new Exception(ex.Message); 
     } 
    } 

這是在的ViewModels串行器和解串器項目

public static byte[] Serializer(Object inObject) 
    { 
     BinaryFormatter binFormatter = new BinaryFormatter(); 
     MemoryStream memStream = new MemoryStream(); 
     binFormatter.Serialize(memStream, inObject); 
     byte[] ByteArrayObject = memStream.ToArray(); 
     return ByteArrayObject; 
    } 

    public static Object Deserializer(byte[] ByteArrayIn) 
    { 
     BinaryFormatter binFormater = new BinaryFormatter(); 
     MemoryStream memStream = new MemoryStream(ByteArrayIn); 
     Object returnObject = binFormater.Deserialize(memStream); 
     return returnObject; 
    } 

這裏是我的ajax調用和相關的JavaScript的東西

function getEmployeeInfoForModal(empID) { 
    $.ajax({ 
     type: "POST", 
     url: "Employees.aspx/GetEmployee", 
     data: "{empID :" + empID + "}", 
     contentType: "application/json; charset-utf-8", 
     success: function (data) { 
      copyEmployeeInfoToModal(data.d); 
     }, 
     error: function (data) { 
      alert('problem getting server data' + data.responseText); 
     } 
    }); 
} 

function copyEmployeeInfoToModal(emp) { 
    .........do stuff - never gets here 
} 

錯誤我不斷收到以下錯誤。這是一個很長的錯誤,所以我要保持在一條線上。

problem getting server data{"Message":"A circular reference was detected while serializing an object of type \u0027ASP.global_asax\u0027.","StackTrace":" at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int3...\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"} 

回答

0

我想通了!那麼,我的教授呢。

我不知道我是如何錯過這一點的,因爲我比較了所有文件和我參考的所有內容。

這個問題是,我在我的ViewModels類繼承System.Web.UI.Page,這是不工作的事實。

例如,我對EmployeeViewModel ViewModel類是....

公共類EmployeeViewModel:System.Web.UI.Page

我刪除了:System.Web.UI.Page和它工作。

我希望能在某個時間點幫助別人。

相關問題