如何解决Azure Pipeline中vstest运行.NET 4.8测试的绑定重定向问题
解决方案
- 首先显式安装指定版本的依赖包
直接在测试项目中手动安装System.Runtime.CompilerServices.Unsafe5.0.0版本的NuGet包,强制统一依赖版本,避免间接依赖的版本冲突。 - 确认配置文件生成正常
检查MSBuild构建后的输出目录下,是否存在和测试程序集同名的.config文件(即_TestAuto.dll.config),确认文件内已包含你添加的绑定重定向规则。如果文件不存在或规则缺失,在测试项目csproj的PropertyGroup节点中添加如下配置,强制生成绑定重定向配置:<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> <BindingRedirectsOutputType>true</BindingRedirectsOutputType> - 通过.runsettings文件配置测试运行时规则
新建.runsettings文件添加到测试项目根目录,设置文件的复制属性为「如果较新则复制」,文件内容如下:
修改VSTest任务配置,指定运行时使用该配置文件:<?xml version="1.0" encoding="utf-8"?> <RunSettings> <RunConfiguration> <DisableAppDomain>false</DisableAppDomain> </RunConfiguration> <MSTest> <AssemblyResolution> <Runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> </dependentAssembly> </assemblyBinding> </Runtime> </AssemblyResolution> </MSTest> </RunSettings>steps: - task: VSTest@2 displayName: 'VsTest - testAssemblies' inputs: testAssemblyVer2: '**\*TestAuto*.dll' searchFolder: '$(test_temp)' vsTestVersion: toolsInstaller rerunFailedTests: true runSettingsFile: '$(test_temp)\你自定义的文件名.runsettings' - 排查GAC冲突
在本地代理上执行命令gacutil /l System.Runtime.CompilerServices.Unsafe,检查全局程序集缓存中是否存在该程序集的其他版本,如果存在则卸载,避免GAC版本优先级高于本地输出目录版本导致重定向失效。
内容的提问来源于stack exchange,提问作者Frédéric
相关产品推荐
相关产品推荐

