Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with xmlserializer when deployed to IIS #44995

Closed
kostasxagoraris opened this issue Nov 18, 2020 · 13 comments
Closed

Bug with xmlserializer when deployed to IIS #44995

kostasxagoraris opened this issue Nov 18, 2020 · 13 comments
Labels
arch-wasm WebAssembly architecture area-Serialization linkable-framework Issues associated with delivering a linker friendly framework untriaged New issue has not been triaged by the area owner

Comments

@kostasxagoraris
Copy link

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.

@javiercn
Copy link
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

@gonsalvg
Copy link

gonsalvg commented Nov 19, 2020

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/

@kostasxagoraris
Copy link
Author

kostasxagoraris commented Nov 19, 2020

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

@kostasxagoraris
Copy link
Author

kostasxagoraris commented Nov 20, 2020

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.

@kostasxagoraris
Copy link
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?

@javiercn javiercn transferred this issue from dotnet/aspnetcore Nov 20, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-Serialization untriaged New issue has not been triaged by the area owner labels Nov 20, 2020
@javiercn
Copy link
Member

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

@ghost
Copy link

ghost commented Nov 23, 2020

Tagging subscribers to this area: @buyaa-n, @krwq, @jeffhandley
See info in area-owners.md if you want to be subscribed.

Issue Details

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.

Author: kostasxagoraris
Assignees: -
Labels:

area-Serialization, area-System.Xml, untriaged

Milestone: -

@lewing lewing added the arch-wasm WebAssembly architecture label Nov 23, 2020
@kostasxagoraris
Copy link
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.

@kostasxagoraris
Copy link
Author

kostasxagoraris commented Nov 24, 2020

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)

@danmoseley
Copy link
Member

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

@eerhardt eerhardt added the linkable-framework Issues associated with delivering a linker friendly framework label Nov 24, 2020
@eerhardt
Copy link
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>

@eerhardt
Copy link
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.

@kostasxagoraris
Copy link
Author

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

@ghost ghost locked as resolved and limited conversation to collaborators Dec 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly architecture area-Serialization linkable-framework Issues associated with delivering a linker friendly framework untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

8 participants