Bug 7746 - XmlSerializer does not deserialize object array correctly from .NET web service
Summary: XmlSerializer does not deserialize object array correctly from .NET web service
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-10-09 15:23 UTC by Chris Boehm
Modified: 2012-10-10 10:31 UTC (History)
2 users (show)

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

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:
Status:
RESOLVED NOT_REPRODUCIBLE

Description Chris Boehm 2012-10-09 15:23:53 UTC
I've got a web service that is supposed to return an array of objects, but Mono crashes with an "Object reference not set..."  error when attempting to deserialize the response.  I found an interesting workaround which makes the problem go away if I initialize the array to an empty array prior to making the web service call.  You can see this workaround in item #4 below.  Still, it shouldn't be necessary to do this in my opinion.  

 
#1  The error message:

Object reference not set to an instance of an object.
 
 at System.Xml.Serialization.XmlSerializationReader.ReadList (System.Object& resultList) [0x000f7] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReader.cs:570 
  at System.Xml.Serialization.XmlSerializationReader.ReadReferencedElement (System.String name, System.String ns) [0x0008b] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReader.cs:520 
  at System.Xml.Serialization.XmlSerializationReader.ReadReferencedElement () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReader.cs:487 
  at System.Xml.Serialization.XmlSerializationReader.ReadReferencedElements () [0x00039] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReader.cs:612 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMessage (System.Xml.Serialization.XmlMembersMapping typeMap) [0x001db] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:168 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () [0x0004c] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:92 
  at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x0001c] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:361 


#2 The web service (example)

[WebMethod, SoapRpcMethod]
public FooResponse GetFoo()
{
     FooResponse response = new FooResponse();

     Foo f1 = new Foo( "data1", "data2" );
     Foo f2 = new Foo( "data3", "data4" );

     response.ArrayOfFoo = new Foo[]{ f1, f2 };
     response.Status = "OK";

     return response;
}


#3 Example service call (crashes):

public BadFooCall()
{
    FooResponse response = new FooResponse();

    using( FooService svc = new FooService() )
    {
       try
       {
           response = svc.GetFoo();  //THIS IS WHERE IT CRASHES
       }
       catch( Exception e )
       {
           response.Status = e.Message;   
       }
    }
}


#4 Example service call (with workaround):

public GoodFooCall()
{
    FooResponse response = new FooResponse();

    response.ArrayOfFoo = new Foo[] {};   //THIS LINE OF CODE MAKES THE ERROR GO AWAY,  Weird, huh?

    using( FooService svc = new FooService() )
    {
       try
       {
         response = svc.GetFoo();  //no longer crashes here when workaround applied
       }
       catch( Exception e )
       {
         response.Status = e.Message;   
       }
    }
}
Comment 1 Rolf Bjarne Kvinge [MSFT] 2012-10-09 17:34:50 UTC
You don't mention if it's happening on both device and in the simulator or only one of them.

If it's device-only, you're most likely running into issues with the managed linker which removes unused code from your app. The quick way to test this is to disable linking (right-click project, Options, iPhone Build page and set "Linker behavior" to "Don't link" - could you try this and see if it makes the problem go away?

There is more documentation about the manager linker here in case you're interested: http://docs.xamarin.com/ios/advanced_topics/linker
Comment 2 Chris Boehm 2012-10-10 10:31:32 UTC
It was happening on both device and simulator, and I did have Link all Assemblies selected.

I understand now though that the property on my web service object was being stripped out because I hadn't yet reached the point in my development where I was actually using it for something.

Thank you for your prompt response.  I'm marking this as resolved (worksforme).