csproj中AutoGenerateBindingRedirects未生成全部NuGet包绑定重定向
原因说明
SDK风格的.NET Framework类库项目中,自动绑定重定向逻辑默认仅会为依赖链中存在多版本冲突的程序集生成重定向规则。你已经生成的Microsoft.Bcl.AsyncInterfaces、Newtonsoft.Json等程序集的重定向,本质是因为你的其他依赖引用了这些程序集的不同版本触发了冲突检测。而Microsoft.SqlServer.Types等包没有触发重定向,是因为当前依赖树中这些包不存在版本冲突,所以自动生成逻辑没有处理。
解决方案
方法1:强制指定需生成重定向的程序集
在你.csproj的PropertyGroup节点中新增BindingRedirectsAdditionalAssemblies属性,明确指定要生成重定向的程序集名称,多个程序集用分号分隔:
<PropertyGroup> <!-- 原有配置保留 --> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> <!-- 新增配置 --> <BindingRedirectsAdditionalAssemblies>Microsoft.SqlServer.Types</BindingRedirectsAdditionalAssemblies> </PropertyGroup>
重新生成项目后就会自动生成对应程序集的绑定重定向规则。
方法2:手动配置合并规则
在项目根目录新增app.config文件,写入你需要的固定重定向规则,自动生成逻辑会将手动规则和自动生成的其他规则合并到最终的.dll.config中,不会互相覆盖:
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-14.0.1016.290" newVersion="14.0.1016.290" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
注意事项
如果该类库是被其他可执行项目(WinForm、WPF、Web应用等)引用,类库本身的.dll.config不会被运行时自动加载,你需要在最终的可执行项目中开启AutoGenerateBindingRedirects属性,上层项目会自动聚合所有依赖的重定向规则到自己的配置文件中。
内容的提问来源于stack exchange,提问作者bgh
相关产品推荐
相关产品推荐

