The CreateAppHost task failed unexpectedly. MSB4018

Viewed 11792

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

9 Answers

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 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.

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.

If anyone still have this issue, stopping the Virus & threat protection -> Real-time protection, solve the problem.

enter image description here

EDIT Use dotnet build from console

It was occurred in dotnet build on docker (dotnet-sdk-3.1, dist:CentOS 8.1).

Log:

/usr/lib64/dotnet/sdk/3.1.111/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(393,5): error MSB4018: The "CreateAppHost" task failed unexpectedly. [<PathToCsproj>.csproj]
 System.IO.IOException: The process cannot access the file '/<PathToCsprojDir>/obj/Debug/netcoreapp3.1/<AssemblyName>' because it is being used by another process. [<PathToCsproj>.csproj]
    at System.IO.FileStream.Init(FileMode mode, FileShare share, String originalPath) in /_/src/System.Private.CoreLib/shared/System/IO/FileStream.Unix.cs:line 86 [<PathToCsproj>.csproj]
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) in /_/src/System.Private.CoreLib/shared/System/IO/FileStream.cs:line 244 [<PathToCsproj>.csproj]
    at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) in /_/src/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs:line 25 [<PathToCsproj>.csproj]
    at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) in /_/src/System.IO.FileSystem/src/System/IO/File.cs:line 77 [<PathToCsproj>.csproj]
    at Microsoft.NET.HostModel.AppHost.BinaryUtils.CopyFile(String sourcePath, String destinationPath) [<PathToCsproj>.csproj]
    at Microsoft.NET.HostModel.AppHost.HostWriter.CreateAppHost(String appHostSourceFilePath, String appHostDestinationFilePath, String appBinaryFilePath, Boolean windowsGraphicalUserInterface, String assemblyToCopyResorcesFrom) [<PathToCsproj>.csproj]
    at Microsoft.NET.Build.Tasks.CreateAppHost.ExecuteCore() [<PathToCsproj>.csproj]
    at Microsoft.NET.Build.Tasks.TaskBase.Execute() [<PathToCsproj>.csproj]
    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [<PathToCsproj>.csproj]
    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [<PathToCsproj>.csproj]

Solution:

Add CopyRetryCount environment variable. After that, it works with warning.

export CopyRetryCount=100
dotnet build --verbosity quiet -c Debug

Acknowledgments:

This answer may be off-topic, but it has similar message. This question has parameter to solve my problem, that was very helpful. Thank you.

this Occasionally happens. Apparently, VS sometimes louse the permission to access some files in the Obj file. There are too possible solution for this:

  1. Close VS and restart as Admin, so that the permissions are granted again

enter image description here

  1. Close VS > Delete the project Obj folder > Open VS > rebuild project (if VS didn't do it automatically)

What fixed for me was deleting all bin and out folder.

This was caused by using VSCode as admin. Apparently after I build solution with VSCode as admin, some gets messed up resulting with this issue.

For the applications running in dotnet core, fire a command dotnet clean to clean up all your binaries. Then dotnet build and you're good to go!

due to multiple sdk version downloaded in system.

remove that from the c:/

then it remove error

Related