本文介绍了无法加载文件或程序集“Newtonsoft.Json"或其依赖项之一.清单定义与程序集引用不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搜索后尝试过的事情:

Things I've tried after searching:

  1. 在 Web.Config 中对旧版本进行绑定:

  1. in Web.Config put a binding on the old version:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.1.0" />
</dependentAssembly>

  • 编辑我的 .csproj 文件以确保只有一个 Newtonsoft 引用

  • Edit my .csproj file to make sure there is only one Newtonsoft reference

    <Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
       <HintPath>..packagesNewtonsoft.Json.6.0.1lib
    et45Newtonsoft.Json.dll</HintPath>
       <SpecificVersion>False</SpecificVersion>
       <Private>True</Private>
    </Reference>
    

  • 在我的计算机上搜索每个 Newtonsoft.Json.dll 并删除所有非 6.0.1 版本并删除我的临时文件夹中的内容

  • Search my computer for every Newtonsoft.Json.dll and delete every non 6.0.1 version and delete the contents of my temp folder

    在 nuget 管理器控制台中修复/重新安装包

    Repair/Reinstall the package in nuget manager console

    构建成功,但访问网站时出错.

    It succeeds on building, but gets the error when going to the site.

    编辑

    好的,然后我尝试像每个 nuget 包一样重新安装,它似乎已经重新添加了 4.5 版本的 newtonsoft.json.dll,但我遇到了同样的错误.我的项目的目标框架是 .NET 4.5.1,这是我现在得到的堆栈跟踪:

    ok, so then I tried to reinstall like every nuget package, and it seems to have added back the 4.5 version of the newtonsoft.json.dll, but I'm getting the same error. My project's Target freamework is .NET 4.5.1 and here is the stack trace I'm getting now:

    /"应用程序中的服务器错误.

    无法加载文件或程序集 Newtonsoft.Json 或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(来自 HRESULT 的异常:0x80131040)

    Could not load file or assembly Newtonsoft.Json or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    描述:在执行当前 Web 请求的过程中发生了未处理的异常.请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    异常详细信息:System.IO.FileLoadException:无法加载文件或程序集 Newtonsoft.Json 或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(来自 HRESULT 的异常:0x80131040)

    Exception Details: System.IO.FileLoadException: Could not load file or assembly Newtonsoft.Json or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    源错误:

    在执行当前 Web 请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息.

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    程序集加载跟踪:以下信息有助于确定无法加载程序集 Newtonsoft.Json 的原因.

    Assembly Load Trace: The following information can be helpful to determine why the assembly Newtonsoft.Json could not be loaded.

    === Pre-bind state information ===
    LOG: DisplayName = Newtonsoft.Json
    (Partial)
    WRN: Partial binding information was supplied for an assembly:
    WRN: Assembly Name: Newtonsoft.Json | Domain ID: 2
    WRN: A partial bind occurs when only part of the assembly display name is provided.
    WRN: This might result in the binder loading an incorrect assembly.
    WRN: It is recommended to provide a fully specified textual identity for the assembly,
    WRN: that consists of the simple name, version, culture, and public key token.
    WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
    LOG: Appbase = file:///c:/users/user/documents/visual studio 2013/Projects/foo/bar/
    LOG: Initial PrivatePath = c:usersuserdocumentsvisual studio 2013Projectsfooarin
    Calling assembly : (Unknown).
     ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: c:usersuserdocumentsvisual studio 2013Projectsfooarweb.config
    LOG: Using host configuration file: C:UsersuserDocumentsIISExpressconfigaspnet.config
    LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json.DLL.
    LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json/Newtonsoft.Json.DLL.
    LOG: Attempting download of new URL file:///c:/users/user/documents/visual studio 2013/Projects/foo/bar/bin/Newtonsoft.Json.DLL.
    LOG: Using application configuration file: c:usersuserdocumentsvisual studio 2013Projectsfooarweb.config
    LOG: Using host configuration file: C:UsersuserDocumentsIISExpressconfigaspnet.config
    LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.config.
    LOG: Redirect found in application configuration file: 4.5.0.0 redirected to 6.0.1.0.
    LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.1.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
    LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json.DLL.
    LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json/Newtonsoft.Json.DLL.
    LOG: Attempting download of new URL file:///c:/users/user/documents/visual studio 2013/Projects/foo/bar/bin/Newtonsoft.Json.DLL.
    WRN: Comparing the assembly name resulted in the mismatch: Major Version
    ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
    

    堆栈跟踪:

    [FileLoadException: Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
    
    [FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.1.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
           System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
           System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +34
           System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
           System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77
           System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +16
           System.Reflection.Assembly.Load(String assemblyString) +28
           System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38
    
    [ConfigurationErrorsException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.1.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
           System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +752
           System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +218
           System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130
            System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170
           System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +91
           System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +285
           System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
           System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516
    
    [HttpException (0x80004005): Could not load file or assembly 'Newtonsoft.Json, Version=6.0.1.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
           System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9913572
           System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
           System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
    
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
    

    推荐答案

    为了解决这个问题,我通过运行以下命令并检查结果来确保我所有的项目都使用相同的版本:

    To solve this, I ensured all my projects used the same version by running the following command and checking the results:

    update-package Newtonsoft.Json -reinstall
    

    而且,最后我从我的 web.config 中删除了以下内容:

    And, lastly I removed the following from my web.config:

      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    

    如果您想确保所有 Newtonsoft.Json 包都是相同版本,您可以像这样指定版本:

    If you want to ensure all your Newtonsoft.Json packages are the same version, you can specify the version like so:

    update-package Newtonsoft.Json -version 6.0.0 -reinstall
    

    这篇关于无法加载文件或程序集“Newtonsoft.Json"或其依赖项之一.清单定义与程序集引用不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 07-20 16:20