MSB4018: The task failed unexpectedly

Yanislav Yanev 6 Reputation points
2022-11-01T21:59:42.827+00:00

Hello I have a problem in VS 2022 .net 6
The problem is: MSB4018: The task failed unexpectedly in Microsoft.Net.Sdk.targets line 554
Did someone have any idea how to fix it?

Severity Code Description Project File Line Suppression State
Error MSB4018 The "CreateAppHost" task failed unexpectedly.
System.AggregateException: One or more errors occurred. ---> System.UnauthorizedAccessException: Access to the path 'C:\Users\Yinislav\Desktop\backend project\obj\Debug\net6.0\apphost.exe' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Microsoft.NET.HostModel.AppHost.HostWriter.<>c__DisplayClass2_0.<CreateAppHost>b__2()
at Microsoft.NET.HostModel.RetryUtil.RetryOnIOError(Action func)
at Microsoft.NET.HostModel.AppHost.HostWriter.CreateAppHost(String appHostSourceFilePath, String appHostDestinationFilePath, String appBinaryFilePath, Boolean windowsGraphicalUserInterface, String assemblyToCopyResorcesFrom, Boolean enableMacOSCodeSign)
--- End of inner exception stack trace ---
at Microsoft.NET.HostModel.AppHost.HostWriter.CreateAppHost(String appHostSourceFilePath, String appHostDestinationFilePath, String appBinaryFilePath, Boolean windowsGraphicalUserInterface, String assemblyToCopyResorcesFrom, Boolean enableMacOSCodeSign)
at Microsoft.NET.Build.Tasks.CreateAppHost.ExecuteCore()
at Microsoft.NET.Build.Tasks.TaskBase.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
---> (Inner Exception #0) System.UnauthorizedAccessException: Access to the path 'C:\Users\Yinislav\Desktop\backend project\obj\Debug\net6.0\apphost.exe' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Microsoft.NET.HostModel.AppHost.HostWriter.<>c__DisplayClass2_0.<CreateAppHost>b__2()
at Microsoft.NET.HostModel.RetryUtil.RetryOnIOError(Action func)
at Microsoft.NET.HostModel.AppHost.HostWriter.CreateAppHost(String appHostSourceFilePath, String appHostDestinationFilePath, String appBinaryFilePath, Boolean windowsGraphicalUserInterface, String assemblyToCopyResorcesFrom, Boolean enableMacOSCodeSign)<---

---> (Inner Exception #1) System.UnauthorizedAccessException: Access to the path 'C:\Users\Yinislav\Desktop\backend project\obj\Debug\net6.0\apphost.exe' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at Microsoft.NET.HostModel.AppHost.HostWriter.CreateAppHost(String appHostSourceFilePath, String appHostDestinationFilePath, String appBinaryFilePath, Boolean windowsGraphicalUserInterface, String assemblyToCopyResorcesFrom, Boolean enableMacOSCodeSign)<---
YanislavOnlineShopBackEnd C:\Program Files\dotnet\sdk\6.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets 554

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,138 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,600 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Tianyu Sun-MSFT 29,346 Reputation points Microsoft Vendor
    2022-11-02T07:47:08.74+00:00

    Hi @Yanislav Yanev ,

    Welcome to Microsoft Q&A forum.

    The error message says "Access to the path C:\Users\xxxx\Desktop\backend project\obj\Debug\net6.0\apphost.exe is denied", please navigate to this directory, and right-click the "net6.0" folder > Properties > switch to Security tab and click Edit > make sure that the permissions(like Full control) for the related accounts have been assigned(also check other sub-folders if necessary).

    Besides, are you using any application/extension/antivirus which may prevent MSBuild from accessing that path? If you try to run Visual Studio as administrator will this error disappear?

    BTW, if it is possible for you to change to another directory(save your project/solution to another folder), please have a try and see if it works.

    Best Regards,
    Tianyu

    • If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.