Skip to content

[deploy net7.0] Self-contained exe with trimming unused code throws TargetInvocationException #1974

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

Closed
viordash opened this issue Nov 22, 2022 · 3 comments

Comments

@viordash
Copy link

STR:

  • select in visual studio 2022 17.4.1 project /command-line-api/tree/main/samples/HostingPlayground
  • specify to .NET7 the target framework, and rebuild project
  • publish the sample to folder target, with selected profile settings:
    • Configuration: Release | Any CPU
    • Target framework: net7.0
    • Deployment mode: Self-contained
    • Target runtime: win-x64
    • Target location: default path
    • File publish options: Produce single file, Enable ReadyToRun compilation, Trim unused code
  • execute the published HostingPlayground.exe --name 'Joe'

When the "Trim unused code" checkbox is unchecked, the exception is no longer thrown.

@viordash
Copy link
Author

sorry i forgot the exception details:
Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object. at HostingPlayground.Program.Run(GreeterOptions, IHost) in C:\Users\User\source\repos\command-line-api\samples\HostingPlayground\Program.cs:line 41 at System.RuntimeMethodHandle.InvokeMethod(Object, Void**, Signature, Boolean) at System.Reflection.MethodInvoker.Invoke(Object, IntPtr*, BindingFlags) --- End of inner exception stack trace --- at System.Reflection.MethodInvoker.Invoke(Object, IntPtr*, BindingFlags) at System.Reflection.RuntimeMethodInfo.Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.CommandLine.NamingConventionBinder.ModelBindingCommandHandler.InvokeAsync(InvocationContext) in C:\Users\User\source\repos\command-line-api\src\System.CommandLine.NamingConventionBinder\ModelBindingCommandHandler.cs:line 87 at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine\Invocation\InvocationPipeline.cs:line 80 --- End of stack trace from previous location --- at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseParseErrorReporting>b__0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine\Builder\CommandLineBuilderExtensions.cs:line 532 --- End of stack trace from previous location --- at System.CommandLine.Hosting.HostingExtensions.<>c__DisplayClass1_0.<<UseHost>b__0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine.Hosting\HostingExtensions.cs:line 49 --- End of stack trace from previous location --- at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseHelp>b__0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine\Builder\CommandLineBuilderExtensions.cs:line 424 --- End of stack trace from previous location --- at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass21_0.<<UseVersionOption>b__0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine\Builder\CommandLineBuilderExtensions.cs:line 661 --- End of stack trace from previous location --- at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass18_0.<<UseTypoCorrections>b__0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine\Builder\CommandLineBuilderExtensions.cs:line 593 --- End of stack trace from previous location --- at System.CommandLine.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__17_0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine\Builder\CommandLineBuilderExtensions.cs:line 567 --- End of stack trace from previous location --- at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass15_0.<<UseParseDirective>b__0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine\Builder\CommandLineBuilderExtensions.cs:line 507 --- End of stack trace from previous location --- at System.CommandLine.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__4_0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine\Builder\CommandLineBuilderExtensions.cs:line 238 --- End of stack trace from previous location --- at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass7_0.<<UseExceptionHandler>b__0>d.MoveNext() in C:\Users\User\source\repos\command-line-api\src\System.CommandLine\Builder\CommandLineBuilderExtensions.cs:line 328

@elgonzo
Copy link
Contributor

elgonzo commented Nov 24, 2022

Trimming is currently not possible -at least not with default trim settings- with the System.CommandLine NamingConventionBinder package, as per comment from System.CommandLine author and maintainer here: #1750 (comment).

You might try configuring the trimming in such a way that it accounts for the types and members accessed through reflection by the NamingConventionBinder (https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-7-0#root-descriptors),

@viordash
Copy link
Author

Thanks for your explanation!

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

No branches or pull requests

2 participants