SerializationException with QueryableDataProvider

7 posts, 0 answers
  1. Daniel B.
    Daniel B. avatar
    18 posts
    Member since:
    Apr 2015

    Posted 31 Mar Link to this post

    So I have a working save/load for my RadPivotGrid with LocalDataProvider, but had performance concerns so added set of wrappers and functionality for QueryableDataProvider.

    My wrappers and set with properties (get; set;) of types int, double, string, bool?, string, MyEnum, DateTime[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]

    Im not applying any custom aggregates or similar, and in this example I had 1 column, 1 row property and 'count' of ItemID value.

     

    The code works fine with LocalDataProvider (though not using wrappers there, instead more complex EF entities), but wit h my lighter QueryableDataProvider datasource it now my export throws an exception when it hits the serialize command `provider.Serialize(this.xPivotGrid_Reports.DataProvider)`

    ---

    An exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.dll but was not handled in user code

    Additional information: Type 'Telerik.Pivot.Queryable.QueryablePropertyAggregateDescription' with data contract name 'QueryablePropertyAggregateDescription:http://schemas.datacontract.org/2004/07/Telerik.Pivot.Queryable' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.

    ---

     

    I cant remember original source, but I found the DataContract code online and changed very little.

    And I am aware that the DataProviderSerializer subclass (where KnownTypes is defined) is called LocalDataSourceSerializer, but thats part of question: 

    Is there another list like `PivotSerializationHelper.KnownTypes` I should be using, because it seems comprehensive enough and im hardly doing anything strange?

     

    ---

        [DataContract]
        public class DataProviderSettings
        {
            [DataMember]
            public object[] Aggregates { get; set; }

            [DataMember]
            public object[] Filters { get; set; }

            [DataMember]
            public object[] Rows { get; set; }

            [DataMember]
            public object[] Columns { get; set; }

            [DataMember]
            public int AggregatesLevel { get; set; }

            [DataMember]
            public PivotAxis AggregatesPosition { get; set; }
        }

        public class LocalDataSourceSerializer : DataProviderSerializer
        {
            public override IEnumerable<Type> KnownTypes
            {
                get
                {
                    return PivotSerializationHelper.KnownTypes;
                }
            }
        }

        public abstract class DataProviderSerializer
        {
            public abstract IEnumerable<Type> KnownTypes { get; }

            public string Serialize(object context)
            {
                string serialized = string.Empty;

                IDataProvider dataProvider = context as IDataProvider;
                if (dataProvider != null)
                {
                    MemoryStream stream = new MemoryStream();

                    DataProviderSettings settings = new DataProviderSettings()
                    {
                        Aggregates = dataProvider.Settings.AggregateDescriptions.OfType<object>().ToArray(),
                        Filters = dataProvider.Settings.FilterDescriptions.OfType<object>().ToArray(),
                        Rows = dataProvider.Settings.RowGroupDescriptions.OfType<object>().ToArray(),
                        Columns = dataProvider.Settings.ColumnGroupDescriptions.OfType<object>().ToArray(),
                        AggregatesLevel = dataProvider.Settings.AggregatesLevel,
                        AggregatesPosition = dataProvider.Settings.AggregatesPosition
                    };

                    DataContractSerializer serializer = new DataContractSerializer(typeof(DataProviderSettings), KnownTypes);
                    serializer.WriteObject(stream, settings);

                    stream.Position = 0;
                    var streamReader = new StreamReader(stream);
                    serialized += streamReader.ReadToEnd();
                }

                return serialized;
            }

            public void Deserialize(object context, string savedValue)
            {
                IDataProvider dataProvider = context as IDataProvider;
                if (dataProvider != null)
                {
                    var stream = new MemoryStream();
                    var tw = new StreamWriter(stream);
                    tw.Write(savedValue);
                    tw.Flush();
                    stream.Position = 0;

                    DataContractSerializer serializer = new DataContractSerializer(typeof(DataProviderSettings), KnownTypes);
                    var result = serializer.ReadObject(stream);

                    dataProvider.Settings.AggregateDescriptions.Clear();
                    foreach (var aggregateDescription in (result as DataProviderSettings).Aggregates)
                    {
                        dataProvider.Settings.AggregateDescriptions.Add(aggregateDescription);
                    }

                    dataProvider.Settings.FilterDescriptions.Clear();
                    foreach (var filterDescription in (result as DataProviderSettings).Filters)
                    {
                        dataProvider.Settings.FilterDescriptions.Add(filterDescription);
                    }

                    dataProvider.Settings.RowGroupDescriptions.Clear();
                    foreach (var rowDescription in (result as DataProviderSettings).Rows)
                    {
                        dataProvider.Settings.RowGroupDescriptions.Add(rowDescription);
                    }

                    dataProvider.Settings.ColumnGroupDescriptions.Clear();
                    foreach (var columnDescription in (result as DataProviderSettings).Columns)
                    {
                        dataProvider.Settings.ColumnGroupDescriptions.Add(columnDescription);
                    }

                    dataProvider.Settings.AggregatesPosition = (result as DataProviderSettings).AggregatesPosition;
                    dataProvider.Settings.AggregatesLevel = (result as DataProviderSettings).AggregatesLevel;
                }
            }
        }

    ---

     

     

     

  2. Daniel B.
    Daniel B. avatar
    18 posts
    Member since:
    Apr 2015

    Posted 31 Mar in reply to Daniel B. Link to this post

    Btw - im also pretty sure that I was the one to name it LocalDataSourceSerializer, it was before just something like MyDataSourceSerializer.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Daniel B.
    Daniel B. avatar
    18 posts
    Member since:
    Apr 2015

    Posted 31 Mar in reply to Daniel B. Link to this post

    Scratch that last reply (why cant you edit or delete posts here???)

    Recalled it was named that and i got that code from WPF demo (Telerik UI For WPF Q1 2015 SP1)

  5. Polya
    Admin
    Polya avatar
    199 posts

    Posted 04 Apr Link to this post

    Hi Daniel,

    For each RadPivotGrid's DataProvider we've exposed a specific KnownTypes property that includes all types needed to serialize the used DataProvider.
    For QueryableDataProvider serialization you should use the QueryablePivotSerializationHelper.KnownTypes instead if PivotSerializationHelper.KnownTypes (as it includes the necessary types for LocalDataSourceProvider serialization).

    Hope this helps.

    Regards,
    Polya
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  6. Daniel B.
    Daniel B. avatar
    18 posts
    Member since:
    Apr 2015

    Posted 06 Apr in reply to Polya Link to this post

    @Polya - exactly what i was looking for, just couldnt find any referance to a comparable to KnownTypes anywhere

    Caught up in other functionality atm, but will post here and/or mark as answered once i get this working

    Thanks

  7. Polya
    Admin
    Polya avatar
    199 posts

    Posted 07 Apr Link to this post

    Hi Daniel,

    Looking forward to your reply whether the QueryablePivotSerializationHelper.KnownTypes did the trick.

    Regards,
    Polya
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  8. Daniel B.
    Daniel B. avatar
    18 posts
    Member since:
    Apr 2015

    Posted 15 May in reply to Polya Link to this post

    Thanks Polya

    That works great :)

Back to Top
UI for WPF is Visual Studio 2017 Ready