应用正在构建但无法加载 - System.Web.Mvc缺少文件或程序集?

3

最近我在TFS上的MVC项目中进行了“获取最新版本”的操作,现在每次尝试运行时都会出现黄色的失败屏幕,显示以下内容:

Could not load file or assembly 'System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

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. 

Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Source Error: 


Line 17:             RouteConfig.RegisterRoutes(RouteTable.Routes);
Line 18:             BundleConfig.RegisterBundles(BundleTable.Bundles);
Line 19:         }
Line 20:     }
Line 21: }

Source Line: 19

以下是相关文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace PROJECTdev
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

有人对为什么会出现这种情况以及如何解决有任何想法吗?

我已经尝试过清理/构建解决方案,甚至在TFS中去掉了当前映射并完成了项目的全新映射/下载——这导致了进一步的问题,但在解决修复损坏的引用等问题方面花费了很多麻烦后,我又回到了原点......

如果有任何帮助,我已经粘贴了装配加载跟踪信息:

Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.

=== Pre-bind state information ===
LOG: DisplayName = System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:/John-Projects/PROJECT/PROJECTdev/PROJECTdev/
LOG: Initial PrivatePath = C:\John-Projects\PROJECT\PROJECTdev\PROJECTdev\bin
Calling assembly : PROJECTdev, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\John-Projects\PROJECT\PROJECTdev\PROJECTdev\web.config
LOG: Using host configuration file: C:\Users\jsmith\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.0.0.0 redirected to 5.1.0.0.
LOG: Post-policy reference: System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/8b582d3d/20022f9a/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/8b582d3d/20022f9a/System.Web.Mvc/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/John-Projects/PROJECT/PROJECTdev/PROJECTdev/bin/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/John-Projects/PROJECT/PROJECTdev/PROJECTdev/bin/System.Web.Mvc/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/8b582d3d/20022f9a/System.Web.Mvc.EXE.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/8b582d3d/20022f9a/System.Web.Mvc/System.Web.Mvc.EXE.
LOG: Attempting download of new URL file:///C:/John-Projects/PROJECT/PROJECTdev/PROJECTdev/bin/System.Web.Mvc.EXE.
LOG: Attempting download of new URL file:///C:/John-Projects/PROJECT/PROJECTdev/PROJECTdev/bin/System.Web.Mvc/System.Web.Mvc.EXE.

堆栈跟踪:

[FileNotFoundException: Could not load file or assembly 'System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.]

[FileNotFoundException: Could not load file or assembly 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.]
   PROJECTdev.MvcApplication.Application_Start() in c:\john-Projects\PROJECT\PROJECTdev\PROJECTdev\Global.asax.cs:19

[HttpException (0x80004005): Could not load file or assembly 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9936485
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Could not load file or assembly 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9950728
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

编辑:

虽然我的构建没有出现任何错误,但我收到了以下警告(对于第一个警告,不确定如何将“日志详细程度”设置为“详细”):

enter image description here

编辑2:

所以,我不知道发生了什么变化,但今天早上我的System.Web.Mvc属性可以选择,并且我能够将Copy Local属性设置为True。现在当我运行应用程序时,我得到以下结果:

Server Error in '/' Application.

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

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. 

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

Source Error: 

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.

Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web.Mvc' could not be loaded.


=== Pre-bind state information ===
LOG: DisplayName = System.Web.Mvc
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Web.Mvc | 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:/John-Projects/PROJECT/PROJECTdev/PROJECTdev/
LOG: Initial PrivatePath = C:\John-Projects\PROJECT\PROJECTdev\PROJECTdev\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\John-Projects\PROJECT\PROJECTdev\PROJECTdev\web.config
LOG: Using host configuration file: C:\Users\jSmith\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.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:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/8b582d3d/20022f9a/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/8b582d3d/20022f9a/System.Web.Mvc/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/John-Projects/PROJECT/PROJECTdev/PROJECTdev/bin/System.Web.Mvc.DLL.
LOG: Using application configuration file: C:\John-Projects\PROJECT\PROJECTdev\PROJECTdev\web.config
LOG: Using host configuration file: C:\Users\jSmith\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.0.0.0 redirected to 5.1.0.0.
LOG: Post-policy reference: System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/8b582d3d/20022f9a/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/8b582d3d/20022f9a/System.Web.Mvc/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/John-Projects/PROJECT/PROJECTdev/PROJECTdev/bin/System.Web.Mvc.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Stack Trace: 


[FileLoadException: Could not load file or assembly 'System.Web.Mvc' 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 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 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 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 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) +521

[HttpException (0x80004005): Could not load file or assembly 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 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) +9950728
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

请问大家对接下来的步骤有什么想法?

参考此篇文章,我查看了我的bin文件夹并发现System.Web.Mvc.dll文件版本为4.0.20710.0,根据我所相信的方式在Web.config文件中定义:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <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.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.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="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.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>
    </assemblyBinding>

EDIT3:

参考这篇文章,我确保我的应用程序中包含了所有列出的引用:

References

当我没有在列表中看到Microsoft.Web.Infrastructure时,我以为找到了问题所在,但事实并非如此:

PM> Install-Package Microsoft.Web.Infrastructure
'Microsoft.Web.Infrastructure 1.0.0.0' already installed.
PROJECTdev already has a reference to 'Microsoft.Web.Infrastructure 1.0.0.0'.

EDIT4 --(Chow先生的建议):

  • 1.) 在Visual Studio中转到“工具” => “扩展和更新”。如果有NuGet的更新,请更新它,然后关闭并重新打开项目。可能是包已更改,如果您没有最新版本,则包不会像应该自动更新。

VS2013已经更新(更新4),但问题仍然存在。

  • 2.) 删除bin / obj目录。清理解决方案并不总是完全清理,您可能在其中一个目录中有旧程序集。

