Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

XML DeSerializazion error #7541

Closed
hhblaze opened this issue Jun 20, 2019 · 4 comments
Closed

XML DeSerializazion error #7541

hhblaze opened this issue Jun 20, 2019 · 4 comments

Comments

@hhblaze
Copy link

hhblaze commented Jun 20, 2019

Only after running native executable (standard .NetCore works)

public class SerClass
    {
        public SerClass()
        {
            Name = String.Empty;
            MinimalVersion = "0";
            MaximalVersion = "40001212180000";
            Version = "200810101800";
            Path = String.Empty;
            Minimal = 0;
        }

        public string Name { get; set; }

        public string MinimalVersion { get; set; }


        public string MaximalVersion { get; set; }

        public string Version { get; set; }


        public string Path { get; set; }


        public byte[] Zipped { get; set; }


        public long Minimal { get; set; }
    }
 static void TestXmlSerialization()
        {
            try
            {
                SerClass ai = new SerClass()
                {
                    Minimal = 4568654,
                    Name = "addinname",
                    Path = "pathtozipfile",
                    MaximalVersion = "smv",
                    MinimalVersion = "sminvc",
                    Version = " dsfsdf",
                    Zipped = new byte[] { 1, 2, 3 }
                };

                Console.WriteLine("---se");
                var sxml = SerializeXml(ai);
                Console.WriteLine(sxml);
                Console.WriteLine("---de");
                var ai1 = DeserializeXml<SerClass>(sxml);
                Console.WriteLine("---done");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
           
        }

  public static string SerializeXml(this object objectForSerialization)
        {
            try
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(objectForSerialization.GetType());

                string r = String.Empty;

                using (System.IO.StringWriter wr = new System.IO.StringWriter())
                {
                    xs.Serialize(wr, objectForSerialization);
                    r = wr.GetStringBuilder().ToString();
                    wr.Close();
                }

                return r;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            
        }


        public static T DeserializeXml<T>(this string str)
        {
            try
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));

                object r = null;
                using (System.IO.StringReader sr = new System.IO.StringReader(str))
                {
                    r = xs.Deserialize(new System.IO.StringReader(str));
                    sr.Close();
                }

                return (T)r;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            
        }

Result:

---se
<?xml version="1.0" encoding="utf-16"?>
<SerClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>addinname</Name>
  <MinimalVersion>sminvc</MinimalVersion>
  <MaximalVersion>smv</MaximalVersion>
  <Version> dsfsdf</Version>
  <Path>pathtozipfile</Path>
  <Zipped>AQID</Zipped>
  <Minimal>4568654</Minimal>
</SerClass>
---de
System.InvalidOperationException: There is an error in XML document (2, 2). ---> EETypeRva:0x018729F8(System.Reflection.MissingRuntimeArtifactException): MakeGenericMethod() cannot create this generic method instantiation because the instantiation was not metadata-enabled: 'System.Xml.Serialization.ReflectionXmlSerializationReaderHelper.GetSetMemberValueDelegateWithType<GpsCarControl.LicenceServer.SerClass,System.Int64>(System.Reflection.MemberInfo)' For more information, please visit http://go.microsoft.com/fwlink/?LinkID=616868
   at Internal.Reflection.Core.Execution.ExecutionEnvironment.GetMethodInvoker(RuntimeTypeInfo, QMethodDefinition, RuntimeTypeInfo[], MemberInfo) + 0x148
   at System.Reflection.Runtime.MethodInfos.NativeFormat.NativeFormatMethodCommon.GetUncachedMethodInvoker(RuntimeTypeInfo[], MemberInfo) + 0x50
   at System.Reflection.Runtime.MethodInfos.RuntimeMethodInfo.get_MethodInvoker() + 0xa8
   at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo`1.MakeGenericMethod(Type[]) + 0x104
   at System.Xml.Serialization.ReflectionXmlSerializationReader.GetSetMemberValueDelegate(Object, String) + 0x152
   at System.Xml.Serialization.ReflectionXmlSerializationReader.WriteLiteralStructMethod(StructMapping, Boolean, Boolean, String) + 0x905
   at System.Xml.Serialization.ReflectionXmlSerializationReader.WriteElement(ElementAccessor, Boolean, Boolean, Boolean, String, Int32, Int32, XmlSerializationReader.Fixup, ReflectionXmlSerializationReader.Member) + 0x462
   at System.Xml.Serialization.ReflectionXmlSerializationReader.WriteMemberElementsIf(ReflectionXmlSerializationReader.Member[], ReflectionXmlSerializationReader.Member, UnknownNodeAction, XmlSerializationReader.Fixup, ReflectionXmlSerializationReader.CheckTypeSource) + 0x30e
   at System.Xml.Serialization.ReflectionXmlSerializationReader.GenerateTypeElement(XmlTypeMapping) + 0x1a9
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader, String, XmlDeserializationEvents) + 0x2e0
@MichalStrehovsky
Copy link
Member

XML deserialization is trying to use reflection on something that wasn't precompiled. This is possible to solve with RD.XML. See some of the relevant discussion here (and in other issues mentioning this exception): #6477 (comment)

@hhblaze hhblaze closed this as completed Jun 20, 2019
@acarrau
Copy link

acarrau commented Jun 6, 2020

@hhblaze I’m having the exact same problem, but can’t figure out what to set in rd.xml.

Could you share your solution? Thanks!

@hhblaze
Copy link
Author

hhblaze commented Jun 8, 2020

@hhblaze I’m having the exact same problem, but can’t figure out what to set in rd.xml.

Could you share your solution? Thanks!

Well @acarrau , I found rd.xml containing

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="MyAssemblyName" Serialize="All">
      
    </Assembly>
  </Application>
</Directives>

having as an output
MyAssemblyName.dll
MyAssemblyName.exe

also, rd.xml resides on the same level as Program.cs (looks like no other special actions with it in the project)

@acarrau
Copy link

acarrau commented Jun 8, 2020

Thanks for answering.

It didn't work for me to include the assembly tag with Serialize="All". Had to add the specific generic method in rd.xml for System.Private.Xml as in the exception message. See #8186 (comment)

In the end adding my assembly was not even necessary.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants