This is a migrated thread and some comments may be shown as answers.

SerializationException with QueryableDataProvider

6 Answers 28 Views
PivotGrid
This is a migrated thread and some comments may be shown as answers.
Daniel B.
Top achievements
Rank 1
Daniel B. asked on 31 Mar 2016, 06:46 AM

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;
            }
        }
    }

---

 

 

 

6 Answers, 1 is accepted

Sort by
0
Daniel B.
Top achievements
Rank 1
answered on 01 Apr 2016, 01:12 AM
Btw - im also pretty sure that I was the one to name it LocalDataSourceSerializer, it was before just something like MyDataSourceSerializer.
0
Daniel B.
Top achievements
Rank 1
answered on 01 Apr 2016, 01:16 AM

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)

0
Polya
Telerik team
answered on 04 Apr 2016, 10:13 AM
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.
0
Daniel B.
Top achievements
Rank 1
answered on 07 Apr 2016, 04:49 AM

@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

0
Polya
Telerik team
answered on 07 Apr 2016, 08:22 AM
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.
0
Daniel B.
Top achievements
Rank 1
answered on 16 May 2016, 12:46 AM

Thanks Polya

That works great :)

Tags
PivotGrid
Asked by
Daniel B.
Top achievements
Rank 1
Answers by
Daniel B.
Top achievements
Rank 1
Polya
Telerik team
Share this question
or