You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级到.NET 6后Web项目运行时报错找不到System.Security.Cryptography.Pkcs编译库位置

.NET 5 升级 .NET 6 缺失 System.Security.Cryptography.Pkcs 编译库异常排查方案

异常信息:
InvalidOperationException: Cannot find compilation library location for package 'System.Security.Cryptography.Pkcs'

  • 修正项目文件核心配置
    打开项目根目录下的.csproj文件,在顶层<PropertyGroup>节点中添加如下配置,确保运行时可以找到编译依赖上下文:
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    
  • 清理残留缓存并重新还原依赖
    关闭IDE后删除项目目录下的bin、obj文件夹,执行命令清除全局Nuget缓存后重新还原依赖:
    dotnet nuget locals all --clear
    dotnet restore
  • 修正System.Security.Cryptography.Pkcs包引用
    卸载你手动安装的System.Security.Cryptography.Pkcs Nuget包,.NET 6框架已内置该组件的兼容版本,手动安装跨版本包会触发依赖冲突。若业务确实需要单独引用,必须选择6.0.x版本分支的包。
  • 适配Razor运行时编译配置
    若项目开启了Razor运行时编译,确认Program.cs中的配置符合.NET 6规范:
    builder.Services.AddControllersWithViews()
        .AddRazorRuntimeCompilation();
    
  • 核对IIS托管配置
    确认站点对应应用程序池的.NET CLR版本设置为「无托管代码」,web.config中已正确配置.NET 6对应的托管处理程序:
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
    </handlers>
    

内容的提问来源于stack exchange,提问作者Cryptc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 17:45:03