You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When publishing in Visual Studio, the option to Trim Unused Code is available to select in the publishing profile settings. This feature is VERY useful when much of the code in the application is not used and it is why I usually have it checked by default. However, I recently came across an issue where I published an application to a folder where I used both the Produce a single file and Trim Unused Code options but when I ran the application an exception was thrown that said, "Namespace.Class cannot be serialized because it does not have a parameterless constructor." I am familiar with this exception because I have seen it before. It is the result of using the native .NET serializer and deserializer which require the parameterless constructor in the class.
After triple checking that the class in question had the parameterless constructor and republishing with newly created publishing profiles but I was still getting the same exception.
I went looking for other issues but remembered that all was working in .NET 6 and the purpose of this project was to upgrade the application from .NET 6 to .NET 8.
So out of a gut feeling, I unchecked the Trim Unused Code because I logically arrived at the idea that the parameterless constructor was "unused" in the application as it is written. I assume since I am not explicitly calling the parameterless constructor in application, the compiler sees it as "unused" and therefore trims it out?
Anyway, if my suspicision are correct, this SHOULD NOT HAPPEN! Please fix! Else, what can I do to use the Trim Unused Code option when publishing.
To Reproduce
See attached ExampleApp.zip file with VS solution.
Publish each profile
Use a terminal (I used Powershell) and run each published EXE to see the difference
Exception on the wTrimUnusedCode only.
ExampleNamespace.ExampleClass cannot be serialized because it does not have a parameterless constructor. at System.Xml.Serialization.TypeDesc.CheckSupported()
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type, MemberInfo, Boolean, Boolean)
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type, MemberInfo, Boolean)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type, Boolean)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type, XmlRootAttribute , String )
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at System.Xml.Serialization.XmlUtils.Deserialize[T](FileInfo) in %USERPROFILE%\source\repos\ExampleApp\ExampleApp\XmlUtils.cs:line 22
Further technical details
Output of dotnet --info
.NET SDK:
Version: 9.0.101
Commit: eedb237549
Workload version: 9.0.100-manifests.4a280210
MSBuild version: 17.12.12+1cce77968
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.101\
.NET workloads installed:
[aspire]
Installation Source: VS 17.12.35527.113
Manifest Version: 8.2.2/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.2\WorkloadManifest.json
Install Type: Msi
Configured to use loose manifests when installing new manifests.
Host:
Version: 9.0.0
Architecture: x64
Commit: 9d5a6a9aa4
.NET SDKs installed:
6.0.203 [C:\Program Files\dotnet\sdk]
6.0.428 [C:\Program Files\dotnet\sdk]
9.0.101 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
The IDE info
Microsoft Visual Studio Professional 2022
Version 17.12.3
VisualStudio.17.Release/17.12.3+35527.113
Microsoft .NET Framework
Version 4.8.09037
The text was updated successfully, but these errors were encountered:
Trim Unused Code Bug
Description
When publishing in Visual Studio, the option to
Trim Unused Code
is available to select in the publishing profile settings. This feature is VERY useful when much of the code in the application is not used and it is why I usually have it checked by default. However, I recently came across an issue where I published an application to a folder where I used both theProduce a single file
andTrim Unused Code
options but when I ran the application an exception was thrown that said, "Namespace.Class cannot be serialized because it does not have a parameterless constructor." I am familiar with this exception because I have seen it before. It is the result of using the native .NET serializer and deserializer which require the parameterless constructor in the class.After triple checking that the class in question had the parameterless constructor and republishing with newly created publishing profiles but I was still getting the same exception.
I went looking for other issues but remembered that all was working in .NET 6 and the purpose of this project was to upgrade the application from .NET 6 to .NET 8.
So out of a gut feeling, I unchecked the
Trim Unused Code
because I logically arrived at the idea that the parameterless constructor was "unused" in the application as it is written. I assume since I am not explicitly calling the parameterless constructor in application, the compiler sees it as "unused" and therefore trims it out?Anyway, if my suspicision are correct, this SHOULD NOT HAPPEN! Please fix! Else, what can I do to use the Trim Unused Code option when publishing.
To Reproduce
See attached ExampleApp.zip file with VS solution.
Publish each profile
Use a terminal (I used Powershell) and run each published EXE to see the difference
Exception on the
wTrimUnusedCode
only.Further technical details
dotnet --info
The text was updated successfully, but these errors were encountered: