Circular Reference with Complex Type

Last post 10-13-2007 7:48 PM by KaziManzurRashid. 4 replies.

Sort Posts:

  • Circular Reference with Complex Type

    04-04-2006, 12:33 PM
    • Member
      195 point Member
    • pcoulter
    • Member since 10-07-2003, 9:20 PM
    • Posts 40

    I have created a webservice that returns a complex type.  This is the xml that is returned from the webservice:

    <NinjaRouteEntity xmlns="http://tempuri.org/">
      <Entity NinjaRouteID="1" xmlns="">
        <AlwaysFetchToRegion>False</AlwaysFetchToRegion>
        <TimeFrameReturnsNewIfNotFound>True</TimeFrameReturnsNewIfNotFound>
        <AlwaysFetchFromRegion>False</AlwaysFetchFromRegion>
        <EntityCollectionReference PropertyName="PointCollectionViaNinjaRouteVector">
          <PointCollectionViaNinjaRouteVector>
            <Entities />
            <AllowRemove>False</AllowRemove>
            <AllowEdit>True</AllowEdit>
            <AllowNew>True</AllowNew>
          </PointCollectionViaNinjaRouteVector>
        </EntityCollectionReference>
        <ToRegionReturnsNewIfNotFound>True</ToRegionReturnsNewIfNotFound>
        <EntityReference PropertyName="TimeFrame" />
        <EntityReference PropertyName="FromRegion" />
        <IsNew>False</IsNew>
        <FromRegionReturnsNewIfNotFound>True</FromRegionReturnsNewIfNotFound>
        <AlwaysFetchNinjaRouteVector>False</AlwaysFetchNinjaRouteVector>
        <AlwaysFetchPointCollectionViaNinjaRouteVector>False</AlwaysFetchPointCollectionViaNinjaRouteVector>
        <EntityReference PropertyName="ToRegion" />
        <AlwaysFetchTimeFrame>False</AlwaysFetchTimeFrame>
        <AlwaysFetchSuggestedRouteCollectionViaNinjaSuggestedRoute>False</AlwaysFetchSuggestedRouteCollectionViaNinjaSuggestedRoute>
        <EntityCollectionReference PropertyName="SuggestedRouteCollectionViaNinjaSuggestedRoute">
          <SuggestedRouteCollectionViaNinjaSuggestedRoute>
            <Entities />
            <AllowRemove>False</AllowRemove>
            <AllowEdit>True</AllowEdit>
            <AllowNew>True</AllowNew>
          </SuggestedRouteCollectionViaNinjaSuggestedRoute>
        </EntityCollectionReference>
        <EntityCollectionReference PropertyName="NinjaSuggestedRoute">
          <NinjaSuggestedRoute>
            <Entities />
            <AllowRemove>False</AllowRemove>
            <AllowEdit>True</AllowEdit>
            <AllowNew>True</AllowNew>
          </NinjaSuggestedRoute>
        </EntityCollectionReference>
        <ObjectID>7ba4f8ed-08d5-47cb-9dcc-096725d03d69</ObjectID>
        <EntityCollectionReference PropertyName="NinjaRouteVector">
          <NinjaRouteVector>
            <Entities />
            <AllowRemove>False</AllowRemove>
            <AllowEdit>True</AllowEdit>
            <AllowNew>True</AllowNew>
          </NinjaRouteVector>
        </EntityCollectionReference>
        <AlwaysFetchNinjaSuggestedRoute>False</AlwaysFetchNinjaSuggestedRoute>
        <Fields>
          <NinjaRouteID>
            <CurrentValue>1</CurrentValue>
            <DbValue>1</DbValue>
            <IsChanged>False</IsChanged>
            <IsNull>False</IsNull>
          </NinjaRouteID>
          <Name>
            <CurrentValue>My Awesomer Route</CurrentValue>
            <DbValue>My Awesomer Route</DbValue>
            <IsChanged>False</IsChanged>
            <IsNull>False</IsNull>
          </Name>
          <Description>
            <CurrentValue>Go from here to there.</CurrentValue>
            <DbValue>Go from here to there.</DbValue>
            <IsChanged>False</IsChanged>
            <IsNull>False</IsNull>
          </Description>
          <CreatorID>
            <CurrentValue>4b4a743b-345f-4dd6-905a-bb59708491ed</CurrentValue>
            <DbValue>4b4a743b-345f-4dd6-905a-bb59708491ed</DbValue>
            <IsChanged>False</IsChanged>
            <IsNull>False</IsNull>
          </CreatorID>
          <DateCreated>
            <CurrentValue>2006-01-31T01:45:34.0000000-08:00</CurrentValue>
            <DbValue>2006-01-31T01:45:34.0000000-08:00</DbValue>
            <IsChanged>False</IsChanged>
            <IsNull>False</IsNull>
          </DateCreated>
          <FromRegionID>
            <CurrentValue>1</CurrentValue>
            <DbValue>1</DbValue>
            <IsChanged>False</IsChanged>
            <IsNull>False</IsNull>
          </FromRegionID>
          <ToRegionID>
            <CurrentValue>2</CurrentValue>
            <DbValue>2</DbValue>
            <IsChanged>False</IsChanged>
            <IsNull>False</IsNull>
          </ToRegionID>
          <TimeFrameID>
            <CurrentValue>2</CurrentValue>
            <DbValue>2</DbValue>
            <IsChanged>False</IsChanged>
            <IsNull>False</IsNull>
          </TimeFrameID>
        </Fields>
        <IsDirty>False</IsDirty>
        <EntityState>Fetched</EntityState>
        <SavedFieldSets />
      </Entity>
    </NinjaRouteEntity

    When I try to call it with Atlas, I see this error via Fiddler:

    new Sys.Net.MethodRequestError("A circular reference was detected while serializing an object of type \'FlatlandMedia.NinjaRoutes.Dal.EntityClasses.NinjaRouteEntity\'.","   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeCustomObject(Object o)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeEnumerable(IEnumerable enumerable)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeCustomObject(Object o)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)\r\n   at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)\r\n   at Microsoft.Web.Services.RestHandler.ProcessRequest(HttpContext context)","System.InvalidOperationException")

    Any way around this?  Are there any guidelines as to how complex a type can be?

    Thanks for any help on this.

  • Re: Circular Reference with Complex Type

    04-04-2006, 3:57 PM
    • Contributor
      5,655 point Contributor
    • davidebb
    • Member since 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 1,123
    There is no limit to the complexity of what can be returned, but the object graph cannot have a circular reference.  However, I would expect the same limitation when calling it as a standard web service, which as I understand from you post works correctly.  So there may be something else going on.  Maybe you have some special serialization attributes that Atlas is not honoring correctly?
  • Re: Circular Reference with Complex Type

    04-07-2006, 1:20 PM
    • Member
      5 point Member
    • NJW
    • Member since 04-07-2006, 5:01 PM
    • Posts 1

    I am encountering the same error as is described above.

    I understand that there will be issues when the object graph has a circular reference in it, however I am encountering this issue when I simply attempt to return a System.Data.DataTable comprised of columns with simple data types.

    What I'm currently attempting to do is call the 'getTableFromService' JavaScript function and get a DataTable back from the Web Service. I've included both the Service and the JavaScript.

    My error handling function output is also included at the bottom.

    Any help you can provide would be greatly appreciated.

     

    Thanks,

     

    neil

     

     

    Imports System
    Imports System.Data
    Imports System.Web.Services
    
    Namespace Services
    
        <WebService(Namespace:="http://tempuri.org/")> _
        <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
        Public Class SampleDataTable
            Inherits System.Web.Services.WebService
    
            <WebMethod()> Public Function GetTable() As System.Data.DataTable
                Dim dtResults As New System.Data.DataTable
                Dim drRow As System.Data.DataRow = Nothing
                dtResults.Columns.Add(New System.Data.DataColumn("ID", GetType(Integer)))
                dtResults.Columns.Add(New System.Data.DataColumn("Digit", GetType(Integer)))
    
                drRow = dtResults.NewRow()
                drRow.Item("ID") = 1
                drRow.Item("Digit") = 1
    
                dtResults.Rows.Add(drRow)
    
                Return dtResults
            End Function
    
        End Class
    
    End Namespace
     

    function

    getTableFromService()

    {

    Services.SampleDataTable.GetTable(onSuccess, onTimeout, onError);

    }

    function

    onSuccess( Results )

    {

    //success

    }

    function

    onTimeout( Results )

    {

    //timeout

    }

    function

    onError( Results )

    {

    alert(

    'Error Type:\n' + Results.get_exceptionType() + '\n\nError:\n' + Results.get_message() + '\n\nStack Trace:\n' + Results.get_stackTrace());

    }

     
    ---------------------------
    Microsoft Internet Explorer
    ---------------------------
    Error Type:
    System.InvalidOperationException
    
    Error:
    A circular reference was detected while serializing an object of type 'System.Reflection.Module'.
    
    Stack Trace:
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeCustomObject(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeCustomObject(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeCustomObject(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeCustomObject(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeEnumerable(IEnumerable enumerable)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeCustomObject(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValueInternal(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.SerializeValue(Object o)
    
       at Microsoft.Web.Script.Serialization.JavaScriptObjectSerializer.Serialize(Object o, IJavaScriptSerializationContext context)
    
       at Microsoft.Web.Services.RestHandler.ProcessRequest(HttpContext context)
    ---------------------------
    OK   
    ---------------------------
    
     
  • Re: Circular Reference with Complex Type

    10-12-2007, 5:44 AM
    • Member
      2 point Member
    • dear.saxena
    • Member since 10-12-2007, 9:26 AM
    • Posts 1

    Hi Neil,

    I am facing the same problem if you have any solution please let me know.

     

    Thanks                                                                                                                                                                                                              Saurabh Saxena

  • Re: Circular Reference with Complex Type

    10-13-2007, 7:48 PM
    • Contributor
      4,792 point Contributor
    • KaziManzurRashid
    • Member since 03-09-2003, 11:04 AM
    • Dhaka, Bangladesh
    • Posts 882

    To deal with Circular reference you have to write your own custom JavaScriptConverter, checkout the Serialize incompatible types of this article:
    http://dotnetslackers.com/columns/ajax/ASPNETAjaxWebService.aspx

    Long Live .NET
    Kazi Manzur Rashid (Amit)
    _________________________
    Web: http //dotnetshoutout.com
    Blog: http://weblogs.asp.net/rashid
    Twitter: http://twitter.com/manzurrashid
Page 1 of 1 (5 items)