在更新Nuget之后,我已经为DLL的地狱而奋斗了一个多星期。我设法通过手动编辑解决方案,项目和包文件解决了大多数问题。但是,无论我做什么VS都坚持认为dll版本与Nuget安装的版本不同。有问题的库是MailKit,在更新之前可以正常使用。现在,已安装的版本是2.4.1,但是尽管显示了正确的位置,属性网格仍坚持认为版本2.4.0是:
我尝试了以下所有方法,但都没有成功:
无论我做什么,它仍然显示错误的版本,因此,当应用程序运行时,由于正确的版本是本地复制的,因此会引发异常。
谁能建议我如何解决这个问题?
编辑:
软件包配置:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BouncyCastle" version="1.8.5" targetFramework="net452" />
<package id="JetBrains.Annotations" version="2019.1.3" targetFramework="net452" />
<package id="MailKit" version="2.4.1" targetFramework="net452" />
<package id="MimeKit" version="2.4.1" targetFramework="net452" />
</packages>
项目文件(仅引用MailKit和MimeKit,其余部分均删除为不必要,让我知道是否需要更多内容):
<Reference Include="MailKit, Version=2.4.1, Culture=neutral, PublicKeyToken=4e064fe7c44a8f1b, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\MailKit.2.4.1\lib\net45\MailKit.dll</HintPath>
</Reference>
<Reference Include="MethodExtensions, Version=1.0.4469.11621, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Libraries\MethodExtensions\bin\Debug\MethodExtensions.dll</HintPath>
</Reference>
<Reference Include="MimeKit, Version=2.4.1, Culture=neutral, PublicKeyToken=bede1c8a46c66814, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\MimeKit.2.4.1\lib\net45\MimeKit.dll</HintPath>
</Reference>
进一步编辑:
为了澄清起见,该软件包安装的dll版本显然是2.4.1:
Here is the runtime error message confirming that it is looking for 2.4.0 which is no longer on the computer following the Nuget update:
The Solution
The answer from @zivkan pointed me in the right direction. Firstly I was looking for entirely the wrong thing, assuming that the version in the Property Grid was incorrect when in fact it was perfectly fine. The reference is located in a plugin module which itself compiles to a library, which means that when compiling the MailKit dll not only has to be copied local but then copied to the main application folder using a post build event command line. It turns out that there was an error in the post build event code so that MailKit was not copied and MimeKit was copied twice. Not taking the blame for that one, but I do take the blame for not recognising the problem!
Thanks, @zirkan, for your help and explanation which finally got me to the resolution...
when the app runs it throws an exception since the correct version is copied local.
Do you mean incorrect? I don't see why an exception will be thrown if the correct version is copied. In any case, it would have been more helpful if you showed us the actual error you got. I'm going to spend the rest of this answer explaining why everything else is working as designed, meaning you haven't given us enough information to understand your problem, so it's hard to suggest a good solution.
Anyway, a NuGet package is just a zip file (renamed .nupkg
) that contains some files, usually some .NET assemblies. So, giving away the fundemental issue, what happens if the different assemblies in the package have different versions? The answer is nothing because assembly version and package version are independent. Often they're similar, or (almost) exactly the same, but since they're independent there's no reason they can't be different.
An assembly also has multiple versions. Firstly, if you find the .dll
in Windows explorer, right click, select properties and go to the details tab, you'll see that all (or most) dlls have a product version and a file version. This is not specific to .NET, all windows executables have this metadata. If you use ILSpy or something similar to inspect the .NET assembly, you'll see there are AssemblyFileVersion, AssemblyInformationalVersion and AssemblyVersion attributes. So all together there are at least 5 different version metadata, and they're all independent and therefore can be different. That's before we consider NuGet package version is a 6th independent version number.
Now, the assembly in the properties window doesn't know anything about NuGet. It's showing you the path to the file, and since NuGet extracts packages to a path that includes the package version, we can see it in the path. But the version shows in the properties window is the assembly version. So, in this case the MailKit v2.4.1 package contains MailKit.dll with assembly version 2.4.0.0. Visual Studio's property window is showing you the correct information, there is no problem.
我的回答已经足够长了,所以我不会赘述很多,但是软件包作者可能会选择在其软件包的多个版本中使用相同的程序集版本,以最大程度地减少需要绑定重定向的次数。但是,这仅在不同产品版本兼容时才有效。当它们不兼容并且两个不同的程序集都依赖于相同的程序集版本的依赖关系时,就不可能同时加载两个不同的版本以解决此问题(是的,当同一个程序集具有不同的程序集版本时,则可以同时加载两者,这需要额外的精力)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句