Assembly Loading Version Mismatch: Why is it loading?

£可爱£侵袭症+ 提交于 2019-11-30 10:17:51

Why did it work?

Because you have specified so ;)

How to make it NOT work?

  1. Rightclick on the DLL in Solution Explorer
  2. Select Properties
  3. Select Use specific version

BONUS QUESTION: What happens during the build process? Isn't the version of external dependencies hard coded to the main dependency?

Not unless you specify so. It's off per default. Well designed assemblies should be backwards compatible and the version shouldn't really matter.

I've faced the same thing and tried to look into the msbuild logs:

msbuild /v:detailed /t:build

The following lines looked interesting:

Unified Dependency "Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". Using this version instead of original version "7.0.0.0" in "C:\src\BindingTest\Lib1\bin\Debug\Lib1.dll" because AutoUnify is 'true'.

Additionally, If you check out resulting app.config file after the build, you would probably see automatic binding redirect there that your app.config did not initially have.

So the behavior we observe has to do with "automatic assembly unification" and "automatic binding redirection" msbuild processes.

Here is what documentation says about AutoUnify parameter:

When true, the resulting dependency graph is automatically treated as if there were anApp.Config file passed in to the AppConfigFile parameter. This virtual App.Config file has a bindingRedirect entry for each conflicting set of assemblies such that the highest version assembly is chosen. A consequence of this is that there will never be a warning about conflicting assemblies because every conflict will have been resolved.

When true, each distinct remapping will result in a high priority comment showing the old and new versions and that AutoUnify was true.

Finaly, if you want to observe the "failure" you can call msbuild with following arguments:

msbuild /v:d /t:build /p:AutoUnifyAssemblyReferences=false;AutoGenerateBindingRedirects=false

The runtime distinguishes between regular and strong-named assemblies for the purposes of versioning. Version checking only occurs with strong-named assemblies.

https://docs.microsoft.com/en-us/dotnet/framework/app-domains/assembly-versioning

Therefor, the answers are as follows

  1. Because the assembly isn't strong-named
  2. Make the assembly strong-named
  3. Yes, the versions of referenced assemblies are hard-coded. You can verify this by using a decompiler (e.g. ILSpy) to read this information
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!