如果已经提出这个问题,我道歉。我对asp.net的东西相当新,我确实在搜索,但我无法找到解决方案,我可以应用于我的情况 - 或者能够以我知道的方式申请。
无论如何,这就是问题所在。
我正在为学校建立一个项目。我们已经提供了很多设置和代码。我们从以下格式构建asp.net Web表单项目开始。
模型 的ViewModels 网站
Models是一个带有ADO.Net数据对象的类库。查看模型由与Model类交谈的类组成,而Website与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中不断收到序列化错误。对于这个解决方案,我们实际上有三个不同的部分 - 有电话,员工和问题。问题部分工作正常。它只有几个数据库项。员工和电话都在上述解决方案中工作,但在我加入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"}
答案 0 :(得分:0)
我想出来了!好吧,我的教授做了。
我不知道我是如何错过这一点的,因为我比较了所有文件和我引用的所有内容。
问题在于我在我的ViewModels类中继承了System.Web.UI.Page并且无法正常工作。
例如,我的EmployeeViewModel的ViewModel类是....
公共类EmployeeViewModel:System.Web.UI.Page
我删除了:System.Web.UI.Page并且它有效。
我希望在某个时间点帮助某个人。