A circular reference was detected while serializing an object of type System.Globalization.CultureInfo

29,766

Solution 1

I changed my webmethod to return

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.

Solution 2

I had the exact same problem,

I removed Virtual keyword from my Entities which make lazy loading of object.

The problem solved!!

Solution 3

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.

The best way to avoid this is to put [ScriptIgnore] and [NonSerialized] on fields and [ScriptIgnore] on properties like this.

[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

Share:
29,766
user48408
Author by

user48408

Updated on December 21, 2020

Comments

  • user48408
    user48408 over 3 years

    I'm using jquery to call a webservice which returns a dataset with a couple of tables in it.

    This was working ok until i needed to set up my webmethod to accept a parameter. I reflected this on the client side with

    data: "{paramname:'" + paramval+ "'}",
    

    I now get the following error when the webmethod returns. This happens regardless of whats being returned in the dataset

    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

    When the webmethod has no parameters the client side js looks the same as below except the data: line is removed.

    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

  • Pomster
    Pomster over 10 years
    Please could you explain more on how you changed you webmethod?