我在我的 4.5.1 WebForms 应用程序中添加了一个 2.1 Web API 控制器类。之后,我的 packages.config 看起来像:
<package id="Microsoft.AspNet.WebApi" version="5.2.7" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.7" targetFramework="net451" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net451" />
我的 Web.config 如下所示:
<runtime>
<assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
我按照本教程中的说明进行操作:https ://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/using-web-api-with -aspnet-web-forms
我将以下内容添加到Application_Start
:
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
但是,当我运行并尝试运行此行时,我收到以下错误:
System.IO.FileLoadException
HResult=0x80131040
Message=Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.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)
Source=System.Net.Http.Formatting
StackTrace:
at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter..ctor()
at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
at System.Web.Http.HttpConfiguration.DefaultFormatters(HttpConfiguration config)
at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes)
at System.Web.Http.GlobalConfiguration.<>c.<CreateConfiguration>b__11_0()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at System.Web.Http.GlobalConfiguration.get_Configuration()
at System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults, Object constraints, HttpMessageHandler handler)
at System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults)
at Modeler.Global.Application_Start(Object sender, EventArgs e) in C:\BTR\Evolution\Websites\ESS\DRA\Global.asax.cs:line 13
我尝试删除 web.config 中的绑定重定向信息并收到相同的错误。
更新
最初,我的库 dll 引用了 Newtonsoft 的 11.0.2。我将它们更新到 12.0.2 以试图使没有的工作正常进行。所以我把我所有的项目(包括网站)都放回了 11.0.2 仍然有同样的问题。但我想指出,我在一个 Webforms 项目中进行了测试,但没有引用我的库 dll,该库 dll 已经对 Newtonsoft 有 12.0.3 引用并且它有效。因此,我有一些引用 Newtonsoft 的依赖 dll,但我不知道它可能是什么。
所有参考 11.0.2 和网站运行时(如果我注释掉对 的调用MapHttpRoute
),AppDomain.CurrentDomain.GetAssemblies()
正确显示:
{Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed}
融合日志
*** Assembly Binder Log Entry (4/16/2021 @ 10:44:07 AM) ***
The operation failed.
Bind result: hr = 0x80131040. No description available.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\IIS Express\iisexpress.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
(Fully-specified)
LOG: Appbase = file:///C:/BTR/Evolution/Websites/ESS/DRA/
LOG: Initial PrivatePath = C:\BTR\Evolution\Websites\ESS\DRA\bin
LOG: Dynamic Base = C:\Users\terry.aney\AppData\Local\Temp\Temporary ASP.NET Files\vs\66fd6d3a
LOG: Cache Base = C:\Users\terry.aney\AppData\Local\Temp\Temporary ASP.NET Files\vs\66fd6d3a
LOG: AppName = 92fcbfc0
Calling assembly : System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\BTR\Evolution\Websites\ESS\DRA\web.config
LOG: Using host configuration file: C:\Users\terry.aney\OneDrive\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/terry.aney/AppData/Local/Temp/Temporary ASP.NET Files/vs/66fd6d3a/92fcbfc0/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/terry.aney/AppData/Local/Temp/Temporary ASP.NET Files/vs/66fd6d3a/92fcbfc0/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/BTR/Evolution/Websites/ESS/DRA/bin/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\BTR\Evolution\Websites\ESS\DRA\bin\Newtonsoft.Json.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.