C#项目从.NET 4.8升级至.NET 5/6后无法加载PresentationFramework报错
问题复现环境
- 开发工具:VS 2022 RC3
- SDK版本:
6.0.0-rc.2.21501.6 - 项目文件配置:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0-windows</TargetFramework> <Platform Condition=" '$(Platform)' == '' ">x86</Platform> <OutputType>WinExe</OutputType> <UseWPF>true</UseWPF> </PropertyGroup> </Project>
- 已正确引用
Microsoft.WindowsDesktop.App.WPF框架,版本为6.0.0-rc.2.21501.6
报错信息
构建项目时触发如下错误:
Error LC0000 'Could not load file or assembly 'PresentationFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)' filename filePathHere LC 1
报错原因
该问题为.NET 6 RC预览版的已知兼容问题,许可证编译器(LC.exe)处理WPF相关程序集时,尝试直接加载执行引用程序集,但引用程序集仅支持在仅反射加载上下文中加载,因此触发加载失败。
解决方案
- 临时适配方案1:在项目文件的
<PropertyGroup>节点中添加如下配置,禁用资源生成时对类型程序集的锁定,修改后重新加载项目构建即可:
<GenerateResourceNeverLockTypeAssemblies>true</GenerateResourceNeverLockTypeAssemblies>
- 临时适配方案2:检查项目中是否存在
.licx后缀的许可证文件,若存在可先将该文件从项目中排除,或清空文件内容后再构建。 - 根因解决:将VS2022升级到正式版,同时将.NET SDK升级到6.0正式版及以上版本,该问题在正式版中已完成修复。
内容的提问来源于stack exchange,提问作者Michael T
相关产品推荐
相关产品推荐

