The CreateAppHost task failed unexpectedly. MSB4018

When I try and build my ASP.NET Core project, I continue to get this error message in Visual Studio 2019. When I build it the second time, the error disappears.

Does anyone know what this error means and how to get rid of it?

Thanks!

<CreateAppHost AppHostSourcePath="$(AppHostSourcePath)"
                   AppHostDestinationPath="$(AppHostIntermediatePath)"
                   AppBinaryName="$(AssemblyName)$(TargetExt)"
                   IntermediateAssembly="@(IntermediateAssembly->'%(FullPath)')"
                   WindowsGraphicalUserInterface="$(_UseWindowsGraphicalUserInterface)"
                   Retries="$(CopyRetryCount)"
                   RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"
                   />

enter image description here


Try running visual studio using 'as administrator' option.

enter image description here

Some of the files which required for complete building may need admin privilege for accessing them. Commonly run Visual Studio has not enough rights to access them


I noticed that my error also noted: System.UnauthorizedAccessException: Access to the path '...\obj\Release\netcoreapp3.1\apphost.exe' is denied.

Deleted the obj folder and rebuild solution resolved the problem for me. Thanks.


I had a similar problem. The error message for my ASP.NET Core application was

error MSB4018: The "CreateAppHost" task failed unexpectedly.
error MSB4018: System.MissingMethodException: Method not found: 'Void Microsoft.NET.HostModel.AppHost.HostWriter.CreateAppHost(...

I tracked the issue down to the environment variable MSBuildSDKsPath, which pointed to an older SDK version. After unsetting the variable with export MSBuildSDKsPath= the problem was resolved. Alternatively, setting the variable to the most recent SDK also worked: export MSBuildSDKsPath=C:\Program Files\dotnet\sdk\6.0.100-rc.2.21505.57\Sdks (just an example).

I don't remember why I set the variable in the first place, and eliminating it didn't seem to hurt either, I just removed it. It seems that in general, it is just not necessary.