简体   繁体   English

'Newtonsoft.Json.dll'和'NuGetApi2.dll'中都存在'Newtonsoft.Json.JsonConvert'类型

[英]The type 'Newtonsoft.Json.JsonConvert' exists in both 'Newtonsoft.Json.dll' and 'NuGetApi2.dll'

I am trying to serialize object on the fly into immediate window by using 我试图通过使用动态序列化对象到即时窗口

Newtonsoft.Json.JsonConvert.SerializeObject(myObj);

However I am getting following error 但是我得到了以下错误

The type 'Newtonsoft.Json.JsonConvert' exists in both 'Newtonsoft.Json.dll' and 'NuGetApi2.dll' 'Newtonsoft.Json.dll'和'NuGetApi2.dll'中都存在'Newtonsoft.Json.JsonConvert'类型

It happens when .dll that is not referenced in project is placed into bin folder next to other .dll that is referenced in that project and has same library within (in this case Newtonsoft.Json in NuGetApi2.dll which ins't referenced in project) 当项目中未引用的.dll被放置在该项目中引用的其他.dll旁边的bin文件夹中并且在其中具有相同的库时(在这种情况下, NuGetApi2.dll中的Newtonsoft.Json未在项目中引用) )

Why do I only get this exception when using immediate/debug window an not when it is compiled code? 为什么我在使用立即/调试窗口时才会出现此异常,而不是在编译代码时? (When its compiled it works fine since compiler is using dll that is referenced in the project) (当编译它的工作正常,因为编译器正在使用项目中引用的dll)

How do I tell Visual Studio which .dll to use (preferably without having to stop program from running)? 如何告诉Visual Studio使用哪个.dll (最好不必停止程序运行)? One obvious way is to delete .dll that isn't referenced in project,.... What I am asking for: is there a way to tell it which dll to use via code from Immediate window .... 一个显而易见的方法是删除未在项目中引用的.dll ,....我要求的是:有没有办法通过Immediate window代码告诉它使用哪个dll ....

UPDATE: solution to reproduce bug can be found here: 更新:重现bug的解决方案可以在这里找到:

https://github.com/liufa/Temp https://github.com/liufa/Temp

Solution is zipped in WebApplication1.7z , I used MVC app, to reproduce add breakpoint in HomeController , Index Method and when it gets hit paste Newtonsoft.Json.JsonConvert.SerializeObject("sfdsdfsdf"); 解决方案是在WebApplication1.7z中压缩的,我使用了MVC应用程序,在HomeController重现断点, Index方法以及何时粘贴Newtonsoft.Json.JsonConvert.SerializeObject("sfdsdfsdf"); into immediate window in Visual Studio. 进入Visual Studio的即时窗口。

Code in project looks following: 项目代码如下:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var x = Newtonsoft.Json.JsonConvert.SerializeObject("sdfsdf");
#if DEBUG    
        var y = Newtonsoft.Json.JsonConvert.SerializeObject("sfssesss");
#endif
        return View(); //put breakpoint here then into immediate window paste Newtonsoft.Json.JsonConvert.SerializeObject("sdfsdf"); and you should get the error.
    }

By default .Net web sites load all of the assemblies in the bin folder at startup. 默认情况下,.Net网站会在启动时加载bin文件夹中的所有程序集。 You can change this behavior by modifying the web.config file, in which case you need to list out the assemblies you want loaded instead. 您可以通过修改web.config文件来更改此行为,在这种情况下,您需要列出要加载的程序集。

Here is an example that only loads version 1.0.0.0 of MySite.dll 这是一个仅加载MySite.dll版本1.0.0.0的MySite.dll

<system.web>
  <compilation>
    <assemblies>
      <remove assembly="*" />
      <add assembly="MySite, Version=1.0.0.0, Culture=neutral />
    </assemblies>
  </compilation>
</system.web>

If you only load the assemblies you need and don't load the NuGetApi2 assembly this should solve your problem 如果您只加载所需的组件而不加载NuGetApi2组件,这应该可以解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'Newtonsoft.Json ...'存在于'Blend \ Newtonsoft.Json.dll'和'Solution \ packages \ ... \中 - 'Newtonsoft.Json…' exists in both 'Blend\Newtonsoft.Json.dll' and 'Solution\packages\…\ 'Newtonsoft.Json 中都存在'JsonConvert' 类型 - The type 'JsonConvert' exists in both 'Newtonsoft.Json 需要Newtonsoft.Json.dll的WEPAPI开发 - WEPAPI development requiring Newtonsoft.Json.dll Newtonsoft.Json.dll中发生了类型为&#39;Newtonsoft.Json.JsonReaderException&#39;的未处理异常 - An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll “Newtonsoft.Json ver 9 和 10”中都存在“JsonConvert”类型 - The type 'JsonConvert' exists in both 'Newtonsoft.Json ver 9 and 10 无法从程序集 Newtonsoft.Json 加载类型“Newtonsoft.Json.JsonConvert” - Could not load type 'Newtonsoft.Json.JsonConvert' from assembly Newtonsoft.Json 找不到元数据文件“ Newtonsoft.Json.dll” - Metadata file 'Newtonsoft.Json.dll' could not be found NHibernate.LazyInitializationException发生在Newtonsoft.Json.dll中 - NHibernate.LazyInitializationException occurred in Newtonsoft.Json.dll 无法加载文件或程序集Newtonsoft.json.dll - Could not load file or assembly Newtonsoft.json.dll 在构建服务器中复制Newtonsoft.json.dll - Newtonsoft.json.dll being copied in build server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM