Bug 36710 - DataContractSerializer does not work in release build
Summary: DataContractSerializer does not work in release build
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 6.0.1 (C6SR1)
Hardware: PC Windows
: --- blocker
Target Milestone: 7.1 (C9)
Assignee: Radek Doulik
: 40184 44702 50701 ()
Depends on:
Reported: 2015-12-08 12:39 UTC by Evgeniy Zverev
Modified: 2016-12-23 10:55 UTC (History)
6 users (show)

Is this bug a regression?: ---
Last known good build:

custom linker description to preserve KeyValue`2* type(s) (168 bytes, text/xml)
2016-04-05 20:36 UTC, Radek Doulik

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Evgeniy Zverev 2015-12-08 12:39:30 UTC
# Steps to reproduce
1. Create a Xamarin.Forms (portable) project.
2. In the App.cs file replace the constructor code with the following:

        public App()
            string strResult = "Welcome to Xamarin Forms!";

                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("a", "a");
                dic.Add("b", "b");
                dic.Add("c", "c");

                MemoryStream clsTargetStream = new MemoryStream();

                DataContractSerializer clsSerializer = new DataContractSerializer(dic.GetType());
                clsSerializer.WriteObject(clsTargetStream, dic);
                clsTargetStream.Seek(0, SeekOrigin.Begin);

                StreamReader clsReader = new StreamReader(clsTargetStream);
                strResult += Environment.NewLine + Environment.NewLine + clsReader.ReadToEnd();
            catch (Exception iException)
                strResult = iException.ToString();
                // The root page of your application
                MainPage = new ContentPage
                    Content = new StackLayout
                        VerticalOptions = LayoutOptions.Center,
                        Children = {
                            new Label {
                                XAlign = TextAlignment.Center,
                                Text = strResult

3. Build and run the project for Android platform in debug. The project runs as expected.
4. Build and run the project for Android platform in release.

# Expected behavior
The screen shows the standard "Welcome to Xamarin Forms!" and serialized data (XML).

# Actual behavior
The screen shows the exception data.

# Supplemental info (logs, images, videos)

# Test environment (full version information)

Microsoft Visual Studio Enterprise 2015
Version 14.0.24720.00 Update 1
Microsoft .NET Framework
Version 4.6.01038

Installed Version: Enterprise

Architecture and Modeling Tools   00322-80000-00000-AA201
Microsoft Architecture and Modeling Tools
UML® and Unified Modeling Language™ are trademarks or registered trademarks of the Object Management Group, Inc. in the United States and other countries.

Visual C# 2015   00322-80000-00000-AA201
Microsoft Visual C# 2015

NuGet Package Manager   3.3.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Visual Studio Tools for Universal Windows Apps   14.0.24720.00
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Xamarin (deffc90)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android (3efa14c)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS (3c0ec35)
Visual Studio extension to enable development for Xamarin.iOS.
Comment 1 Evgeniy Zverev 2015-12-08 14:10:30 UTC
As a temporary workaround I excluded the "System.Runtime.Serialization" assembly from linking in Android project properties.
Comment 2 Brendan Zagaeski (Xamarin Team, assistant) 2015-12-08 17:24:37 UTC
## The exception message produced by the test case in comment 0

> System.MissingMethodException: Constructor on type 'System.Runtime.Serialization.CollectionDataContract+GenericDictionaryEnumerator`2[[System.String, mscorlib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, mscorlib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' not found.
>   at System.RuntimeType.CreateInstanceImpl (BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) <0x94ca4bc0 + 0x002f3> in <filename unknown>:0 

Based on that exception message, this appears to be a duplicate of Bug 36250. I will mark it accordingly.

*** This bug has been marked as a duplicate of bug 36250 ***
Comment 3 Evgeniy Zverev 2016-03-25 10:21:06 UTC
Tested against Xamarin.Android (e98e962) today. The bug transformed into something more fierce and frightening.

Just try the same repro and you end up with no exception and NO DATA.

The result of serialization looks something like this:

<ArrayOfKeyValueOfstringstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><KeyValueOfstringstring/><KeyValueOfstringstring/><KeyValueOfstringstring/></ArrayOfKeyValueOfstringstring>

and should look something like this:

<ArrayOfKeyValueOfstringstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><KeyValueOfstringstring><Key>a</Key><Value>aa</Value></KeyValueOfstringstring><KeyValueOfstringstring><Key>b</Key><Value>bb</Value></KeyValueOfstringstring><KeyValueOfstringstring><Key>c</Key><Value>cc</Value></KeyValueOfstringstring></ArrayOfKeyValueOfstringstring>

It saves the dictionary without Keys and Values i.e. strips your data completely and silently. Makes me shiver in misery and dismay. Argh.

Again. The bug IS NOT fixed, it is now much worse.
Comment 4 Evgeniy Zverev 2016-03-25 10:25:14 UTC
Previously the report was marked as a duplicate of https://bugzilla.xamarin.com/show_bug.cgi?id=36250

Since the bug 36250 was closed as fixed which is not true and nobody seems to care for new comments in the closed bug report I reopen my report.
Comment 5 Radek Doulik 2016-04-05 20:35:32 UTC
I am sorry your problem wasn't fixed completely with the previous fix.

I have the fix for the described problem nearly ready. Meanwhile you can use this workaround. Put attached CustomLinker.xml file to your Android project and set the "Build Action" to LinkDescription. It should fix your bug and you shouldn't need to preserve the whole serialization assembly anymore.
Comment 6 Radek Doulik 2016-04-05 20:36:40 UTC
Created attachment 15614 [details]
custom linker description to preserve KeyValue`2* type(s)
Comment 7 Evgeniy Zverev 2016-04-06 10:12:47 UTC
Are you sure that there is only a problem with the KeyValue<T1, T2> pairs? Did you check that other collections like List or HashSet or non-generic KeyValue are not affected?
Comment 8 Radek Doulik 2016-09-13 08:16:35 UTC
Should be fixed in master by ac5269d54e518d9db6dac31e49de26c8b288662c and ae7d535efa3749ddf2f8e47b2f08f56f1d87e166
Comment 9 Samantha Houts [MSFT] 2016-09-13 16:16:46 UTC
*** Bug 40184 has been marked as a duplicate of this bug. ***
Comment 10 Brendan Zagaeski (Xamarin Team, assistant) 2016-12-23 02:56:30 UTC
*** Bug 50701 has been marked as a duplicate of this bug. ***
Comment 12 Brendan Zagaeski (Xamarin Team, assistant) 2016-12-23 03:57:20 UTC
*** Bug 44702 has been marked as a duplicate of this bug. ***
Comment 13 Parmendra Kumar 2016-12-23 10:55:42 UTC
I have checked this issue with latest C9 and Mater XVS build and observed that this issue has been fixed.
I have also checked with Mac XS and its working fine.

Screencast: https://www.screencast.com/t/PmLPq89wYvq

C9: Xamarin.VisualStudio_4.3.0.498_46382dfe307002b6446568baf70240c619723b5c
Master: Xamarin.VisualStudio_99.1.0.385_c3ad6e143daa383824691a7fd3125e7cf16c9bbd

hence marked this issue as Verified Fixed.