'Unable to cast object of type PhysicalFileProvider to type CompositeFileProvider' when publish blazor server app to iis on windows 11

Hamed Vaziri 136 Reputation points
2023-07-31T20:29:14.12+00:00

Hi all

I'm facing this error in event viewer when publish my blazor server app to iis in windows 11 :

System.InvalidCastException: Unable to cast object of type 'Microsoft.Extensions.FileProviders.PhysicalFileProvider' to type 'Microsoft.Extensions.FileProviders.CompositeFileProvider'

And in my browser facing this error :

HTTP Error 500.30 - ASP.NET Core app failed to start.

It works in my visual studio, but i don't know what is the problem in published version!!

The detail info in event viewer is :

Application: w3wp.exe CoreCLR Version: 7.0.523.17405 .NET Version: 7.0.5 Description: The process was terminated due to an unhandled exception. Exception Info: System.InvalidCastException: Unable to cast object of type 'Microsoft.Extensions.FileProviders.PhysicalFileProvider' to type 'Microsoft.Extensions.FileProviders.CompositeFileProvider'. at SmartMIS.Shared.Extensions.ConfigureStaticModuleFiles(WebApplication app) in E:\Development\Projects\Backups\SmartMIS\14020509.3\SmartMIS\SmartMIS.Shared\Extensions.cs:line 32 at Program.$(String[] args) in E:\Development\Projects\Backups\SmartMIS\14020509.3\SmartMIS\SmartMIS.Web\Program.cs:line 37

And here is my code which cause the problem :

public static WebApplication ConfigureStaticModuleFiles(this WebApplication app)
        {
            var moduleManagerService = app.Services.GetService<ModuleManager>();
            var originalFileProviders = ((CompositeFileProvider)app.Environment.WebRootFileProvider).FileProviders.ToList();
            StaticAssetInfo staticAssetInfo = new StaticAssetInfo();
            foreach (IModulePackage modulePackage in moduleManagerService.Packages)
            {
                Assembly assembly = modulePackage.GetType().Assembly;
                var newPathProvider = new ManifestEmbeddedFileProvider(assembly, "wwwroot");
                originalFileProviders.Add(newPathProvider);
            }

            app.Environment.WebRootFileProvider = new CompositeFileProvider(originalFileProviders.ToArray());

            return app;
        }

Any help would be great appreciated.

Thanks in advance.

Internet Information Services
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,020 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,322 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lex Li (Microsoft) 4,242 Reputation points Microsoft Employee
    2023-07-31T23:39:54.1466667+00:00

    By definition, app.Environment.WebRootFileProvider is just IFileProvider,

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.iwebhostenvironment.webrootfileprovider?view=aspnetcore-7.0

    So, you cannot assume it is CompositeFileProvider all the times. On IIS, it is PhysicalFileProvider based on your current configuration.

    It shouldn't be a total surprise, as when VS launches a web project, there are many differences,

    https://halfblood.pro/web-application-differences-in-visual-studio-and-iis-60fec7e311b3

    https://halfblood.pro/how-visual-studio-launches-iis-express-to-debug-asp-net-core-apps-d7fd3677e3c3

    0 comments No comments