question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ArgumentNullException when Deserializing a Class in a Collection with an Interface and Migration

See original GitHub issue

Hello everyone, I have found a problem with Migrations again. This time it seems to be a very special scenario involving interfaces and different namespaces. My Issue Example:

using ExtendedXmlSerializer.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;

namespace ExtendedXmlSerializer.Tests.Performance
{
  class MigrationInterfaceNamespaceIssue
  {
    public static void Run()
    {
      var asset = new FirstNameSpace.ItemContainer();
      for (int i = 0; i < 1; i++)
      {
        var subject = new FirstNameSpace.Class1();
        subject.obj = new SecondNameSpace.Class2();
        asset.Items.Add(subject);
      }

      IConfigurationContainer config = new ConfigurationContainer();
      config.Type<FirstNameSpace.ItemContainer>().AddMigration(new EmptyMigration());
      var serializer = config.Create();

      var output = serializer.Serialize(asset);

      // Exception is thrown: ArgumentNullException
      var loaded = serializer.Deserialize<FirstNameSpace.ItemContainer>(output);
    }

    public class EmptyMigration : IEnumerable<Action<XElement>>
    {
      public IEnumerator<Action<XElement>> GetEnumerator()
      {
        yield return x => { }; //Do nothing
      }

      IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

    }

  }

  namespace FirstNameSpace
  {
    class ItemContainer
    {

      public List<Class1> Items { get; set; } = new List<Class1>();

    }

    class Class1
    {

      public Interface1 obj { get; set; }
    }

    interface Interface1 { }

  }

  namespace SecondNameSpace
  {
    class Class2 : FirstNameSpace.Interface1 { }
  }
}

