.Net Standard NuGet包在VSIX项目运行时报无法加载依赖程序集错误
问题解决思路
核心原因是VSIX项目默认不会将PackageReference引入的间接依赖自动打包到最终生成的VSIX安装包中,同时.NET Framework项目的绑定重定向规则在VSIX运行场景下存在特殊要求,以下是可落地的解决步骤:
- 第一步:修改VSIX项目的引用属性
找到VSIX项目中对Newtonsoft.Json(或System.Text.Json)的引用,右键打开属性面板,将两个属性设置为True:- 复制本地
- 包含在VSIX中
如果是间接依赖找不到引用条目,可以直接在VSIX项目的.csproj文件中添加如下配置:
切换为System.Text.Json的话修改对应的包名和版本即可。<ItemGroup> <PackageReference Include="Newtonsoft.Json" Version="13.0.1"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> <IncludeInVSIX>true</IncludeInVSIX> </PackageReference> </ItemGroup> - 第二步:强制开启绑定重定向并校验配置
打开VSIX项目的.csproj文件,在第一个<PropertyGroup>节点中添加两个配置项:
同时检查项目根目录的<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>app.config文件,确认存在对应程序集的绑定重定向规则,示例如下:<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-13.0.1.0" newVersion="13.0.1.0" /> </dependentAssembly> <!-- 切换为System.Text.Json时添加对应规则 --> <dependentAssembly> <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-对应引用版本号" newVersion="对应引用版本号" /> </dependentAssembly> </assemblyBinding> </runtime> - 第三步:校验VSIX打包内容
编译完成后,找到输出目录中的.vsix文件,将后缀修改为.zip解压后检查根目录是否存在对应的Newtonsoft.Json.dll/System.Text.Json.dll,如果不存在则回到第一步检查配置是否正确。 - 第四步:排查NuGet包的依赖声明
检查你开发的.NET Standard 2.0 NuGet包的.nuspec配置,确认依赖项没有被标记为developmentDependency,否则安装时不会同步识别传递依赖。
内容的提问来源于stack exchange,提问作者gandarez
相关产品推荐
相关产品推荐

