VS2017本地构建正常,MSBuild/TFS构建失败:程序集加载异常
Hey Ben, let’s dig into this frustrating build issue—local VS2017 builds work perfectly, but TFS is throwing that assembly load error? Chances are it’s related to how the build server handles NuGet caching, package references, or restore steps. Here are the most common fixes to try out:
Clear the TFS build agent’s NuGet cache
Build agents often hang onto old cached packages, which can cause version mismatches. Head to the agent’s NuGet cache directory (usuallyC:\Users\<AgentServiceAccount>\.nuget\packages), delete thePacsgearLibfolder if it exists, then re-run the build. Alternatively, add a NuGet restore step with the--forceflag to overwrite cached packages:nuget restore YourSolution.sln --forceDouble-check csproj reference consistency
Even though you updated your csproj files, verify every project referencingPacsgearLibhas the exact 2.5.4.0 version specified. Look for entries like this and confirm the version number and HintPath match your new package:<Reference Include="PacsgearLib, Version=2.5.4.0, Culture=neutral, PublicKeyToken=05b30ac9ab9dbb8e, processorArchitecture=MSIL"> <HintPath>..\packages\PacsgearLib.2.5.4.0\lib\net46\PacsgearLib.dll</HintPath> </Reference>Validate the TFS build definition’s restore step
Make sure your build definition includes a NuGet restore step that runs before the compile step. Sometimes the build tries to compile before pulling down the new packages. Also, confirm the restore step points to the correct NuGet feed where your 2.5.4.0 package is hosted.Check assembly binding redirects
If your solution uses binding redirects inweb.configorapp.config, ensure there’s no rule forcing an older version ofPacsgearLib. Update the redirect to target your new version like this:<dependentAssembly> <assemblyIdentity name="PacsgearLib" publicKeyToken="05b30ac9ab9dbb8e" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.5.3.0" newVersion="2.5.4.0" /> </dependentAssembly>Confirm the build agent’s .NET framework version
Ensure the TFS build agent has the same .NET framework version installed as your local VS2017 setup. If your project targets .NET 4.6, for example, verify the agent has that framework and the build step is configured to use the correct target.Verify the NuGet package is properly published
It’s possible your new 2.5.4.0 package wasn’t pushed correctly to the feed the TFS agent uses. Double-check the feed to confirm the package exists, has the right version number, and matches the public key token in the error message.
内容的提问来源于stack exchange,提问作者Ben_G