I get following Stacktrace:

   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at ExtendedXmlSerializer.Core.Sources.CacheBase`2.Get(TKey key) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\Core\Sources\CacheBase.cs:line 34
   at ExtendedXmlSerializer.ContentModel.Identification.IdentityStore.Get(String name, String identifier) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Identification\IdentityStore.cs:line 10
   at ExtendedXmlSerializer.ExtensionModel.Xml.IdentityMapper.Get(String name, String identifier) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionModel\Xml\IdentityMapper.cs:line 17
   at ExtendedXmlSerializer.ContentModel.Reflection.TypePartReflector.Create(TypeParts parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Reflection\TypePartReflector.cs:line 24
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at ExtendedXmlSerializer.Core.Sources.CacheBase`2.Get(TKey key) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\Core\Sources\CacheBase.cs:line 34
   at ExtendedXmlSerializer.ContentModel.Reflection.TypeParser.Create(String parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Reflection\TypeParser.cs:line 22
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at ExtendedXmlSerializer.Core.Sources.CacheBase`2.Get(TKey key) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\Core\Sources\CacheBase.cs:line 34
   at ExtendedXmlSerializer.ContentModel.Reflection.ReflectionParser.Create(String parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Reflection\ReflectionParser.cs:line 41
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at ExtendedXmlSerializer.Core.Sources.CacheBase`2.Get(TKey key) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\Core\Sources\CacheBase.cs:line 34
   at ExtendedXmlSerializer.ExtensionModel.Xml.FormatReaderContext.Get(String parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionModel\Xml\FormatReaderContext.cs:line 19
   at ExtendedXmlSerializer.ExtensionModel.Xml.XmlReader.Get(String parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionModel\Xml\XmlReader.cs:line 29
   at ExtendedXmlSerializer.ContentModel.Format.ParsedContent`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Format\ParsedContent.cs:line 16
   at ExtendedXmlSerializer.ContentModel.Format.ContextualReader`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Format\ContextualReader.cs:line 17
   at ExtendedXmlSerializer.ContentModel.DecoratedReader`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\DecoratedReader.cs:line 26
   at ExtendedXmlSerializer.ContentModel.Reflection.TypedParsingReader.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Reflection\TypedParsingReader.cs:line 15
   at ExtendedXmlSerializer.ContentModel.TrackingReader`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\TrackingReader.cs:line 16
   at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 12
   at ExtendedXmlSerializer.ContentModel.Serializer`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Serializer.cs:line 22
   at ExtendedXmlSerializer.ContentModel.Properties.Property`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Properties\Property.cs:line 17
   at ExtendedXmlSerializer.ContentModel.Reflection.Classification.FromAttributes(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Reflection\Classification.cs:line 36
   at ExtendedXmlSerializer.ContentModel.Reflection.Classification.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Reflection\Classification.cs:line 32
   at ExtendedXmlSerializer.ContentModel.RuntimeSerializer.Get(IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\RuntimeSerializer.cs:line 25
   at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 12
   at ExtendedXmlSerializer.ContentModel.Serializer`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Serializer.cs:line 22
   at ExtendedXmlSerializer.ContentModel.Members.MemberSerializer.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\MemberSerializer.cs:line 22
   at ExtendedXmlSerializer.ContentModel.Members.MemberHandler.Handle(IInnerContent contents, IMemberSerializer member) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\MemberHandler.cs:line 13
   at ExtendedXmlSerializer.ContentModel.Content.InnerContentServices.Handle(IInnerContent contents, IMemberSerializer member) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Content\InnerContentServices.cs:line 41
   at ExtendedXmlSerializer.ContentModel.Members.MemberInnerContentHandler.IsSatisfiedBy(IInnerContent parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\MemberInnerContentHandler.cs:line 31
   at ExtendedXmlSerializer.ContentModel.Content.InnerContentReader.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Content\InnerContentReader.cs:line 26
   at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 12
   at ExtendedXmlSerializer.ContentModel.Serializer`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Serializer.cs:line 22
   at ExtendedXmlSerializer.ContentModel.SerializerAdapter`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\SerializerAdapter.cs:line 41
   at ExtendedXmlSerializer.ExtensionModel.References.ReferenceAwareSerializers.Serializer.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionModel\References\ReferenceAwareSerializers.cs:line 54
   at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 12
   at ExtendedXmlSerializer.ContentModel.Serializer`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Serializer.cs:line 22
   at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 12
   at ExtendedXmlSerializer.ContentModel.Serializer`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Serializer.cs:line 22
   at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned(IReader this, IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 15
   at ExtendedXmlSerializer.ContentModel.Collections.CollectionContentsHandler.Handle(IListInnerContent contents, IReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Collections\CollectionContentsHandler.cs:line 13
   at ExtendedXmlSerializer.ContentModel.Content.InnerContentServices.Handle(IListInnerContent contents, IReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Content\InnerContentServices.cs:line 43
   at ExtendedXmlSerializer.ContentModel.Collections.CollectionInnerContentHandler.IsSatisfiedBy(IInnerContent parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Collections\CollectionInnerContentHandler.cs:line 27
   at ExtendedXmlSerializer.ContentModel.Collections.CollectionWithMembersInnerContentHandler.IsSatisfiedBy(IInnerContent parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Collections\CollectionWithMembersInnerContentHandler.cs:line 19
   at ExtendedXmlSerializer.ContentModel.Content.InnerContentReader.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Content\InnerContentReader.cs:line 26
   at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 12
   at ExtendedXmlSerializer.ContentModel.Serializer`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Serializer.cs:line 22
   at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 12
   at ExtendedXmlSerializer.ContentModel.Serializer`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Serializer.cs:line 22
   at ExtendedXmlSerializer.ContentModel.Members.MemberSerializer.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\MemberSerializer.cs:line 22
   at ExtendedXmlSerializer.ContentModel.Members.MemberHandler.Handle(IInnerContent contents, IMemberSerializer member) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\MemberHandler.cs:line 13
   at ExtendedXmlSerializer.ContentModel.Content.InnerContentServices.Handle(IInnerContent contents, IMemberSerializer member) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Content\InnerContentServices.cs:line 41
   at ExtendedXmlSerializer.ContentModel.Members.MemberInnerContentHandler.IsSatisfiedBy(IInnerContent parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\MemberInnerContentHandler.cs:line 31
   at ExtendedXmlSerializer.ContentModel.Content.InnerContentReader.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Content\InnerContentReader.cs:line 26
   at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 12
   at ExtendedXmlSerializer.ContentModel.Serializer`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\Serializer.cs:line 22
   at ExtendedXmlSerializer.ExtensionModel.Xml.MigrationsExtension.Contents.Serializer.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionModel\Xml\MigrationsExtension.cs:line 142
   at ExtendedXmlSerializer.ContentModel.SerializerAdapter`1.Get(IFormatReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ContentModel\SerializerAdapter.cs:line 41
   at ExtendedXmlSerializer.ExtensionModel.Xml.Read.Get(XmlReader parameter) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionModel\Xml\Read.cs:line 26
   at ExtendedXmlSerializer.ExtensionModel.Xml.Serializer.Deserialize(XmlReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionModel\Xml\Serializer.cs:line 21
   at ExtendedXmlSerializer.ExtensionModel.Xml.ExtendedXmlSerializer.Deserialize(XmlReader reader) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionModel\Xml\ExtendedXmlSerializer.cs:line 18
   at ExtendedXmlSerializer.ExtensionMethodsForSerialization.Deserialize[T](IExtendedXmlSerializer this, XmlReaderSettings settings, Stream stream) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionMethodsForSerialization.cs:line 145
   at ExtendedXmlSerializer.ExtensionMethodsForSerialization.Deserialize[T](IExtendedXmlSerializer this, XmlReaderSettings settings, String data) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionMethodsForSerialization.cs:line 121
   at ExtendedXmlSerializer.ExtensionMethodsForSerialization.Deserialize[T](IExtendedXmlSerializer this, String data) in C:\Users\mrapka\source\repos\ExtendedXmlSerializerOrig\src\ExtendedXmlSerializer\ExtensionMethodsForSerialization.cs:line 109
   at ExtendedXmlSerializer.Tests.Performance.MigrationInterfaceNamespaceIssue.Run()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Mike-E-angelocommented, Mar 27, 2021

Doh, forgot to check if the added attribute was already added. Try this one:

https://github.com/ExtendedXmlSerializer/home/pull/518#issuecomment-808739734

2reactions
Mike-E-angelocommented, Mar 26, 2021

Thank you for reporting this @imperiobadgo. I did take a look into it and it appears the ns1 is not being resolved somehow. Looks like we both found the workaround in tandem. 😅 I will look further into this and see if I can find the root cause.

FWIW, UseOptimizedNamespaces does a reference walk through the entire graph so it knows which namespaces to emit at the root element, essentially serializing twice, hence the performance drag.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialization of reference types without parameterless ...
The answer from jawa states that Deserializing to immutable classes and structs can be achieved with System.Text.Json, by creating a custom ...
Read more >
ArgumentNullException Class (System)
An ArgumentNullException exception is thrown when a method is invoked and at least one of the passed arguments is null but should never...
Read more >
Refit: The automatic type-safe REST library for .NET Core ...
JSON requests and responses are serialized/deserialized using an instance of the IHttpContentSerializer interface. Refit provides two implementations out of the ...
Read more >
Introduction to Unity Serialization | Serialization | 1.1.1 ...
The serialization system supports migration and version through an API similar to the adapters. In order to implement migration for a given type,...
Read more >
JSON Serialization and Deserialization
This dictionary must be deserialized such that the objects it contains are lists (in the form of IEnumerable collections), objects (in the form...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found