'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

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");

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

            return app;

Any help would be great appreciated.

Thanks in advance.

Internet Information Services
A set of technologies in the .NET Framework for building web applications and XML web services.
4,020 questions
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

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


    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,



    0 comments No comments