2

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)
poupou
  • 43,413
  • 6
  • 77
  • 174
Charles
  • 43
  • 4
  • what is the error you see when it crashes? – Viral Patel Jan 06 '16 at 20:22
  • On the device I see "Unfortunately has stopped" In monitor, 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 ... – Charles Jan 06 '16 at 20:33
  • that is when you'll see the error on logcat (with phone connected). post it here. – Viral Patel Jan 06 '16 at 20:34
  • Good point. Added info to original post. – Charles Jan 06 '16 at 21:43

2 Answers2

3

I think this due to too aggressive Xamarin.Android linking. This still occurs with Xamarin's cycle 7 release. We faced the same issue and for now have worked around it by adding

<AndroidLinkSkip>System.Runtime.Serialization</AndroidLinkSkip>

in the Android csproj file for the Release configuration.

Waiting for a Xamarin update with a proper fix for this... See https://bugzilla.xamarin.com/show_bug.cgi?id=37491

vlillkall
  • 31
  • 2
1

Your issue is here:

No set method for property 'OffsetMinutes' in type 'System.Runtime.Serialization.DateTimeOffsetAdapter'.

Check if you are making the call right where ever it is being used.

If you are using proguard when doing a release build, ensure the method causing the issue is not getting removed.

Also, check this post for answers to the same exception reported by another user.

Community
  • 1
  • 1
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • The code I am using is code that has been written countless times in apps that use DataContractJsonSerializer. – Charles Jan 06 '16 at 21:48
  • have you enable proguard for release build? – Viral Patel Jan 06 '16 at 21:48
  • also check this : http://stackoverflow.com/questions/2362936/system-runtime-serialization-invaliddatacontractexception-no-set-method-for-pro – Viral Patel Jan 06 '16 at 21:52
  • You are on the right track. Unfortunately, DateTimeOffsetAdapter is a class that is part of the xamarin System.Runtime.Serialization library. I cannot change it. And, interestingly, Reflector shows me that it does in fact have a setter for OffsetMinutes. – Charles Jan 06 '16 at 21:59
  • You should got in touch with the xamarin support team probably to seek help on this of it is not in your control. – Viral Patel Jan 06 '16 at 22:07
  • 1
    Proguard is a Java specifc tool; it won't apply to Xamarin.Android apps (which are built in C# using the mono vm). @Charles, you probably have the linker enabled in release mode and it's stripping out the property setter for `OffsetMinutes` in `DateTimeOffsetAdapter`. – matthewrdev Jan 06 '16 at 22:29
  • 1
    @matthewrdev, I agree, that is probably what is happening. Oddly this wasn't happening until recently. This code was working great until I moved to VS2015 and xamarin.android 6.0 – Charles Jan 06 '16 at 22:48