ASP.NET MVC WebApi 应用程序发布到 IIS 时出现程序集绑定错误
Assembly binding error on ASP.NET MVC WebApi application when published to IIS
我的任务是维护 ASP.NET MVC 5 WebApi 应用程序。每次我用 IIS Express 运行 这个应用程序时,它 运行 没问题,但每次我将它发布到 IIS 站点并尝试调用任何 API 方法时,都会出现以下错误出现:
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)
堆栈跟踪如下:
[FileLoadException: 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)]
System.Net.Http.Formatting.BaseJsonMediaTypeFormatter..ctor() +0
System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() +73
System.Net.Http.Formatting.MediaTypeFormatterCollection.
CreateDefaultFormatters() +55
System.Web.Http.HttpConfiguration.DefaultFormatters(HttpConfiguration config) +34
System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) +382
System.Web.Http.GlobalConfiguration.<CreateConfiguration>b__0() +94
System.Lazy`1.CreateValue() +708
System.Lazy`1.LazyInitValue() +184
Consalud.Seguridad.WebApi.App_Start.UnityWebApiActivator.Start() +93
[TargetInvocationException: Exception has been thrown by the target of an
invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +260
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +142
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +34
WebActivatorEx.BaseActivationMethodAttribute.InvokeMethod() +280
WebActivatorEx.ActivationManager.RunActivationMethods(Boolean designerMode) +749
WebActivatorEx.ActivationManager.RunPreStartMethods(Boolean designerMode) +49
WebActivatorEx.ActivationManager.Run() +75
[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation.]
System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +850
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +162
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +128
System.Web.Compilation.BuildManager.ExecutePreAppStart() +170
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +820
[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +523
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +107 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +688
库版本:
- Newtonsoft.Json 10.0.3
- 统一 4.0.1
- Unity.AspNet.WebApi 4.0.1
- WebActivatorEx 2.0.0
- Microsoft.Net.Http 2.2.29
Unity 用于整个应用程序的依赖注入。
Web.config 程序集重定向部分:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<publisherPolicy apply="no" />
<assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
UnityWebApiActivatorclass:
public static class UnityWebApiActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
// Use UnityHierarchicalDependencyResolver if you want to use a new child container for each IHttpController resolution.
var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());
// var resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}
据我所知,异常是在 UnityWebApiActivator.Start()
的 GlobalConfiguration.Configuration.DependencyResolver = resolver;
行抛出的。
检查 IIS 服务器上使用的实际部署的 web.config
文件(Web 应用程序根文件夹中的文件)是否具有相同的绑定重定向。
另请注意:任何 *.dll.config
文件都无效;它们通常是 Visual Studio 在开发过程中添加它们的结果,并且它们通常(通常是不必要的)默认部署。
我的任务是维护 ASP.NET MVC 5 WebApi 应用程序。每次我用 IIS Express 运行 这个应用程序时,它 运行 没问题,但每次我将它发布到 IIS 站点并尝试调用任何 API 方法时,都会出现以下错误出现:
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)
堆栈跟踪如下:
[FileLoadException: 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)]
System.Net.Http.Formatting.BaseJsonMediaTypeFormatter..ctor() +0
System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() +73
System.Net.Http.Formatting.MediaTypeFormatterCollection.
CreateDefaultFormatters() +55
System.Web.Http.HttpConfiguration.DefaultFormatters(HttpConfiguration config) +34
System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) +382
System.Web.Http.GlobalConfiguration.<CreateConfiguration>b__0() +94
System.Lazy`1.CreateValue() +708
System.Lazy`1.LazyInitValue() +184
Consalud.Seguridad.WebApi.App_Start.UnityWebApiActivator.Start() +93
[TargetInvocationException: Exception has been thrown by the target of an
invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +260
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +142
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +34
WebActivatorEx.BaseActivationMethodAttribute.InvokeMethod() +280
WebActivatorEx.ActivationManager.RunActivationMethods(Boolean designerMode) +749
WebActivatorEx.ActivationManager.RunPreStartMethods(Boolean designerMode) +49
WebActivatorEx.ActivationManager.Run() +75
[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation.]
System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +850
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +162
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +128
System.Web.Compilation.BuildManager.ExecutePreAppStart() +170
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +820
[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +523
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +107 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +688
库版本:
- Newtonsoft.Json 10.0.3
- 统一 4.0.1
- Unity.AspNet.WebApi 4.0.1
- WebActivatorEx 2.0.0
- Microsoft.Net.Http 2.2.29
Unity 用于整个应用程序的依赖注入。
Web.config 程序集重定向部分:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<publisherPolicy apply="no" />
<assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
UnityWebApiActivatorclass:
public static class UnityWebApiActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
// Use UnityHierarchicalDependencyResolver if you want to use a new child container for each IHttpController resolution.
var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());
// var resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}
据我所知,异常是在 UnityWebApiActivator.Start()
的 GlobalConfiguration.Configuration.DependencyResolver = resolver;
行抛出的。
检查 IIS 服务器上使用的实际部署的 web.config
文件(Web 应用程序根文件夹中的文件)是否具有相同的绑定重定向。
另请注意:任何 *.dll.config
文件都无效;它们通常是 Visual Studio 在开发过程中添加它们的结果,并且它们通常(通常是不必要的)默认部署。