.net8项目运行在IIS中报错 Unhandled exception. System.NotSupportedException: Collection was of a fixed size.at System.SZArrayHelper.Add[T](T _)

现象

准备将一个.net8项目放到IIS中运行,给服务器装好 host bundle dotnet-hosting-8.0.19-win.exe 以后,程序启动后报错,stdout输出如下错误信息:

Unhandled exception. System.NotSupportedException: Collection was of a fixed size.
   at System.SZArrayHelper.Add[T](T _)
   at NucTplImagelib.WebApi.Program.Main(String[] args)

查找部分文档也没找到错误原因,仔细查看代码发现代码中和Add有关的地方指定了启动后的端口号:

app.Urls.Add("http://*:8081");

注释改代码即可解决问题

解决办法

//app.Urls.Add("http://*:8081");

把该配置放到配置文件中,这样就不会影响在IIS环境运行,其它环境可以按该端口配置运行

//appsettting.json
"Urls":"http://*:8081"
posted @ 2025-09-08 15:54  呱呱蛙ꪝ·  阅读(17)  评论(0)    收藏  举报