C# 应用程序正在生成,但无法加载-System.Web.Mvc缺少文件或程序集?

C# 应用程序正在生成,但无法加载-System.Web.Mvc缺少文件或程序集?,c#,asp.net,asp.net-mvc,tfs,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Tfs,Asp.net Mvc 5,最近,我在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. De

最近,我在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
编辑

虽然我没有得到任何关于构建的错误,但我得到了以下警告(关于第一个警告,不确定如何将“log verbosity”设置为“detailed”):

EDIT2

所以,我不知道发生了什么变化,但今天早上我的
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>
EDIT4--(周议员的建议):

  • 1.)在Visual Studio中,转到工具=>扩展和更新。如果要更新NuGet update,请关闭并重新打开项目。可能是软件包已更改,如果您没有最新版本,软件包将不会自动更新。
VS2013有一个更新(更新4),但仍然是相同的问题

  • 2.)删除您的bin/obj目录。清洁解决方案并不总是完全清洁,您可能在其中一个目录中有一个旧程序集。
我按照指示清除了这些文件夹,现在我的项目无法生成,出现43个错误。其中大多数涉及到找不到
类型或名称空间名称
DbSet
实体
,以及
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
  • 查看每个项目中的每个程序集,找出使用错误版本的程序集。删除引用并重新添加。

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

有几件事可能会导致此问题,当我遇到此问题时,我会执行以下操作:

1.)在Visual Studio中,转到工具=>扩展和更新。如果要更新NuGet update,请关闭并重新打开项目。可能是软件包已更改,如果您没有最新版本,软件包将不会自动更新

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

3.)在Web.Config中,查看runtime=>assemblyBinding部分中的绑定重定向。新版本值应为较新版本


4.)查看每个项目中的每个程序集,找出使用错误版本的程序集。删除引用并重新添加它。

我们与Autofac联合使用了此功能。结果证明web.config中的合并错误

我们在一个
dependentAssembly
节点中有两个
assemblyIdentity
节点。本质上,合并去掉了一个结束标记和一个开始标记。它位于
assemblyBinding
块的顶部,似乎已经删除了它后面的所有绑定重定向


当然,我们最后看的地方…

这是在本地而不是在部署时,但是当我选择
System.Web.Mvc
的引用并转到VS2013中的属性时,所有属性都灰显,我无法选择
Copy Local=True
?@asawyer我今天早上能够更改属性。请看我上面的编辑。删除我所有的bin和obj目录为我解决了这个问题。谢谢回复尊敬的周。关于你的每一条建议,请参见我的上述编辑。
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
   <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>