In order to understand the issue, I created a plain blank Xamarin Android app. With the following simple bit of code, I can get the Release version of the app to crash when I press the button. Note that in order to build, I needed to add a reference to System.Runtime.Serialization:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate
{
using (MemoryStream stream = new MemoryStream())
{
DateTimeOffset offset = new DateTimeOffset(DateTime.Now);
string serializedOffset;
var serializer = new DataContractJsonSerializer(typeof(DateTimeOffset));
Log.Info(nameof(MainActivity), "HACKAGE:about to serialize");
serializer.WriteObject(stream, offset);
serializedOffset = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Position);
Log.Info(nameof(MainActivity), "HACKAGE: got serialized" + serializedOffset);
}
button.Text = string.Format("{0} clicks!", count++);
};
}
static void ExistenceCausesCrash(string serializedObject)
{
DateTimeOffset resultEx;
var serializer = new DataContractJsonSerializer(typeof(DateTimeOffset));
byte[] serializedObjectBytes = Encoding.UTF8.GetBytes(serializedObject);
using (var stream = new MemoryStream(serializedObjectBytes))
{
resultEx = (DateTimeOffset)serializer.ReadObject(stream);
}
}
The interesting thing is this: If I comment out the ExistenceCausesCrash()
routine, the app does not crash.
I am using:
Microsoft Visual Studio Enterprise 2015
Version 14.0.24720.00 Update 1
Microsoft .NET Framework
Version 4.6.01038
Xamarin 4.0.0.1717 (1390b70)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.
Xamarin.Android 6.0.0.35 (d300845)
Visual Studio plugin to enable development for Xamarin.Android.
Xamarin.iOS 9.3.99.33 (ea30b32)
Visual Studio extension to enable development for Xamarin.iOS.
It seems to me that this started happening when I upgraded to 4.0.0.1717
In logcat, I see:
01-06 12:32:01.850: I/MonoDroid(18126): UNHANDLED EXCEPTION:
01-06 12:32:01.861: I/MonoDroid(18126): System.Runtime.Serialization.InvalidDataContractException: No set method for property 'OffsetMinutes' in type 'System.Runtime.Serialization.DateTimeOffsetAdapter'.
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.DataContract+DataContractCriticalHelper.ThrowInvalidDataContractException (System.String message, System.Type type) [0x0007b] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.DataContract.ThrowInvalidDataContractException (System.String message, System.Type type) [0x00000] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteToJson (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString[] memberNames) [0x00051] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.JsonFormatWriterGenerator+CriticalHelper+<GenerateClassWriter>c__AnonStorey0.<>m__0 (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString[] memberNames) [0x0000b] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.JsonClassDataContract.WriteJsonValueCore (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) [0x00027] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.JsonDataContract.WriteJsonValue (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) [0x00007] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteJsonValue (System.Runtime.Serialization.Json.JsonDataContract contract, System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) [0x00000] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.WriteDataContractValue (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, RuntimeTypeHandle declaredTypeHandle) [0x0003d] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) [0x0007a] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.SerializeWithXsiTypeAtTopLevel (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, System.Type graphType) [0x00066] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalWriteObjectContent (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00110] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalWriteObject (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00008] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.XmlObjectSerializer.InternalWriteObject (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00079] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00000] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteObject (System.Xml.XmlDictionaryWriter writer, System.Object graph) [0x0000d] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteObject (System.IO.Stream stream, System.Object graph) [0x00018] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at XamarinDateTimeOffsetBugApp1.MainActivity+<>c__DisplayClass1_0.<OnCreate>b__0 (System.Object <sender>, System.EventArgs <e>) [0x00030] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at Android.Views.View+IOnClickListenerImplementor.OnClick (Android.Views.View v) [0x00014] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (IntPtr jnienv, IntPtr native__this, IntPtr native_v) [0x00011] in <filename unknown>:0
01-06 12:32:01.861: I/MonoDroid(18126): at (wrapper dynamic-method) System.Object:0d369c4c-eddc-4ca3-ad2e-8d81a770584b (intptr,intptr,intptr)