为什么我在DotNet依赖地狱,我该怎么办才能离开?

时间:2017-09-05 18:49:31

标签: c# json nuget assemblies dependency-management

我有一个带有MVC和WebAPI的DotNet 4.6.1应用程序。 MVC端具有GlobalConfiguration.Configuration,WebAPI依赖于Assembly1

显然System.Web.Http中的GlobalConfiguration.Configuration依赖于" Newtonsoft.Json,Version = 6.0.0.0",而Assembly1依赖于" Newtonsoft.Json&#34 ;,版本= 7.0.1。

我精确地放置了这些引文,因为它们是精确的依赖关系: 当我尝试对我的WebApi运行ping时,我得到:

"Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies"

我的web.config由NuGet添加并由Microsoft创建,所以直到现在我还没有触及它,它是为我而构建的。 web.config的结构是:

<!--Personal Comment: See how configuration has no namespace-->
<configuration>
    <runtime>
        <!--Personal Comment: See how assemblyBinding DOES have namespace-->
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <!--Left out all of the other dependentAssemblies for brevity-->
            <dependentAssembly>
                <!--Personal Comment: Take note the upper/lowercase of attributes-->
                <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral">
                    <bindingRedirect oldVersion="0.0.0.0-7.0.0.0 newVersion="7.0.0.0" />
                </assemblyIdentity>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

奇怪的是,如果我转到VSCode中的.csproj并将引用更改为Include =&#34; Newtonsoft.Json&#34;并删除版本= 7.0.0.0等...然后没有任何问题得到解决。但是,如果我将Version = 7.0.0.0更改为Version = 6.0.0.0并将hintpath指向7.0.1,那么我的解决方案就可以了!

这似乎是一种生活和编程的糟糕方式,除非我必须处理它,否则我不喜欢它。根据我在网上阅读的每篇文章,每个问题,每个答案,他们都说&#34;使用bindingRedirect&#34;他们以我尝试的方式使用它。我的假设是bindingRedirect不能用于我的代码,我需要知道为什么,或者我可以在我的引用中以某种方式引用Newtonsoft.Json两次,并告诉编译器如果它不是第三个,则使用7.0.0.0代码 - 派对项目?

2 个答案:

答案 0 :(得分:1)

我前段时间遇到过类似的问题。试试这个:

  • 删除任何使用它的项目中的Json依赖项。
  • 关闭解决方案。
  • 删除SUO文件。它与您的解决方案的名称相同,扩展名为.SUO。
  • 重新启动解决方案。
  • 清洁解决方案。
  • 将所需的Json依赖项添加到需要它的项目中。
  • 重建解决方案。
  • 运行以验证。

原因是Visual Studio出现了&#34;缓存&#34; SUO文件中的引用。

如果它仍然行为不当,请将编译器设置为详细和重建。它应该告诉你输出中不匹配的确切来源。

答案 1 :(得分:0)

如果使用NuGet管理所有依赖项,则可以打开Manage NuGet Packages for Solution并将每个项目升级到最新版本的程序包(在本例中为NewtonSoft.JSON)。一旦不再引用旧的软件包版本,它将被卸载。