在Windows 11上将blazor服务器应用程序发布到iis时,“无法将PhysicalFileProvider类型的对象强制转换为CompositeFileProvider类型”

jogvjijk  于 2023-08-05  发布在  Windows
关注(0)|答案(1)|浏览(107)

我在事件查看器中遇到此错误时发布我的blazor服务器应用程序到iis在windows 11:
System.InvalidCastException:无法将类型“Microsoft.Extensions.FileProviders.PhysicalFileProvider”的对象强制转换为类型“Microsoft.Extensions.FileProviders.CompositeFileProvider”
而在我的浏览器面临这个错误:
HTTP错误500.30 - ASP.NET Core应用程序无法启动。
它在我的Visual Studio中工作,但我不知道在发布的版本中有什么问题!!
事件查看器中的详细信息为:
适用范围:w3wp.exe CoreCLR版本:5.0.523.13516.NET版本:7.0.5说明:由于未处理的异常,进程终止。异常信息:System.InvalidCastException:无法将类型“Microsoft.Extensions.FileProviders.PhysicalFileProvider”的对象强制转换为类型“Microsoft.Extensions.FileProviders. CompositeFileProvider”。在SmartMIS.Shared.Extensions.ConfigureStaticModuleFiles(WebApplication app)in E:\Development\Projects\Backups\SmartMIS\14020509.3\SmartMIS\SmartMIS.Shared\Extensions.cs:line 32在Program.$(String[] args)in E:\Development\Projects\Backups\SmartMIS\14020509.3\SmartMIS\SmartMIS.Web\Program.cs:line 37
下面是导致问题的代码:

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;
        }

字符串
任何帮助将不胜感激。先谢了。

xvw2m8pv

xvw2m8pv1#

最后,我改变了我的静态方法如下:

public static WebApplication ConfigureStaticModuleFiles(this WebApplication app)
        {
            var moduleManagerService = app.Services.GetService<ModuleManager>();
            List<IFileProvider> originalFileProviders = new List<IFileProvider> { app.Environment.WebRootFileProvider };
            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;
        }

字符串
正如你所看到的,我使用了'IFileProvider'而不是compositeFileProvider的列表。问候..

相关问题