Skip to content

Bug with xmlserializer when deployed to IIS #44995

Closed

Description

@kostasxagoraris

I have a project that was created originally with blazor web assembly 3.2.
It used some more .net standard 2.0 projects to serialize a few classes from and to xml (also under .net standard 2.0).
I recently upgraded the blazor web app to .net 5.0 . (the projects under .net standard 2.0 remained as they were)
While everything on my development machines run smoothly when i deploy to iis I cant instantiate the xml serializer

[NotSupportedException] : "Parent does not have a default constructor. The default constructor must be explicitly defined."

The stack trace
at System.Reflection.Emit.TypeBuilder.DefineDefaultConstructor(MethodAttributes attributes)
blazor.webassembly.js:1 at System.Xml.Serialization.XmlSerializationILGen.GenerateBaseSerializer(String baseSerializer, String readerClass, String writerClass, CodeIdentifiers classes)
blazor.webassembly.js:1 at System.Xml.Serialization.TempAssembly.GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace)
blazor.webassembly.js:1 at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location)
blazor.webassembly.js:1 at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace, String location)
blazor.webassembly.js:1 at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace)
blazor.webassembly.js:1 at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
blazor.webassembly.js:1 at System.Xml.Serialization.XmlSerializer..ctor(Type type)
blazor.webassembly.js:1 at PanasoftUtilities.XMLStatic.Create_ClassObject_From_XMLDeserialization[AADEBooksParam](String xml, String& Errormessage, Type[] ExtraTypes)

Source: System.Private.CoreLib

TargetSite: DefineDefaultConstructor

Of course there is no lack of default constructor anywhere. And anyway it works fine when running the site on visual studio and it worked fine under blazor 3.2 .net core 3.1.

I tried publishing the site using different combinations of x64 x86 and target runtime.
I ve being struggling with the issue for 2 days. I don't know what else to do.

Do you need me to upload the published artifacts so that you have a look?

Thanks for your advice.