我按照指示清理了这些文件夹,现在我的项目无法构建,出现43个错误。大多数涉及找不到: DbSetEntity 的类型或命名空间名称,以及分别为DbMigration / DbMigrationsConfiguration的一个错误。似乎导致所有其他问题的主要错误如下:

The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.

我接着尝试通过Nuget命令 Install-Package EntityFramework -Version 6.0.0 添加特定的包和版本,结果如下:

PM> Install-Package EntityFramework -Version 6.0.0
Updating 'EntityFramework' from version '6.1.0' to '6.0.0' in project 'PROJECTdev'.
Install-Package : Updating 'EntityFramework 6.1.0' to 'EntityFramework 6.0.0' failed. Unable to find a version of 'Microsoft.AspNet.Identi
ty.EntityFramework' that is compatible with 'EntityFramework 6.0.0'.
At line:1 char:16
+ Install-Package <<<<  EntityFramework -Version 6.0.0
    + CategoryInfo          : NotSpecified: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

思考一下这个问题?
3.) 在Web.Config中查看运行时=>assemblyBinding部分的绑定重定向。新版本值应该是更新的版本。
我已经复制了我的Web.config文件中部分,如下所示。我没有发现任何问题:
   <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <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.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.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="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.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>
    </assemblyBinding>
  </runtime>
  • 4.) 查看每个项目中的所有程序集,找到正在使用错误版本的程序集,并将其移除并重新添加。

在这个特定的解决方案中,我只有一个项目。我不确定哪个程序集是错误版本?


http://haacked.com/archive/2008/11/03/bin-deploy-aspnetmvc.aspx/ - asawyer
这是在本地而不是部署上,但当我选择System.Web.Mvc的引用并在VS2013中转到属性时,所有属性都变灰了,我无法选择Copy Local = True - Analytic Lunatic
@asawyer,今天早上我成功修改了该属性。请查看我上面的编辑。 - Analytic Lunatic
删除我所有的bin和obj目录解决了我的问题。 - user8128167
2个回答

4
有几个原因会导致这种情况的发生,遇到这种问题时,我会采取以下措施:
1.) 在Visual Studio中转到 "工具" => "扩展和更新"。如果有NuGet的更新,请更新它,然后关闭并重新打开项目。可能是包已经更改了,如果您没有最新版本,包将不会像应该自动更新。
2.) 删除bin/obj目录。清理方案并不能完全清除,您可能在其中一个目录中拥有旧的程序集。
3.) 在Web.Config文件中查看"运行时"=> "assemblyBinding"部分中的绑定重定向。新版本值应为较新版本。
4.) 查看每个项目中的每个程序集,并找到正在使用错误版本的程序集。删除引用并重新添加。

感谢您的回复,尊敬的Chow先生。请查看我上面的编辑,涉及到您提出的每一个建议。 - Analytic Lunatic

0

我们在使用Autofac时遇到了这个问题。结果发现是web.config文件中的错误合并。

我们在一个dependentAssembly节点内有两个assemblyIdentity节点。实际上,合并操作去掉了一个结束标签和一个开始标签。它位于assemblyBinding块的顶部,并似乎使其后面的所有绑定重定向都失效了。

当然,这是我们最后检查的地方...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接