c# - Visual Studio : Can't load 2 versions of Json. NET 和使用别名

标签 c# visual-studio json.net

M-Files VAF 仅需要 json.net 版本 =10.0.3,而不是 >=10.0.3。这与 Microsoft.PowerBI.API 崩溃了,因为它在任何版本中都不支持这种确切的依赖关系。它从 7.0 -> 11.xx
我已经为此苦苦挣扎了一天。我一直在遵循本指南:
How to use two versions of the same DLL in the same project
我什至无法从 VS 添加第二个版本的 json.net。
然后我在 *.proj 文件和 app.config 中手动尝试,如下所示:

<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
  <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
  <Aliases>NewJson</Aliases>
</Reference>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
  <HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
  <Aliases>OldJson</Aliases>     
</Reference>
应用配置
<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <codeBase version="7.0.0.0" href="FILE:///../packages/Newtonsoft.Json.7.0.1/lib/net45/Newtonsoft.Json.dll" />
    <codeBase version="10.0.0.0" href="FILE:///../packages/Newtonsoft.Json.10.0.3/lib/net45/Newtonsoft.Json.dll" />       
</dependentAssembly>
这甚至有效吗?还是首先在 GAC 中查找?我可以在 gac 中安装版本 7 和 10 吗?
无论哪种方式,通过 ../packages 或通过本地文件夹引用旧的,都会在 VS 中给我黄色感叹号。 VS 说只要我点击运行:
System.IO.FileNotFoundException:无法加载文件或程序集“Newtonsoft.Json,版本=7.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其依赖项之一。该系统找不到指定的文件。
我确实添加了:
外部别名 OldJson;
并使用 OldJson::Newtonsoft.Json;
这适用于 NewJson 别名。
我看过这个:.NET: Load two version of the same DLL .我不想在运行时这样做,还是最好的方法?如果我稍后将其推送到 azure 服务,最好的方法是什么?
有人愿意分享有关这种困境的一些看法吗? 😊
更新 :我现在实际上在使用这个版本的 powerbi:Version 2.0.12 .不需要任何 json.net。但在加载 json.net 时仍然出错。这是 fuslog 的截图。
=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
(Fully-specified)
LOG: Appbase = file:///C:/Program Files/M-Files/20.8.9339.3/Bin/x64/
LOG: Initial PrivatePath = NULL
Calling assembly : Microsoft.PowerBI.Api, Version=2.0.12.18114, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Program Files\M-Files\20.8.9339.3\Bin\x64\mfappplatform.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///C:/Program Files/M-Files/20.8.9339.3/Bin/x64/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/M-Files/20.8.9339.3/Bin/x64/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/M-Files/20.8.9339.3/Bin/x64/Newtonsoft.Json.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/M-Files/20.8.9339.3/Bin/x64/Newtonsoft.Json/Newtonsoft.Json.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/M-Files/20.8.9339.3/Server/Data/Apps/{508AA001-0DF6-4396-A440-6985D9C46A7E}/{9DC357E6-D62A-4A53-A8D4-747B23826B2E}-hVIg9CF9YxhkXsvwapj2HCgrFD1Yi6XZhBsR7WU0Be0=/Newtonsoft.Json.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
LOG: Attempting download of new URL file:///C:/Program Files/M-Files/20.8.9339.3/Server/Data/Apps/{508AA001-0DF6-4396-A440-6985D9C46A7E}/{9DC357E6-D62A-4A53-A8D4-747B23826B2E}-hVIg9CF9YxhkXsvwapj2HCgrFD1Yi6XZhBsR7WU0Be0=/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/M-Files/20.8.9339.3/Server/Data/Apps/{508AA001-0DF6-4396-A440-6985D9C46A7E}/{9DC357E6-D62A-4A53-A8D4-747B23826B2E}-hVIg9CF9YxhkXsvwapj2HCgrFD1Yi6XZhBsR7WU0Be0=/Newtonsoft.Json.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/M-Files/20.8.9339.3/Server/Data/Apps/{508AA001-0DF6-4396-A440-6985D9C46A7E}/{9DC357E6-D62A-4A53-A8D4-747B23826B2E}-hVIg9CF9YxhkXsvwapj2HCgrFD1Yi6XZhBsR7WU0Be0=/Newtonsoft.Json/Newtonsoft.Json.EXE.
如果我使用 M-Files VAF 10.0.3 以外的任何其他版本,它甚至无法编译。
VS警告:
VS Warning
缺少 JSON.NET 7.0 依赖项的 Nuget 信息
Nuget Info
CodeReflect Microsoft.PowerBI.Api.2.0.14 引用。
PowerBI References

最佳答案

被称为“依赖 hell ”的问题:
enter image description here
可能的解决方案:

There are more ways of loading multiple versions of the same assembly into a single application domain. All of them require the assemblies to be strong-named to avoid identity conflicts.


  • Install Assembly to Global Assembly Cache - 一种简单的方法,仅推荐用于生产环境。我们根本不需要接触应用程序,因为它会自动在 GAC 中查找程序集。然而,它对于开发环境来说并不是很方便,因为必须在每台机器上重复安装。
  • Perform Custom Resolution via AppDomain.AssemblyResolve event - 最强大的选项,当加载逻辑更复杂或参数化时特别有用。
  • Specify code base - 通过编辑(网络)应用程序配置文件,我们可以轻松地将应用程序指向适当的 DLL 版本,即使它们不直接位于 bin 文件夹中。
    <configuration>
      <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
              <dependentAssembly>
                  <assemblyIdentity name="Some.API" publicKeyToken="12ab3cd4e5f6abcd"
                    culture="neutral" />
                  <codeBase version="1.0.0.0" href="v1\Some.API.dll" />
                  <codeBase version="2.0.0.0" href="v2\Some.API.dll" />
                  <!-- INFO: The version attribute represents an assembly 
                             version which doesn't always have to match the 
                             NuGet package version.
                             The codebase attribute can be anywhere on 
                             the local intranet or the Internet. -->
              </dependentAssembly>
          </assemblyBinding>
      </runtime>
    </configuration>
    

  • 注:您不应更改 CMSDependencies 文件夹的内容。这样做可能会破坏您的升级路径,因为该文件夹旨在专门包含 Kentico 的依赖项。
    有用的网址:
  • https://devnet.kentico.com/articles/referencing-multiple-versions-of-the-same-assembly-in-a-single-application
  • https://docs.microsoft.com/en-us/dotnet/framework/deployment/how-the-runtime-locates-assemblies?redirectedfrom=MSDN
  • https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/specify-assembly-location?redirectedfrom=MSDN
  • 关于c# - Visual Studio : Can't load 2 versions of Json. NET 和使用别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63596664/

    相关文章:

    c# - Windows 运行时从 WebView 中的 JavaScript 访问文件

    visual-studio - 为什么我的进程在 64 位操作系统上使用 vshost32.exe 而不是 vshost.exe 运行?

    c# - 更新对象数组

    c# - 如何配置两个JSON序列化器并根据路由选择正确的一个

    c# - NuGet 包导致 visual studio 2015 和 Xamarin 出现问题

    c# - 命令在我的附加属性中获取 null 用于将事件转换为命令

    c# - 从 uwp 应用程序中的代码打开表情符号面板

    python - 当从 python 使用 TSQL(SQL Server 上的 mssql)时,如何自动生成 SQLAlchemy 的 ORM 代码?

    c# - 尝试在 Visual Studio 中执行三个文本框的乘法

    c# - 如何使用 Json.NET.Schema 要求属性?