I am seeing a problem that I THINK is caused by linker optimizations on serializable classes.
I have a WCF service client class defined in my Xamarin.Forms PCL library. When I run the application under debug (without linker optimization because it is using the mono shared runtime) everything works as expected.
However, when I run the app under release I get a NullReference error. This is the specific stack trace I see:
[MonoDroid] UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
[MonoDroid] at System.ServiceModel.MonoInternal.ClientRuntimeChannel..ctor (System.ServiceModel.Dispatcher.ClientRuntime,System.ServiceModel.Description.ContractDescription,System.TimeSpan,System.TimeSpan,System.ServiceModel.Channels.IChannel,System.ServiceModel.Channels.IChannelFactory,System.ServiceModel.Channels.MessageVersion,System.ServiceModel.EndpointAddress,System.Uri)
[MonoDroid] at System.ServiceModel.MonoInternal.ClientRuntimeChannel..ctor (System.ServiceModel.Description.ServiceEndpoint,System.ServiceModel.ChannelFactory,System.ServiceModel.EndpointAddress,System.Uri)
[MonoDroid] at System.ServiceModel.ClientBase1/ChannelBase
1<m.transport.Svc.ISoapService, m.transport.Svc.ISoapService>.get_Inner () <0x0009f>
[MonoDroid] at System.ServiceModel.ClientBase1/ChannelBase
1<m.transport.Svc.ISoapService, m.transport.Svc.ISoapService>.BeginInvoke (string,object[],System.AsyncCallback,object) <0x00137>
[MonoDroid] at m.transport.Svc.SoapServiceClient/SoapServiceClientChannel.BeginLogin (m.transport.Svc.LoginRequest,System.AsyncCallback,object)
[MonoDroid] at m.transport.Svc.SoapServiceClient.m.transport.Svc.ISoapService.BeginLogin (m.transport.Svc.LoginRequest,System.AsyncCallback,object)
[MonoDroid] at m.transport.Svc.SoapServiceClient.BeginLogin (string,string,string,System.AsyncCallback,object)
[MonoDroid] at m.transport.Svc.SoapServiceClient.OnBeginLogin (object[],System.AsyncCallback,object)
[MonoDroid] at System.ServiceModel.ClientBase1<m.transport.Svc.ISoapService>.InvokeAsync (System.ServiceModel.ClientBase
1/BeginOperationDelegate<m.transport.Svc.ISoapService>,object[],System.ServiceModel.ClientBase
1/EndOperationDelegate<m.transport.Svc.ISoapService>,System.Threading.SendOrPostCallback,object) <0x0029a>
[MonoDroid] at m.transport.Svc.SoapServiceClient.LoginAsync (string,string,string,object) <IL 0x0007e, 0x0035f>
[MonoDroid] at m.transport.Svc.SoapServiceClient.LoginAsync (string,string,string) <IL 0x00005, 0x00043>
[MonoDroid] at m.transport.ViewModels.LoginViewModel.Login (object) <IL 0x0001e, 0x00088>
[MonoDroid] at m.transport.ViewModels.DelegateCommand
1.Execute (object) <0x00062>[MonoDroid] at Xamarin.Forms.Button.SendClicked ()
[MonoDroid] at Xamarin.Forms.Platform.Android.ButtonRenderer/ButtonClickListener.OnClick (Android.Views.View)
[MonoDroid] at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr)
[MonoDroid] at (wrapper dynamic-method) object.5314e107-5021-4398-b5a2-18194feb3bdc (intptr,intptr,intptr)
I think the linker is stripping out something that it thinks is "unused".
I have read about the Preserve Attribute and how to mark methods with it to prevent the linker from stripping it out (see here: http://developer.xamarin.com/guides/android/advanced_topics/linking/)
HOWEVER, since this is a platform specific attribute how do I got about using it in a Xamarin Forms PCL library? Also, if it is possible to use it in a Xamarin Forms library, how do I got about figuring out what method is being stripped?
Or is this not a linker problem? I only seem to see the error on Android where the Release Settings are "Link SDK Assemblies" (running in a GenyMotion emulator). When setting my iOS project to the same Linker settings it appears to work (running in the iOS simulator)?
Also note, when I change the linker settings in EITHER Android or iOS to "Link All Assemblies" I get what also appears to be a linker optimization error (before being able to get to my previous error)
Xamarin.Forms.Xaml.XamlParseException: Position 11:29. No method MenuClicked found on type m.transport.UI.MainPage
at Xamarin.Forms.Xaml.BaseValueNode.SetPropertyValue (System.Object xamlelement, XmlName propertyName, System.Object value, Xamarin.Forms.BindableObject rootElement) [0x00000] in :0
at Xamarin.Forms.Xaml.ValueNode.ApplyTo (System.Object source, XmlName propertyName, Boolean dontUseLazy) [0x00000] in :0
at Xamarin.Forms.Xaml.INodeExtension.ApplyProperties (IElementNode node, System.Object source, Boolean dontUseLazy) [0x00000] in :0
at Xamarin.Forms.Xaml.ElementNode.ApplyProperties (System.Object source, Boolean dontUseLazy) [0x00000] in :0
at Xamarin.Forms.Xaml.ElementNode.Create (Boolean dontUseLazy) [0x00000] in :0
at Xamarin.Forms.Xaml.ElementNode.<.ctor>b__0 () [0x00000] in :0
at System.Lazy`1[System.Object].InitValue () [0x00000] in :0
In this case it appears the linker is stripping out the MenuClicked handler that I have defined in code behind because it doesn't think it is used (I assume because the linker doesn't understand XAML and doesn't realize that the XAML attribute is using this method)
This second case seem like a perfect use case for the Preserve attribute. However this is a Xamarin Forms PCL library so we are stuck?