C# 无法加载文件或程序集“Microsoft.Web.Infrastructure,
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13149851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not load file or assembly 'Microsoft.Web.Infrastructure,
提问by user1348351
I tried to upload my web site to a server. It was working fine with my local host, so I uploaded everything in my localhost wwwrootfolder to the server and changed the connection string.
我试图将我的网站上传到服务器。它在我的本地主机上运行良好,因此我将 localhostwwwroot文件夹中的所有内容上传到服务器并更改了连接字符串。
But there is this error:
但是有这个错误:
Exception information:
Exception type: InvalidOperationException
Exception message: The pre-application start initialization method Start on type RouteDebug.PreApplicationStart threw an exception with the following error message: Could not load file or assembly 'Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..
at System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods)
at System.Web.Compilation.BuildManager.CallPreStartInitMethods()
at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
Could not load file or assembly 'Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
at RouteDebug.PreApplicationStart.Start()
The project was nopcommerce.
该项目是nopcommerce.
What should be done to resolve this error?
应该怎么做才能解决这个错误?
采纳答案by Colin Pear
You will need to include the dll with your project and add a reference to it as well.
您需要在项目中包含 dll 并添加对它的引用。
Here is a link to a similar issue already on Stack: MVC3 Deployment Dependency Problems
这是堆栈上已有类似问题的链接: MVC3 部署依赖问题
回答by Ivo
Try installing Web Platform from https://www.microsoft.com/web/platform/
尝试从https://www.microsoft.com/web/platform/安装 Web 平台
Hope it helps.
希望能帮助到你。
回答by KickerKeeper
You need to download the ASP.NET MVC framework on the server hosting your application. It's a quick fix just download and install from here (This is the MVC 3 framework http://www.asp.net/mvc/mvc3), then boom you are good to go.
您需要在托管您的应用程序的服务器上下载 ASP.NET MVC 框架。这是一个快速修复,只需从这里下载并安装(这是 MVC 3 框架http://www.asp.net/mvc/mvc3),然后你就可以开始了。
回答by Sgurijala
回答by jwrightmail
I found that even though it worked on my dev box, the assembly wasn't added to the project. Search for Microsoft.Web.Infrastructure in NuGet and install it from there. Then, make sure it has Copy Local selected.
我发现即使它在我的开发箱上工作,程序集也没有添加到项目中。在 NuGet 中搜索 Microsoft.Web.Infrastructure 并从那里安装它。然后,确保它选择了“复制本地”。
回答by Omid Rahimi
Very easy solution:
非常简单的解决方案:
In Visual Studio, go to Tools/Library Package Manager/Package Manager Console
在 Visual Studio 中,转到工具/库包管理器/包管理器控制台
<PM> Install-Package Microsoft.Web.InfraStructure
Have a nice time
过得愉快
回答by riaandl
It turns out after doing a Reference Cleaning, it removed Microsoft.Web.Infrastructure, but not from the packages.config file. After trying to add it again using the Package Manager Console, Visual Studio says that it is already installed which is false because it was removed.
事实证明,在执行参考清理后,它删除了Microsoft.Web.Infrastructure,但没有从 packages.config 文件中删除。在尝试使用 再次添加它后Package Manager Console,Visual Studio 说它已经安装,这是错误的,因为它已被删除。
I then removed the line of code in the packages.configfile
然后我删除了packages.config文件中的代码行
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
and ran the command again
并再次运行命令
PM> Install-Package Microsoft.Web.Infrastructure
After this, now it works fine.
在此之后,现在它工作正常。
回答by Javier
Resharper detected Microsoft.Web.Infrastructure as an unused reference an so I deleted it. Locally was working fine but then I got the same error after publishing to dev.
Resharper 将 Microsoft.Web.Infrastructure 检测为未使用的引用,因此我将其删除。本地工作正常,但在发布到开发人员后我遇到了同样的错误。
Conclusion, beware when deleting references marked as unused by Resharper
结论,删除被 Resharper 标记为未使用的引用时要小心
回答by Rob
Here was my scenario.
这是我的场景。
I had a multi project solution containing projects A, B, C .. N.
我有一个包含项目 A、B、C .. N 的多项目解决方案。
Project B was a code library that contained a factory for selectlistobjects.
项目 B 是一个包含selectlist对象工厂的代码库。
The project would run as expected in development, but when publishing to our test environment I was getting the error you were encountering:
该项目将在开发中按预期运行,但是在发布到我们的测试环境时,我遇到了您遇到的错误:
Could not load file or assembly 'Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
What had happened was through nuget package manager, I had accidentally installed "Microsoft ASP.NET MVC" which installed dependencies for:
发生的事情是通过 nuget 包管理器,我不小心安装了“Microsoft ASP.NET MVC”,它安装了以下依赖项:
- Microsoft.AspNet.Razor
- Microsoft.AspNet.WebPages
- 微软.AspNet.Razor
- Microsoft.AspNet.WebPages
Low and behold, Microsoft.AspNet.WebPages depends on "Microsoft.Web.Infrastructure".
瞧,Microsoft.AspNet.WebPages 依赖于“Microsoft.Web.Infrastructure”。
My solution was uninstalling the three packages mentioned above (MVC, Razor, WebPages) then right click references > add reference > Assemblies > Extensions > System.Web.MVC.
我的解决方案是卸载上面提到的三个包(MVC、Razor、WebPages),然后右键单击引用 > 添加引用 > 程序集 > 扩展 > System.Web.MVC。
回答by Alexander Christov
Despite the number of answers I'll add another one which IMHO makes the things a bit clearer.
尽管答案很多,但我会添加另一个答案,恕我直言,让事情变得更清楚一些。
As Roband wrightmailalready mentioned Microsoft.Web.Infrastructureis a NuGet package (link not needed, you have it in NuGet Package Manager).
正如Rob和wrightmail已经提到的Microsoft.Web.Infrastructure是一个 NuGet 包(不需要链接,你在 NuGet 包管理器中有它)。
Apparently, it was referenced by your project and suddenly disappeared. A number of reason may exists but the important thing is that despite you may have enabled Automatic Package Restore in Visual Studioby:
显然,它被您的项目引用并突然消失了。可能存在多种原因,但重要的是,尽管您可能通过以下方式在 Visual Studio 中启用了自动包还原:
- Manage NuGet packages for solution (context menu in Solution Explorer),
- Allow NuGet to download missing packages (settings),
- Automatically check for missing packages during build in Visual Studio (settings),
- 管理解决方案的 NuGet 包(解决方案资源管理器中的上下文菜单),
- 允许 NuGet 下载丢失的包(设置),
- 在 Visual Studio 中构建期间自动检查丢失的包(设置),
certain packages may require a manual reinstall. I am not aware what confuses NuGet, maybe manually removing a reference, but here is the solution I usually apply in such cases. The following PM Console helps restoring a package while preserving the original version (not updating to possibly existing new one):
某些软件包可能需要手动重新安装。我不知道是什么让 NuGet 感到困惑,也许是手动删除引用,但这是我通常在这种情况下应用的解决方案。以下 PM 控制台有助于恢复软件包,同时保留原始版本(不更新到可能存在的新版本):
Update-Package Microsoft.Web.Infrastructure -Reinstall
更新包 Microsoft.Web.Infrastructure -重新安装
Version preservation may be required if you do not want to accidentally overwrite an existing package with its newer version which possibly removes "old" functionality you may have used in your project.
如果您不想意外地用较新版本覆盖现有包,这可能会删除您可能在项目中使用的“旧”功能,则可能需要保留版本。
And, as a proof, despite a bit lengthy one, that the version does not change, here's the output when the command is executed:
而且,作为证明,尽管有点冗长,但版本没有改变,这是执行命令时的输出:
PM> Update-Package Microsoft.Web.Infrastructure -Reinstall
Attempting to gather dependencies information for multiple packages with respect to project 'Samples.NuGet\DemoApp\DemoApp', targeting '.NETFramework,Version=v4.5.2'
Attempting to resolve dependencies for multiple packages
Resolving actions install multiple packages
...
Package removal starts here...
...
Removed package 'Microsoft.AspNet.Web.Optimization 1.1.3' from 'packages.config'
Successfully uninstalled 'Microsoft.AspNet.Web.Optimization 1.1.3' from DemoApp
Removed package 'WebGrease 1.5.2' from 'packages.config'
Executing script file 'D:\Projects\DemoApp\packages\WebGrease.1.5.2\tools\uninstall.ps1'
Successfully uninstalled 'WebGrease 1.5.2' from DemoApp
...
More package removals here. Omitted for brevity...
...
Removed package 'Microsoft.Web.Infrastructure 1.0.0.0' from 'packages.config'
Successfully uninstalled 'Microsoft.Web.Infrastructure 1.0.0.0' from DemoApp
...
More package removals here. Omitted for brevity...
...
Removed package 'Antlr 3.4.1.9004' from 'packages.config'
Successfully uninstalled 'Antlr 3.4.1.9004' from MvcLenseApp
Package 'Antlr.3.4.1.9004' already exists in folder 'D:\Projects\Lense.Mvc5\packages'
--- Install packages (in reverse order) ---
Package 'Antlr.3.4.1.9004' already exists in folder 'D:\Projects\DemoApp\packages'
Added package 'Antlr.3.4.1.9004' to 'packages.config'
Successfully installed 'Antlr 3.4.1.9004' to DemoApp
...
More package installs here. Omitted for brevity...
...
Package 'Microsoft.Web.Infrastructure.1.0.0' already exists in folder 'D:\Projects\Lense.Mvc5\packages'
Added package 'Microsoft.Web.Infrastructure.1.0.0' to 'packages.config'
Successfully installed 'Microsoft.Web.Infrastructure 1.0.0' to MvcLenseApp
...
More package installs here. Omitted for brevity...
...
Package 'WebGrease.1.5.2' already exists in folder 'D:\Projects\DemoApp\packages'
Added package 'WebGrease.1.5.2' to 'packages.config'
Executing script file 'D:\Projects\DemoApp\packages\WebGrease.1.5.2\tools\install.ps1'
Successfully installed 'WebGrease 1.5.2' to DemoApp
Package 'Microsoft.AspNet.Web.Optimization.1.1.3' already exists in folder 'D:\Projects\DemoApp\packages'
Added package 'Microsoft.AspNet.Web.Optimization.1.1.3' to 'packages.config'
...
End of package re-install.
...
Successfully installed 'Microsoft.AspNet.Web.Optimization 1.1.3' to DemoApp
PM>
Of course if you wish to reinstall all packages you may need to get familiar with update/install commands in NuGet hereand here.

