Search code examples
c#.net-coreasp.net-core-mvc.net-6.0azure-service-fabric

Migrating Service Fabric .NET Core 3.1 app to .NET 6 resulting in UI rendering 404


I've recently been trying to migrate an existing Service Fabric .NET Core 3.1 application that has a Web API project, MVC UI project, several class libraries and associated tests projects.

I've been following the Microsoft documentation Migrate from ASP.NET Core 3.1 to 6.0 and applied the "Update the target framework", "Update package references" steps to all the projects in this application as those are the only steps relevant to my application, fixed any compilation errors/warnings and it's now building successfully.

I've successfully managed to run the application and tested the Web API project via postman and can verify everything works fine i.e. I can hit the enpoints and get responses.

I've then tried to hit my local UI environment via https://localhost:30000/ but this renders an immediate 404 error

My Startup.cs and Program.cs files havent changed from .NET core 3.1 to .NET 6 within the affected UI project.

The default routing is configured.

app.UseEndpoints(endpoints =>
{
      endpoints.MapControllerRoute(
             name: "default",
             pattern: "{controller=Home}/{action=Index}/{id?}");
       endpoints.MapRazorPages();
})

But it's rendering 404.

enter image description here

I'm not quite sure why hitting the above URL doesnt load the home page given the default route is configured.

I might be completely wrong here but my assumption is on Service Fabric as I believe something has changed but the documenation for this is quite poor in my opinion.

Additional Information

.NET SDK's installed locally on my machine:

  • 3.1.423
  • 6.0.402

Service Fabric development kit installed locally:

  • 9.0.1107.9590 (Runtime version)
  • 6.0.1107.9590 (SDK version)

Any help would be much appreciated


Here is the UI project package references:

<ItemGroup>
        <PackageReference Include="Ardalis.GuardClauses" Version="4.0.1" />
        <PackageReference Include="AutoMapper" Version="12.0.0" />
        <PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
        <PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="6.0.10" />
        <PackageReference Include="Microsoft.AspNetCore.DataProtection.Extensions" Version="6.0.10" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.10" />
        <PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="6.0.10" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
        <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
        <PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.10" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10" />
        <PackageReference Include="Microsoft.Identity.Client" Version="4.47.2" />
        <PackageReference Include="Microsoft.Identity.Web" Version="1.25.5" />
        <PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="1.25.5" />
        <PackageReference Include="Microsoft.Identity.Web.UI" Version="1.25.5" />
        <PackageReference Include="Microsoft.ServiceFabric.AspNetCore.Kestrel" Version="6.0.1107" />
        <PackageReference Include="Microsoft.ServiceFabric.AspNetCore.Configuration" Version="6.0.1107" />
        <PackageReference Include="Microsoft.ServiceFabric.Services" Version="6.0.1107" />
        <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />
        <PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.175" />
    </ItemGroup>

Solution

  • When migrating from .NET Core 3.1 to .NET 6, there have been several changes and improvements, including changes in how MVC views and controllers are discovered and configured.

    In .NET 6, there have been enhancements to the application parts feature, which allows MVC to discover controllers and views from additional sources beyond the main application assembly.

    In previous versions of .NET Core (such as 3.1), MVC controllers and views were automatically discovered from the main application assembly by default. However, in .NET 6, there might be scenarios where controllers or views are located in separate assemblies or dynamically loaded assemblies, and MVC may not automatically discover them.

    Adding the assembly explicitly as an application part ensures that MVC knows to look for controllers and views within that assembly, enabling correct functionality even in scenarios where controllers and views are not located in the main application assembly.

    So, in the migration process from .NET Core 3.1 to .NET 6, explicitly adding the assembly as an application part ensures that MVC continues to work correctly by discovering controllers and views from the specified assembly. This ensures compatibility and functionality with the new features and enhancements introduced in .NET 6.

    Given the above, to solve my problem I added the following snippet of code to my UI startup file

    var assembly = typeof(HomeController).Assembly;
    services.AddControllersWithViews().AddNewtonsoftJson().PartManager.ApplicationParts.Add(new Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPart(assembly));
    

    After adding the above I was able to successfully hit and navigate to different pages in the UI