序列化 \u0027ASP.global_asax 类型的对象时检测到循环引用
Posted
技术标签:
【中文标题】序列化 \\u0027ASP.global_asax 类型的对象时检测到循环引用【英文标题】:A circular reference was detected while serializing an object of type \u0027ASP.global_asax序列化 \u0027ASP.global_asax 类型的对象时检测到循环引用 【发布时间】:2015-01-21 06:49:29 【问题描述】:如果已经问过这个问题,我深表歉意。我对 asp.net 的东西还很陌生,我确实搜索过,但我找不到我能够适用于我的情况的解决方案 - 或者能够以我知道的方式应用。
无论如何,这就是问题所在。
我正在为学校构建一个项目。很多设置和代码已经提供给我们。我们从构建以下格式的 asp.net 网络表单项目开始。
型号 视图模型 网站
Models 是一个带有 ADO.Net 数据对象的类库。视图模型由与模型类通信的类组成,而网站与视图模型通信。相当直截了当。
在模型中,我们正在序列化所有内容 - 包括实体。这是序列化的代码
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 服务 视图模型 网站
在调用 ajax 之前一切正常。我知道 GetByID 方法有效,因为我从网站项目中的单独驱动程序调用它并打印出结果。调用 ajax 函数时,我不断收到 javascript 中的序列化错误。对于这个解决方案,我们实际上有三个不同的部分——电话、员工和问题。问题部分工作正常。它只有几个数据库项目。雇员和电话都在上述解决方案中工作,但当我包含 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;
这是 ViewModel 方法
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"
【问题讨论】:
【参考方案1】:我想通了!嗯,我的教授做到了。
我不知道我是怎么错过的,因为我比较了所有文件和我引用的所有内容。
问题在于我在我的 ViewModels 类中继承了 System.Web.UI.Page 但不起作用。
例如,EmployeeViewModel 的 ViewModel 类是....
公共类 EmployeeViewModel : System.Web.UI.Page
我删除了 : System.Web.UI.Page 并且它起作用了。
我希望在某个时间点对某人有所帮助。
【讨论】:
以上是关于序列化 \u0027ASP.global_asax 类型的对象时检测到循环引用的主要内容,如果未能解决你的问题,请参考以下文章
“命名空间 ASP 中不存在 ASP.global_asax”
jQuery/ASMX:在 C# 中反序列化 JSON 时出错