Activity

  1. javiercn commented on Nov 19, 2020

    @javiercn
    Member

    @kostasxagoraris thanks for contacting us.

    This is likely caused by the linker being more agressive in 5.0, you can customize the linker following the steps here

  2. gonsalvg commented on Nov 19, 2020

    @gonsalvg

    I was having the same issue, though on an asp.net hosted setup. Following @javiercn 's link I just decided to temporarily disable the crazy trimming by setting <PublishTrimmed>false</PublishTrimmed> in my client project's propertygroup, which fixed this. Obviously hope to re-enable the feature soon as it seems nice :)

    I also found this post helpful https://devblogs.microsoft.com/dotnet/app-trimming-in-net-5/

  3. kostasxagoraris commented on Nov 19, 2020

    @kostasxagoraris
    Author

    I tried different things but i still havent resolved the issue (disabling trimming all the way is one of them ) I will try again tomorrow. I m sure i m doing something wrong because the error insists

  4. kostasxagoraris commented on Nov 20, 2020

    @kostasxagoraris
    Author

    I have disable trimming completely in my blazor webassembly client project and in the server project

    <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
      <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
        <PublishTrimmed>false</PublishTrimmed>
        <BlazorCacheBootResources>false</BlazorCacheBootResources>
      </PropertyGroup>
    
      <ItemGroup>....
    

    like this i still get the same issue.

  5. kostasxagoraris commented on Nov 20, 2020

    @kostasxagoraris
    Author

    I also used this command

    dotnet publish Panasoft.Web.MyData.Server.csproj -p:PublishTrimmed=false --self-contained --runtime win-x64 -c release
    and the problem is still there. Very persistent!!

    I m a bit frustrated What else can I do?

  6. transferred this issue fromdotnet/aspnetcoreon Nov 20, 2020
  7. added
    untriaged
    on Nov 20, 2020
  8. javiercn commented on Nov 20, 2020

    @javiercn
    Member

    This seems to be an issue with the XML serializer running on WASM, the user disabled trimming and the problem still persisted

  9. added
    arch-wasm
    on Nov 23, 2020
  10. kostasxagoraris commented on Nov 23, 2020

    @kostasxagoraris
    Author

    I also tried to copy the files from the build directory to a seperate web site on the same iis that i use for my development and i had the same exact issue.

  11. kostasxagoraris commented on Nov 24, 2020

    @kostasxagoraris
    Author

    Yet another similar issue
    Trying to transform xml with xslt
    Here is the working code .net code 3.1

     protected async override Task OnParametersSetAsync()
        {
            using (StringReader text = new StringReader(TraceLog.Trace))
            {
    
    
                XPathDocument myXPathDoc = new XPathDocument(text);
                XslCompiledTransform myXslTrans = new XslCompiledTransform();
                string xmlStr = await Http.GetTraceXSLT(Navigation.ToAbsoluteUri("XSLTTraceToHTML.xslt").AbsoluteUri);
                using (StringReader str = new StringReader(xmlStr))
                using (var reader = System.Xml.XmlReader.Create(str))
                {
                    myXslTrans.Load(reader);
                    StringWriter myWriter = new StringWriter();
                    myXslTrans.Transform(myXPathDoc, null, myWriter);
                    TraceHtml = myWriter.GetStringBuilder().ToString();
    
                }
            }
    
        }

    And here is the error with .Net 5.0

    (unknown) Unhandled Exception:
    window.console.error
    (unknown) System.ArgumentNullException: ArgumentNull_Generic Arg_ParamName_Name, con
    window.console.error
    (unknown)    at System.Reflection.Emit.CustomAttributeBuilder.Initialize(ConstructorInfo con, Object[] constructorArgs, PropertyInfo[] namedProperties, Object[] propertyValues, FieldInfo[] namedFields, Object[] fieldValues)
    window.console.error
    (unknown)    at System.Reflection.Emit.CustomAttributeBuilder..ctor(ConstructorInfo con, Object[] constructorArgs)
    window.console.error
    (unknown)    at System.Xml.Xsl.IlGen.XmlILModule.CreateLREModule()
    window.console.error
    (unknown)    at System.Xml.Xsl.IlGen.XmlILModule..cctor()
    window.console.error
    (unknown) crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
          Unhandled exception rendering component: TypeInitialization_Type, System.Xml.Xsl.IlGen.XmlILModule
    System.TypeInitializationException: TypeInitialization_Type, System.Xml.Xsl.IlGen.XmlILModule
     ---> System.ArgumentNullException: ArgumentNull_Generic Arg_ParamName_Name, con
       at System.Reflection.Emit.CustomAttributeBuilder.Initialize(ConstructorInfo con, Object[] constructorArgs, PropertyInfo[] namedProperties, Object[] propertyValues, FieldInfo[] namedFields, Object[] fieldValues)
       at System.Reflection.Emit.CustomAttributeBuilder..ctor(ConstructorInfo con, Object[] constructorArgs)
       at System.Xml.Xsl.IlGen.XmlILModule.CreateLREModule()
       at System.Xml.Xsl.IlGen.XmlILModule..cctor()
       Exception_EndOfInnerExceptionStack
       at System.Xml.Xsl.XmlILGenerator.Generate(QilExpression query, TypeBuilder typeBldr)
       at System.Xml.Xsl.XslCompiledTransform.CompileQilToMsil(XsltSettings settings)
       at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
       at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet)
       at Panasoft.Web.MyData.Client.Pages.Monitor.TraceView.OnParametersSetAsync()
       at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
       at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
       at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
    
  12. danmoseley commented on Nov 24, 2020

    @danmoseley
    Contributor

    @eerhardt I wonder whether it would be useful to create a meta label to add to any trim safety issues.

  13. added
    linkable-framework
    on Nov 24, 2020
  14. eerhardt commented on Nov 24, 2020

    @eerhardt
    Member

    I wonder whether it would be useful to create a meta label to add to any trim safety issues.

    I've been using the linkable-framework label.

    The original issue is a duplicate of #41389 and will be resolved with #44772 once it gets approved.

    The 2nd issue is a new one, it looks like the following code needs to be annotated correctly for the ILLinker to preserve the constructors correctly:

    internal static class XmlILConstructors
    {
    public static readonly ConstructorInfo DecFromParts = GetConstructor(typeof(decimal), typeof(int), typeof(int), typeof(int), typeof(bool), typeof(byte));
    public static readonly ConstructorInfo DecFromInt32 = GetConstructor(typeof(decimal), typeof(int));
    public static readonly ConstructorInfo DecFromInt64 = GetConstructor(typeof(decimal), typeof(long));
    public static readonly ConstructorInfo Debuggable = GetConstructor(typeof(DebuggableAttribute), typeof(DebuggableAttribute.DebuggingModes));
    public static readonly ConstructorInfo NonUserCode = GetConstructor(typeof(DebuggerNonUserCodeAttribute));
    public static readonly ConstructorInfo QName = GetConstructor(typeof(XmlQualifiedName), typeof(string), typeof(string));
    public static readonly ConstructorInfo StepThrough = GetConstructor(typeof(DebuggerStepThroughAttribute));
    public static readonly ConstructorInfo Transparent = GetConstructor(typeof(SecurityTransparentAttribute));
    private static ConstructorInfo GetConstructor(Type className)
    {
    ConstructorInfo constrInfo = className.GetConstructor(Type.EmptyTypes)!;
    Debug.Assert(constrInfo != null, "Constructor " + className + " cannot be null.");
    return constrInfo;
    }
    private static ConstructorInfo GetConstructor(Type className, params Type[] args)
    {
    ConstructorInfo constrInfo = className.GetConstructor(args)!;
    Debug.Assert(constrInfo != null, "Constructor " + className + " cannot be null.");
    return constrInfo;
    }
    }

    Side-note: The ILLink warnings caught this as well:

    <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
    <argument>ILLink</argument>
    <argument>IL2070</argument>
    <property name="Scope">member</property>
    <property name="Target">M:System.Xml.Xsl.IlGen.XmlILConstructors.GetConstructor(System.Type,System.Type[])</property>
    </attribute>
    <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
    <argument>ILLink</argument>
    <argument>IL2070</argument>
    <property name="Scope">member</property>
    <property name="Target">M:System.Xml.Xsl.IlGen.XmlILConstructors.GetConstructor(System.Type)</property>
    </attribute>

  15. eerhardt commented on Nov 24, 2020

    @eerhardt
    Member

    @kostasxagoraris - to try to get you unblocked on net5.0, can you try the following?

    In your Blazor Client.csproj, add the following lines:

      <ItemGroup>
        <TrimmerRootAssembly Include="System.Private.Xml" />
        <TrimmerRootDescriptor Include="LinkerWorkaround.xml" />
      </ItemGroup>

    And then create a file named LinkerWorkaround.xml in your Blazor Client.csproj folder with the following contents:

    <?xml version="1.0" encoding="utf-8" ?>
    <linker>
      <assembly fullname="System.Private.CoreLib">
        <type fullname="System.Security.SecurityTransparentAttribute" />
      </assembly>
    </linker>

    I tried this myself, and it worked for me. Let me know if it works for you. We will get this fixed in net6.0.

  16. kostasxagoraris commented on Nov 26, 2020

    @kostasxagoraris
    Author

    @eerhardt Thank you very much That solved all issues. Now it works smoothly!!

  17. ghost locked as resolved and limited conversation to collaborators on Dec 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-wasmarea-Serializationlinkable-frameworkuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions