我创建了一个 Web 项目,它在 Visual Studio 中运行良好。但是,将其发布到 azurewebsites 后出现以下错误。什么可能导致问题?
无法加载文件或程序集“System.Web.Http.WebHost,Version=5.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (来自 HRESULT 的异常:0x80131040) 说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.IO.FileLoadException:无法加载文件或程序集“System.Web.Http.WebHost,Version=5.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (来自 HRESULT 的异常:0x80131040)源错误:在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。程序集加载跟踪:以下信息有助于确定为什么无法加载程序集“System.Web.Http.WebHost, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”。警告:程序集绑定日志记录已关闭。要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) 设置为 1。注意:与程序集绑定失败日志记录相关的一些性能损失。要关闭此功能,请删除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。
以下是 web.config 文件的一部分。
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.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>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
已发布(已部署的环境)中缺少 dll
。这就是为什么它可以在本地即 Visual Studio 中工作但不能在 Azure 网站环境中工作的原因。
只需在程序集的属性中执行 Copy Local = true
(System.Web.Http.WebHost),然后重新部署,它应该可以正常工作。
如果您遇到类似的错误,即缺少其他程序集,则将该程序集设置为 copylocal=true 并重新部署,重复此操作 - 如果您不确定它的依赖关系。
如果您仍在寻找答案,请尝试检查此 question thread。它帮助我解决了类似的问题。
编辑:帮助我的解决方案是从 NugGet 包管理器运行 Update-Package Microsoft.AspNet.WebApi -reinstall
,正如 Pathoschild 所建议的那样。然后我不得不删除我的 .suo 文件并重新启动 VS,正如 Sergey Osypchuk 在 this thread 中所建议的那样。
我遇到了同样的问题,我通过将以下库的 CopyLocal
设置为 true 来解决它:
System.Web.Http.dll
System.Web.Http.WebHost.dll
System.Net.Http.Formatting.dll
我必须补充一点,我使用 MVC4 和 NET 4
对我来说,将以下部分添加到 web.config
文件中:
<configuration>
...
<runtime>
...
<dependentAssembly>
<assemblyIdentity name="System.Web.Http.WebHost" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
</dependentAssembly>
...
</runtime>
...
</configuration>
此示例代表 MVC 5.1。希望它能帮助某人解决此类问题。
Microsoft.AspNet.WebApi.WebHost
包。
对我来说,它在发布对话框设置下的文件发布选项中选择“在目标位置删除其他文件”后开始工作。
已发布(已部署的环境)中缺少 dll。这就是为什么它可以在本地即 Visual Studio 中工作但不能在 Azure 网站环境中工作的原因。
只需在程序集(System.Web.Http.WebHost)的属性中执行 Copy Local = true ,然后重新部署,它应该可以正常工作。
我正在使用 vs2012,我认为更新 KB2781514 更改了一些设置。我的 MVC4 项目中的所有 System.Web.Http 都更改为 false,并且我一直收到此消息。我更改了发布属性中的 All file in this project
,但它不起作用。最后我不得不一一更改Copy Local = true
并解决了这个问题。
我遇到了同样的错误,我将我的版本从 4 更改为 3 并解决了:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- Ensure correct version of MVC -->
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
我在我的应用程序中遇到了同样的问题。
System.web.http.webhost not found.
您只需从您在 Visual Studio 中运行的主项目复制 system.web.http.webhost
文件并将其粘贴到您已发布的项目 bin
目录中。
在此之后它可能会显示相同的错误,但目录名称已更改,它可能是 system.web.http
。遵循与上述相同的程序。上传所有文件后,它将起作用。这是由于他们从 Internet 下载的 Visual Studio 中的 nuget 包,但在服务器上无法下载它。
您可以在项目 bin
目录中找到此文件。
这发生在我的 VS2013(更新 5)/ASP.NET 4.5 上,在包含 MVC 和 Web API 2 的项目类型“Web 应用程序”下。在创建项目后和添加任何代码之前发生错误。添加以下配置为我修复它。解决“System.Web.Helpers”问题后,“System.Web.Mvc”和“System.Web.WebPages”又出现了两个类似的错误。
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
我错过了几个 DLL。即使我下次发布时手动将它们复制到目录中,它们也会消失。每个都已在 VS 中设置为本地复制。对我来说,解决方法是将每个设置为本地复制为假,保存,构建,然后将每个设置为在本地复制为真。这次我发布了所有正确发布的 DLL。奇怪的
如果您的解决方案中有多个项目,并且您的一个项目由于此错误而无法构建,请确保您已在该项目中安装了 WebApi Core nuget 包。简单地添加对 System.Web.Http 的引用并没有帮助,您需要将正确的 nuget 包安装到该项目中。
我的解决方案中有多个项目,并且 WebApi Core 已经安装在另一个项目中。我通过右键单击并从列表中勾选程序集来引用 System.Web.Http 程序集,它在 Azure 上不起作用,尽管在本地它可以构建。我不得不删除手动引用并将 WebApi Core nuget 包添加到需要程序集引用的每个项目中。
如果“复制本地”已经为真,我发现如果您删除它已发布到的文件并再次发布,它有时会起作用。
例如,如果您使用的是 IIS,请删除网站及其发布到的目录的内容,然后再次发布。
目的地可能有旧版本的文件,因此为确保您没有使用旧版本,请在再次发布之前删除所有内容。
我从 web.config 中删除了以下条目,它对我有用。
<dependentAssembly>
<assemblyIdentity name="System.Web.Http.WebHost" culture="neutral" publicKeyToken="31BF3856AD364E35" />
<bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="5.2.6.0" />
</dependentAssembly>
确保整个解决方案中的包版本相同。我刚刚降级 &跨解决方案升级了 Microsoft.AspNet.Mvc
包,问题已解决。
就我而言,这个问题可能发生在弄乱 nuget 并让它协调版本之后。
我发现了这个系统。我的开发文件夹中的 Web.http.webhost 版本与已部署文件夹中的版本不同。我所做的只是删除文件,重新部署,然后它就可以工作了。
请注意,我还设置了其他人所说的“复制本地”的引用,但这不起作用。最后我不能说它是否也是一个帮助因素。
不定期副业成功案例分享
Copy Local
已经是 True。奇怪的是它显示Runtime Version
是 v4.0.30319 而不是 v5?