回答@AndreiU的帖子,以及如何在本地重现运行时错误。
我在部署到Azure时遇到了下面的运行时错误,而不是在本地。
{"Message":"An error has occurred.","ExceptionMessage":"尝试创建类型为'OrderController'的控制器时发生错误。
确保该控制器具有无参数的公共构造函数。","ExceptionType":"System.InvalidOperationException","StackTrace":“
在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,Type
controllerType)\r\n 在
System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage
request)\r\n at
System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()","InnerException":{"Message":"An
error has occurred.","ExceptionMessage":“无法加载文件或程序集'System.Net.Http,Version=4.2.0.0,Culture=neutral,
PublicKeyToken = b03f5f7f11d50a3a'或其某个依赖项。该文件
指定。” ,“ExceptionType”:“System.IO.FileNotFoundException”,“StackTrace”:“
at Company.Project.Service.CompanyIntegrationApiService..ctor(Uri
baseAddress)\r\n at
Company.Project.BackOffice.Web.Controllers.OrderController..ctor() in
C:\projects\company-project\src\Company.Project.BackOffice.Web\Controllers\Order\OrderController.cs:line
30\r\n at lambda_method(Closure )\r\n at
System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage
request, HttpControllerDescriptor controllerDescriptor, Type
controllerType)”}}
当我开始查看程序集时,我发现我的Web项目和服务项目针对System.Net.Http
的不同版本。
Web项目:
![enter image description here](https://istack.dev59.com/Zid7c.webp)
服务项目:
![enter image description here](https://istack.dev59.com/eS0SH.webp)
很容易认为这是因为版本不匹配导致的,但关键在于查看错误The system cannot find the file specified.
。
查看路径属性,我们可以看到Web项目针对.NET Framework程序集,而服务则针对来自Visual Studio 2017的程序集。由于服务器未安装Visual Studio 2017,因此将出现运行时错误。
Web路径:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll
服务路径:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll
只需将 Copy Local
设置为 true
,问题就可以得到解决,但并非所有情况都适用。
![enter image description here](https://istack.dev59.com/vnX5w.webp)
要在本地机器上重现错误,只需从 Visual Studio 特定文件夹中删除所需的 System.Net.Http.dll
。这将导致运行时错误和一些构建错误。解决这些错误后,一切应该正常工作,至少对我来说是这样。
如果您是通过 NuGet
安装了 System.Net.Http
,请检查哪个程序集被使用,方法是查看 .csproj
版本。例如,System.Net.Http 4.3.4
给出以下程序集:
<Reference Include="System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
<Private>True</Private>
</Reference>
如果您使用像Jenkins、TeamCity或AppVeyor这样的构建服务器,则运行时缺少的.dll
可能也存在于其中。 在这种情况下,使用System.Net.Http的NuGet版本或在本地删除缺少的.dll
可能无法解决此问题。 要解决此错误,请查看未找到的版本和特定的PublicKeyToken
。 然后根据您的项目,在Web.config
或App.config
中创建绑定重定向。 在我的情况下,我想使用4.0.0.0代替:
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
关于这个问题,有一篇不错的 Github 讨论:
https://github.com/dotnet/corefx/issues/22781
node
一样将VS的包都放在node_modules
文件夹中,这样所有的文件都可以集中管理。遇到问题时,只需删除该文件夹,运行npm install
命令,搞定。 - Jeb50