roonz11
roonz11

Reputation: 855

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

Upvotes: 21

Views: 23882

Answers (12)

hills
hills

Reputation: 81

Maybe some day I'll help someone) So in my case problem was in VS 2022 new update that I installed. I had build error exactly in terminal, not in VS. I've tried to roll back an VS update and its helped.

Upvotes: 0

Pecs Correia
Pecs Correia

Reputation: 71

VS sometimes looses the permission to access some files in the Obj directory. There are two possible solutions 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)

Upvotes: 0

CodeThief
CodeThief

Reputation: 749

I had this problem and it was because BitDefender had quarantined the apphost.exe file via Advanced Threat Protection.

Even if you add an exception the file stays locked by Windows until you REBOOT!

Upvotes: 2

Raj K
Raj K

Reputation: 638

In my case, the folder path was too long. I simply shifted my project to a short path directory i.e., D:/MyProject1 and it works.

Upvotes: 0

Prakash Dahal
Prakash Dahal

Reputation: 4875

I was running the code inside shared directory, moving the folder out of shared directory and running the dotnet again solved the error.

Upvotes: 0

Romit Mehta
Romit Mehta

Reputation: 33

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!

Upvotes: 1

Ceres Rohana
Ceres Rohana

Reputation: 101

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.

Upvotes: 1

lzydrmr
lzydrmr

Reputation: 909

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.

Upvotes: 4

okud
okud

Reputation: 233

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.

Upvotes: 0

Tien
Tien

Reputation: 199

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.

Upvotes: 5

simply good
simply good

Reputation: 1103

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

Upvotes: 9

Boris Serafimov
Boris Serafimov

Reputation: 635

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

Upvotes: 2

Related Questions