Serialization Exception in Release Mode

Hi,

I'm trying to make use of the SfDataGrid component in my app and have everything working just fine in debug mode. When I switched over to release mode to regression test the app before publishing to the store the app throws an exception during grid serialization.

I have an SfDataGrid defined with 4 text columns, 1 numeric column and 1 template column. The template column just includes a delete button so that the user to can remove the row.

I have a method to return the serialization options as follows:

         private SerializationOptions GetGridSerializationOptions()
        {
            return new SerializationOptions()
            {
                SerializeFiltering = false,
                SerializeColumns = true,
                SerializeGrouping = true,
                SerializeSorting = true,
                SerializeTableSummaries = true,
                SerializeCaptionSummary = true,
                SerializeGroupSummaries = true,
                SerializeStackedHeaders = true
            };
        }

Then I have another method to serialize the grid settings as follows:
         private void RetrieveDefaultGridSettings()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                gridReport.Serialize(ms, GetGridSerializationOptions());
                _defaultGridSettings = Convert.ToBase64String(ms.ToArray());
            }
        }


I've followed the documentation (https://help.syncfusion.com/uwp/datagrid/serialization-and-deserialization) which describes how to serialize template columns. I have everything working perfectly in debug mode, but when I switch to release mode I get an exception on this line:

gridReport.Serialize(ms, GetGridSerializationOptions());

The exception is:
System.Runtime.Serialization.InvalidDataContractException: 'KnownTypeAttribute attribute on type 'Syncfusion.UI.Xaml.Grid.SerializableGridColumn' specifies a method named 'KnownTypes' to provide known types. Static method 'KnownTypes()' was not found on this type. Ensure that the method exists and is marked as static.'

I've had a look at the SerializableGridColumn class and can see a public static method called KnownTypes so I don't really understand why this exception is happening. I'm even more confused about why it's only happening in release mode.

Thanks very much for any assistance anyone can provide.

8 Replies

MA Mathew May 13, 2020 04:05 PM UTC

After some further digging I discovered that switching OFF "Compile with .NET Native tool chain" resolves the problem. I wasn't debugging with the setting switched on, which does go some way to explaining the difference.

Is anyone able to explain why the "Compile with .NET Native tool chain" setting causes the exception?

I should point out I need to compile in release mode using .NET native tool chain as this is required for submitting my app to the Windows store.


VS Vijayarasan Sivanandham Syncfusion Team May 14, 2020 01:30 PM UTC

Hi Mathew,    
    
Thank you for contacting Syncfusion support.    
    
We have checked your query. While running the application with .Net native chain tool in Release mode, it will require more memory space to build the application. As .NET Native tools are compiled in IL libraries with managed code in native libraries and also "Syncfusion Controls for UWP XAML" SDK contains more number of assemblies we suggest to use corresponding assemblies in your application instead of "Syncfusion Controls for UWP XAML" SDK to resolve this issue.    
    
You can refer the individual assemblies from the below location.    
Assembly location : {installed location}\Syncfusion\Essential Studio\18.1.0.42\Assemblies for Universal Windows\10.0      
    
Please let us know, if you need any further assistance on this. 
Regards,    
Vijayarasan S   


MA Mathew May 14, 2020 02:47 PM UTC

Hi,

Thanks for getting back to me.

I had already taken a look at the documentation here about the required dependencies: https://help.syncfusion.com/uwp/control-dependencies#sftreenavigator To keep the application as lightweight as possible I always like to keep the assemblies to a minimum, so unfortunately I've already experimented with removing the SDK and including the assemblies. And it didn't make any difference.

With the SDK referenced in release mode, the call to Serialize raises the exception. And with the SDK excluded from the release with following assemblies included, it still raises the exception:

Syncfusion.SfGrid.UWP
Syncfusion.Data.UWP
Syncfusion.SfInput.UWP
Syncfusion.SfShared.UWP
Syncfusion.SfGridConverter.UWP
Syncfusion.XlsIO.UWP
Syncfusion.Pdf.UWP

I'm interested to hear you mention memory usage when building the application. I have 32GB memory on my machine, my memory usage never gets anywhere near capacity and the application builds fine. The application builds without any problems, the SyncFusion data grid appears at runtime without any problems, it is only when I call Serialize on the grid that I get the exception.

For your own reference, the SDK is version 18.1.0.42. The assemblies are on version 18.1460.0.42.

Mat


MA Mathew May 14, 2020 04:12 PM UTC

I have built a very simple project which demonstrates the issue. Please run it in debug to confirm it is working, then run it again in release mode to confirm it breaks.

The project I've put together references the SDK, I can make another project which just references the required assemblies, but as I've said, it makes no difference to whether the exception is raised or not.

Attachment: GridSerializeTest_5d1a2f8d.zip


VS Vijayarasan Sivanandham Syncfusion Team May 17, 2020 03:40 PM UTC

Hi Mathew,

Thanks for the update.

Currently, we are analyzing your requirement of “Serialization Exception in Release Mode”. We will validate and update you the details on May 19, 2020.
 
 
We appreciate your patience until then. 
 
Regards, 
Vijayarasan S 



SS Susmitha Sundar Syncfusion Team May 21, 2020 01:35 AM UTC

Hi Mathew, 
 
Sorry for the inconvenience caused. 
 
Due to complexity of this issue, we did not find the exact root cause for your reported issue. We will analyze and update you with details on or before May 22, 2020. 
 
 We appreciate your patience until then. 
 
Regards, 
Susmitha S 



MA Mathew May 22, 2020 11:20 AM UTC

Hi,

No problem at all. In the interim I have put together a fairly primitive work around. I've cut back the allowed functionality to column moving and re-sizing, so the work around methods I've built just store and restore the order of the columns and the column widths.

To store columns:
         public static string GetGridSettings(SfDataGrid grid)
        {
            try
            {
                List<GridColumnProps> colProps = new List<GridColumnProps>();

                foreach (GridColumn col in grid.Columns)
                {
                    colProps.Add(new GridColumnProps
                    {
                        MappingName = col.MappingName,
                        Width = col.Width
                    });
                }
                return JsonConvert.SerializeObject(colProps);
            }
            catch 
            {
                return "";
            }
        }


To restore columns:
         public static void RestoreGridSettings(SfDataGrid grid, string colPropsStr)
        {
            try
            {
                if (string.IsNullOrEmpty(colPropsStr)) return;
                List<GridColumnProps> colProps = JsonConvert.DeserializeObject<List<GridColumnProps>>(colPropsStr);

                grid.GroupSummaryRows.Clear();
                grid.TableSummaryRows.Clear();
                grid.ClearFilters();

                string mappingName;
                double width;

                List<GridColumn> gridColumns = grid.Columns.ToList<GridColumn>();
                grid.Columns.Clear();

                foreach (GridColumnProps col in colProps)
                {
                    mappingName = col.MappingName;
                    width = col.Width;

                    foreach (GridColumn gridCol in gridColumns)
                    {
                        if (gridCol.MappingName == mappingName)
                        {
                            if (!double.IsNaN(width))
                            {
                                gridCol.Width = width;
                            }
                            grid.Columns.Add(gridCol);
                            break;
                        }
                    }
                }
            }
            catch { }
        }

That'll keep the app working for now :-)

Let me know if you need any assistance implementing/testing a future fix. I'm more than happy to help.

Mat


SS Susmitha Sundar Syncfusion Team May 22, 2020 02:15 PM UTC

Hi Mathew, 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. 
 
Regards, 
Susmitha S 


Loader.
Up arrow icon