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

MVC apps no longer work with PublishTrimmed #49360

Closed
1 task done
eerhardt opened this issue Jul 12, 2023 · 3 comments · Fixed by #49393
Closed
1 task done

MVC apps no longer work with PublishTrimmed #49360

eerhardt opened this issue Jul 12, 2023 · 3 comments · Fixed by #49393
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@eerhardt
Copy link
Member

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Using an MVC app with PublishTrimmed no longer works. The same app worked in .NET 7.

This appears to be caused by the JsonSerializerIsReflectionEnabledByDefault changes. See dotnet/runtime#84378.

FYI @eiriktsarpalis

Expected Behavior

The app should work.

Steps To Reproduce

  1. dotnet new mvc
  2. Add <PublishTrimmed>true</PublishTrimmed> to the csproj
  3. dotnet run

Exceptions (if any)

Unhandled exception. System.InvalidOperationException: JsonSerializerOptions instance must specify a TypeInfoResolver setting before being marked as read-only.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_JsonSerializerOptionsNoTypeInfoResolverSpecified()
   at System.Text.Json.JsonSerializerOptions.MakeReadOnly()
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter..ctor(JsonSerializerOptions jsonSerializerOptions)
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.CreateFormatter(JsonOptions jsonOptions)
   at Microsoft.AspNetCore.Mvc.MvcCoreMvcOptionsSetup.Configure(MvcOptions options)
   at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
   at Microsoft.Extensions.Options.UnnamedOptionsManager`1.get_Value()
   at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.<>c.<AddMvcCoreServices>b__5_0(IServiceProvider s)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)

.NET Version

8.0.100-preview.7.23360.2

Anything else?

No response

@eerhardt eerhardt added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jul 12, 2023
@DamianEdwards
Copy link
Member

The goal is to ensure this still works for the legacy <TrimMode>Partial</TrimMode> so if we need to update the Web SDK to detect and set more switches to enable this then that's what we'll do.

@eerhardt
Copy link
Member Author

You get this error whether you set TrimMode to partial or full.

The issue is that when JsonSerializerIsReflectionEnabledByDefault=false (which is the default for all trimmed apps), the SystemTextJsonOutputFormatter MVC code isn't handling it successfully. Even if I don't try to use JSON at all in my app (for example, the template app just returns HTML from the View), I still get this startup error.

One way to fix this is before we call MakeReadOnly on the JsonSerializerOptions, check if the TypeInfoResolver is still null. If it is, set it to an empty resolver that does nothing.

@captainsafia
Copy link
Member

This also affects the dotnet new webapi -minimal template.

One way to fix this is before we call MakeReadOnly on the JsonSerializerOptions, check if the TypeInfoResolver is still null. If it is, set it to an empty resolver that does nothing.

I've taken a stab at this approach in #49393. @eiriktsarpalis What are your thoughts on how we should approach this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants