C# 序列化 System.Globalization.CultureInfo 类型的对象时检测到循环引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/673079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A circular reference was detected while serializing an object of type System.Globalization.CultureInfo
提问by user48408
I'm using jquery to call a webservice which returns a dataset with a couple of tables in it.
我正在使用 jquery 调用一个 web 服务,它返回一个包含几个表的数据集。
This was working ok until i needed to set up my webmethod to accept a parameter. I reflected this on the client side with
这一切正常,直到我需要设置我的 webmethod 以接受参数。我在客户端反映了这一点
data: "{paramname:'" + paramval+ "'}",
I now get the following error when the webmethod returns. This happens regardless of whats being returned in the dataset
当 webmethod 返回时,我现在收到以下错误。无论数据集中返回什么,都会发生这种情况
Error:{"Message":"A circular reference was detected while serializing an object of type \u0027System.Globalization.CultureInfo\u0027.","StackTrace":" at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at ...etc
错误:{"Message":"在序列化 \u0027System.Globalization.CultureInfo\u0027 类型的对象时检测到循环引用。","StackTrace":" 在 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o , StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at ...等
When the webmethod has no parameters the client side js looks the same as below except the data: line is removed.
当 webmethod 没有参数时,客户端 js 看起来与下面相同,除了 data: 行被删除。
function ClientWebService(paramval){
$.ajax({
type: "POST",
url: "WebService1.asmx/webmethodName",
data: "{paramname:'" + paramval+ "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
ParseResult(msg.d);
},
error: function(err) {
if (err.status == 200) {
ParseResult(err);
}
else { alert('Error:' + err.responseText + ' Status: ' + err.status); }
}
});
}
}
Edit: As per suggestion to change the request to
编辑:根据建议将请求更改为
data: {paramname: paramval},
yields the following error.
产生以下错误。
Error:{"Message":"Invalid JSON primitive: paramval.","StackTrace":"
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"} Status: 500
错误:{"Message":"无效的 JSON 原语:paramval。","StackTrace":"
在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize (String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script。 Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n 在 System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n 在 System.Web.Script.Services.RestHandler。 GetRawParams(WebServiceMethodData methodData,HttpContext 上下文)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"} 状态:500
采纳答案by user48408
I changed my webmethod to return
我改变了我的 webmethod 返回
ds.GetXml();
and this worked. Considering datasets are serializeable I'm not sure why I have to do this, but it gets me over this hurdle.
这有效。考虑到数据集是可序列化的,我不确定为什么我必须这样做,但它让我克服了这个障碍。
回答by BlueBird
I had the exact same problem,
我有同样的问题,
I removed Virtual keyword from my Entities which make lazy loading of object.
我从我的实体中删除了 Virtual 关键字,它使对象延迟加载。
The problem solved!!
问题解决了!!
回答by Deviland
I know this question has been answered but I came across the same problem and thought I should put what worked for me here so others can hopefully tackle the problem.
我知道这个问题已经得到了回答,但我遇到了同样的问题,并认为我应该把对我有用的东西放在这里,这样其他人就可以解决这个问题了。
data: JSON.stringify(Params),
By using the JSON.stringifymethod I got the required result
通过使用JSON.stringify方法,我得到了所需的结果
where paramsis equal to {"key":"value"}
其中params等于{"key":"value"}
回答by dynamiclynk
I ran into this response when there was an type conversion exception server side.
当存在类型转换异常服务器端时,我遇到了这个响应。
回答by Contra
this will often happen when serializing objects with entityrefs and entitysets. e.g if you have a set like this
当使用实体引用和实体集序列化对象时,经常会发生这种情况。例如,如果你有这样的一套
public EntitySet<ProductCategory> Subcategories
the serializer will go into the ProductCategory
and try to serialize all sets there aswell, often ending up back on the original object, and therefore creating a loop.
序列化器将进入ProductCategory
并尝试序列化那里的所有集合,通常最终回到原始对象,因此创建一个循环。
The best way to avoid this is to put [ScriptIgnore]
and [NonSerialized]
on fields and [ScriptIgnore]
on properties like this.
避免这种情况的最好方法是像这样放置 [ScriptIgnore]
和[NonSerialized]
放置字段和[ScriptIgnore]
属性。
[ScriptIgnore]
[NonSerialized]
private EntitySet < ProductCategory > _Subcategories;
[ScriptIgnore]
[Association(Storage = "_Subcategories", ThisKey = "ID", OtherKey = "ParentID")]
public EntitySet < ProductCategory > Subcategories {
get {
return this._Subcategories;
}
set {
this._Subcategories.Assign(value);
}
}
Same on Refs
参考文献相同