Skip to content

(NET 9) MapControllers() don't work with option PublishTrimmed = true #58926

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
1 task done
dayvejones opened this issue Nov 13, 2024 · 15 comments
Closed
1 task done

(NET 9) MapControllers() don't work with option PublishTrimmed = true #58926

dayvejones opened this issue Nov 13, 2024 · 15 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@dayvejones
Copy link

dayvejones commented Nov 13, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Hello!

I have a problem with migration from NET 8 to 9.

Image

System.NotSupportedException: 'IsConvertibleType is not initialized when Microsoft.AspNetCore.Mvc.ApiExplorer.IsEnhancedModelMetadataSupported is false.'

This code work well on NET 8, but on NET 9 dont work .

Is this a breaking change or a bug?

Steps To Reproduce

Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

var app = builder.Build();

app.UseRouting();

app.MapControllers();

app.Run();

HomeController.cs

using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("api/v1/home")]
    public class HomeController : ControllerBase
    {
        [HttpGet("hello")]
        public IActionResult Index(string text)
        {
            return Ok(text);
        }
    }
}

WebApplication1.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <PublishSelfContained>true</PublishSelfContained>
    <PublishTrimmed>true</PublishTrimmed>
  </PropertyGroup>https://github.com/dotnet/aspnetcore/issues/58926#top

</Project>

If I don't use the option PublishTrimmed, then everything is fine. But I need it for compact deployment with SelfContained.

.NET Version

9

@ghost ghost added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Nov 13, 2024
@dayvejones dayvejones changed the title (NET 9) MapControllers() don't work with option PublishSelfContained = true (NET 9) MapControllers() don't work with option PublishTrimmed = true Nov 13, 2024
@martincostello
Copy link
Member

MVC doesn't support trimming: #41767 #27384 (comment)

If it worked in previous releases for your application, that was just a happy accident.

@martincostello martincostello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates and removed old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Nov 13, 2024
@dayvejones
Copy link
Author

dayvejones commented Nov 13, 2024

MVC doesn't support trimming: #41767 #27384 (comment)

If it worked in previous releases for your application, that was just a happy accident.

Yes

If I change it to

<TargetFramework>net8.0</TargetFramework>

Then everything will work. And on NET 8 it greatly reduced the build size. I'm sad to lose that feature in the new 9 version.

@BrennanConroy BrennanConroy added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Nov 13, 2024
sboulema added a commit to sboulema/StreetBook that referenced this issue Nov 18, 2024
@richard-collette-precisely

Wow, 1 day of no activity and issue closure. Better not file issues on Friday.

@jjxtra
Copy link

jjxtra commented Dec 5, 2024

I assume the MVC assemblies are not marked as supporting trimming? In which case this project property should still work:

<TrimMode>partial</TrimMode>

And why was this closed after a day? This smells like a regression, as I can reproduce the problem with the above project property, which should disable trimming on any assembly not explicitly opting in to trimming.

@martincostello
Copy link
Member

AFAIA, it working before was a happy accident. It wasn't, and still isn't, a supported scenario.

@jjxtra
Copy link

jjxtra commented Dec 5, 2024

The bigger concern goes beyond MVC trimming support: this appears as a defect with the partial trim mode project property. If MVC assemblies can break when trimming is enabled, even though they haven't opted into trimming, who knows what other assemblies might have regressed, when they too, have also not opted into trimming.

I'm disturbed this isn't being taken more seriously.

@azzlack
Copy link

azzlack commented Dec 5, 2024

Using Microsoft.AspNetCore.Mvc.ControllerBase for API controllers have been trimmable for at least two years (that is how long i have been using ut). This no longer works with .NET 9.

I believe this ticket should be reopened, this is a regression, not a "happy accident"

@dayvejones
Copy link
Author

Using Microsoft.AspNetCore.Mvc.ControllerBase for API controllers have been trimmable for at least two years (that is how long i have been using ut). This no longer works with .NET 9.

I believe this ticket should be reopened, this is a regression, not a "happy accident"

I also think this is a bug. The code from the first message works great in the previous versions of the framework (net 7,8). Or this problem should be announced in the "breaking change net 9" section.

@martincostello @BrennanConroy

@dayvejones
Copy link
Author

dayvejones commented Dec 13, 2024

I used this "happy accident" for several years.

Previously, I created Docker images with net 8 application with controllers (PublishTrimmed = true, TrimMode = partial, SelfContained = true). I made images on pure Alpine without NET runtime. The images size was about 28 megabytes.

Now the trimming does not work. I have to not use the SelfContained option. Now I will disgrace in front of our devops engineers with an image of 58 megabytes due to upgrade on NET 9. 😅

It was cool feature 😔

@jjxtra
Copy link

jjxtra commented Dec 13, 2024

@davidfowl I'd love to hear your thoughts on this and whether this "happy accident" is a regression, and whether the partial trim mode option may have a bigger bug blast radius than asp.net itself.

@davidfowl
Copy link
Member

Partial trim mode is definitely a happy accident. AFAIK we don't even default to partial trim mode anymore, but left it in for those that happened to be using it successfully.

@jjxtra
Copy link

jjxtra commented Dec 15, 2024

If this is an intentional breaking change, it ought to be called out in the breaking changes of the .NET 9.0 release notes, otherwise people will assume it was an unintentional regression.

I am still concerned about the overall state of partial trim mode support beyond ASP.NET--are we 100% sure partial trim mode works as expected beyond ASP.NET? Is partial trim mode going to be supported in .NET 10?

https://learn.microsoft.com/en-us/dotnet/core/compatibility/9.0#aspnet-core

@dayvejones
Copy link
Author

dayvejones commented Dec 15, 2024

Partial trim mode is definitely a happy accident. AFAIK we don't even default to partial trim mode anymore, but left it in for those that happened to be using it successfully.

@davidfowl Hey, David! If partial trimming worked in the previous version(net 7-8), but does not work in the new one(net 9), then this is a breaking changes. Do you disagree?

@davidfowl
Copy link
Member

@jjxtra
Copy link

jjxtra commented Dec 16, 2024

So if you use trim mode full in an asp.net project it should work fine? It's just partial that's busted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates ✔️ 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

7 participants