I have made a change to a web service in Visual Studio 2019 and although it builds it throws an error when I try to publish. The web service is built on .Net Framework 4.5.1, and the error I'm seeing is:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v16.0\Web\Transform\Microsoft.Web.Publishing.AspNetCompileMerge.targets(462,5): Error MSB6006: "aspnet_compiler.exe" exited with code 1.
I've tried googling the error but most of the answers talk about issues in asp.net pages, which don't apply here as the project is a web service. I also tried restarting my pc but again it made no difference. Does anyone have any ideas how to fix this, please?
Update 03/04/2023
I did some more investigation and found this in the Event Viewer:
Application: csc.exe Framework Version: v4.0.30319 Description: The application requested process termination through System.Environment.FailFast(string message). Message: System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I don't understand this. The code has no reference to System.Runtime.CompilerServices.Unsafe, and neither does the version of the code on our live server. I have tried adding this with Nuget but it makes no difference. I should also note that the issue happens when I try to run the web service, as well as when I try to publish it.
Update 03/04/2023 - Part 2
I undid all my changes and started again. It still won't publish, but now the error I get is:
Application: csc.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.IO.FileLoadException at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(System.String[])
Is this a bug with csc.exe? If so, does anyone know a workaround?
I finally managed to get the publish to work. I came across a post I wrote in July 2022 which doesn't have an answer as such, but did point me in the right direction. Basically I deleted the code from my machine and re-cloned it from DevOps. Then I just had to fix the references and the publish worked.