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

Web API broken in .NET 7.0 after trimming #44997

Closed
1 task done
MarkusRodler opened this issue Nov 10, 2022 · 6 comments
Closed
1 task done

Web API broken in .NET 7.0 after trimming #44997

MarkusRodler opened this issue Nov 10, 2022 · 6 comments
Labels
area-web-frameworks ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@MarkusRodler
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

After upgrading multiple working projects from .NET 6.0 to .NET 7.0, all .NET Web Apis are broken that I tried.
I am using trimming and r2r.

Expected Behavior

No response

Steps To Reproduce

  1. Create a new blank web api: dotnet new webapi --use-minimal-apis -n Net7trimmingbug --no-https --no-openapi
  2. Add one simple route that calls a method to Program.cs:
app.MapGet("{message}", (string message) => System.Console.WriteLine(message));
  1. Use that csproj config with trimming and r2r enabled:
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishReadyToRunComposite>true</PublishReadyToRunComposite>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>copyused</TrimMode>
  </PropertyGroup>

</Project>
  1. Publish and run the project and afterwards call that route:
dotnet publish --configuration Debug --output app/publish --runtime linux-x64 --self-contained true

./app/publish/Net7trimmingbug 

Exceptions (if any)

fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMM30E2TLGA4", Request id "0HMM30E2TLGA4:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: Encountered a parameter of type 'System.String' without a name. Parameters must have a name.
at Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArgument(ParameterInfo parameter, RequestDelegateFactoryContext factoryContext)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArguments(ParameterInfo[] parameters, RequestDelegateFactoryContext factoryContext)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArgumentsAndInferMetadata(MethodInfo methodInfo, RequestDelegateFactoryContext factoryContext)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.InferMetadata(MethodInfo methodInfo, RequestDelegateFactoryOptions options)
at Microsoft.AspNetCore.Routing.RouteEndpointDataSource.CreateRouteEndpointBuilder(RouteEntry entry, RoutePattern groupPrefix, IReadOnlyList1 groupConventions, IReadOnlyList1 groupFinallyConventions)
at Microsoft.AspNetCore.Routing.RouteEndpointDataSource.get_Endpoints()
at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.CreateEndpointsUnsynchronized()
at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.EnsureEndpointsInitialized()
at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.get_Endpoints()
at Microsoft.AspNetCore.Routing.DataSourceDependentCache1.Initialize() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory)
at Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher..ctor(EndpointDataSource dataSource, Lifetime lifetime, Func1 matcherBuilderFactory) at Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource dataSource) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatcher|8_0(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task1 matcherTask)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

.NET Version

7.0.100

Anything else?

No response

@davidfowl
Copy link
Member

Trimming isn't really supported for ASP.NET Core apps, it's best effort (if it works it works) until .NET 8. If you're looking for a workaround, it might be possible to help here.

If you remove <TrimMode>copyused</TrimMode> does it work?

@MarkusRodler
Copy link
Contributor Author

Oh, this is a surprising info, because I use trimming successfully in several projects ASP.NET core projects with both .NET 6 and .NET 5.
Regarding <TrimMode>copyused</TrimMode>: If I remove it, then it works again. I didn't noticed, that copyused and link aren't supported anymore? But enabling them is accepted but breaks the application? I'm a little bit confused about that behaviour.

@davidfowl
Copy link
Member

Oh, this is a surprising info, because I use trimming successfully in several projects ASP.NET core projects with both .NET 6 and .NET 5.

But you got warnings and ignored them right?

cc @sbomer about the trimmer changes in 7.0

@MarkusRodler
Copy link
Contributor Author

MarkusRodler commented Nov 11, 2022

Yes.

Fyi: Removing the above setting works in the minimal example above but unfortunately not in the production projects because they are trimmed to much afterwards.

@sbomer
Copy link
Member

sbomer commented Nov 11, 2022

@MarkusRodler see this issue: dotnet/linker#3039. As a workaround, you might be able to use <_TrimmerDefaultAction>copy</_TrimmerDefaultAction>.

@MarkusRodler
Copy link
Contributor Author

@sbomer Yes that works like before. Thanks.

@rafikiassumani-msft rafikiassumani-msft added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Nov 16, 2022
@ghost ghost added the Status: Resolved label Nov 16, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Dec 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-web-frameworks ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

5 participants