aspnetcore: (NET 9) MapControllers() don't work with option PublishTrimmed = true
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.
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
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 5
- Comments: 15 (4 by maintainers)
Commits related to this issue
- Disable trimming https://github.com/dotnet/aspnetcore/issues/58926 — committed to sboulema/StreetBook by sboulema 2 years ago
MVC doesn’t support trimming: #41767 https://github.com/dotnet/aspnetcore/issues/27384#issuecomment-2455225137
If it worked in previous releases for your application, that was just a happy accident.
Using
Microsoft.AspNetCore.Mvc.ControllerBasefor 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”
AFAIA, it working before was a happy accident. It wasn’t, and still isn’t, a supported scenario.
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.
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 😔
The bigger concern goes beyond MVC trimming support: this appears as a defect with the
partialtrim 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.
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.
Wow, 1 day of no activity and issue closure. Better not file issues on Friday.
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
So if you use trim mode full in an asp.net project it should work fine? It’s just partial that’s busted?
@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?
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