The "CreateAppHost" task failed unexpectedly. How do I fix this?

Jon Hall 20 Reputation points
2024-06-13T00:02:22.32+00:00

I opened a project I worked on a few months and a few updates ago. Now I can't open the form designer. The error list states:

MSB4018 The "CreateAppHost" task failed unexpectedly.

System.IO.IOException: The requested operation cannot be performed on a file with a user-mapped section open.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateCore(SafeFileHandle fileHandle, String mapName, HandleInheritability inheritability, MemoryMappedFileSecurity memoryMappedFileSecurity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, Int64 capacity)

at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(FileStream fileStream, String mapName, Int64 capacity, MemoryMappedFileAccess access, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability, Boolean leaveOpen)

at Microsoft.NET.HostModel.ResourceUpdater.Update()

at Microsoft.NET.HostModel.AppHost.HostWriter.<>c__DisplayClass2_0.<CreateAppHost>b__1()

at Microsoft.NET.HostModel.RetryUtil.RetryOnIOError(Action func)

at Microsoft.NET.HostModel.AppHost.HostWriter.CreateAppHost(String appHostSourceFilePath, String appHostDestinationFilePath, String appBinaryFilePath, Boolean windowsGraphicalUserInterface, String assemblyToCopyResourcesFrom, 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() EQ2Completionist C:\Program Files\dotnet\sdk\8.0.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets 709

and the call stack shows:

|at Microsoft.DotNet.DesignTools.Client.Host.ProjectInfoProvider.NetCore.d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at Microsoft.DotNet.DesignTools.Client.Host.ProjectInfoProvider.d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at Microsoft.DotNet.DesignTools.Client.Host.ServerHostFactory.d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at Microsoft.DotNet.DesignTools.Client.DesignToolsClientLoader.d__29.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at Microsoft.DotNet.DesignTools.Client.DesignToolsClientLoader.<>c__DisplayClass25_1.<<-ctor>b__1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at Microsoft.DotNet.DesignTools.Client.DesignToolsClientLoader.d__27.MoveNext()||||| | -------- | -------- | -------- | -------- | -------- | |||

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,011 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,156 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 48,441 Reputation points Microsoft Vendor
    2024-06-13T03:12:39.6133333+00:00

    Hi @Jon Hall , Welcome to Microsoft Q&A,

    Q&A: There is no way to mark your own case to end.

    I revised and summarized the OP's answer:

    The error you are encountering, MSB4018: The "CreateAppHost" task failed unexpectedly, is associated with a problem during the build process. Specifically, the error message System.IO.IOException: The requested operation cannot be performed on a file with a user-mapped section open indicates that there is a file in use that cannot be accessed because it is memory-mapped, which prevents other operations from being performed on it.

    There are several solutions to this problem:

    Clean and rebuild solution: - Right-click your solution in Solution Explorer and select Clean Solution. - After cleaning, right-click again and select Rebuild Solution.

    Delete temporary files and cache: - Delete the bin and obj folders in your project directory. - Clear the NuGet cache using the command dotnet nuget locals all --clear.

    Update Visual Studio and .NET SDK: - Make sure your Visual Studio and .NET SDK are up to date. Sometimes updates contain fixes for issues like this. - You can check for Visual Studio updates via Help -> Check for Updates. - Update the .NET SDK by downloading the latest version from the official .NET website.

    Modify project files: - You can try to modify the .csproj file to include or exclude certain build steps. For example, if your project does not require "CreateAppHost", try disabling it. Add the following property to your project file: xml <PropertyGroup> <EnableAppHost>false</EnableAppHost> </PropertyGroup>


    OP mainly solves this problem by modifying the SDK:

    I fixed it! Here is what to do:

    Check what SDK your project is using by opening the code of <your project>.csproj

    Then look at the <TargetFramework> tag to get the SDK version:

    <PropertyGroup>

    <OutputType>WinExe</OutputType>

    <TargetFramework>net6.0-windows</TargetFramework>

    <Nullable>enable</Nullable>

    <UseWindowsForms>true</UseWindowsForms>

    <ImplicitUsings>enable</ImplicitUsings>

    <Platforms>AnyCPU;x64</Platforms>

    </PropertyGroup>

    Then, check what SDKs you have installed. To do this, open the command prompt (Run -> cmd). In the command prompt, type

    dotnet --list-sdks

    This will list the SDKs you have installed. From here, you have 2 options:

    1. Change the .NET version in your .csproj code to one you have installed already.
    2. Download the SDK your .csproj is expecting to read.

    Once finished with either, restart Visual Studio and reopen your project. It should now load properly.

    Best Regards,

    Jiale


    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.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. vicsar 5 Reputation points
    2024-11-24T13:52:52.3766667+00:00

    The answer has been found. The application was on a virtual Google Drive. After transferring to a standard disk, the error was gone.

    Source (tested, fixed the problem):

    1 person found this answer helpful.

  2. Jon Hall 20 Reputation points
    2024-06-13T00:15:13.81+00:00

    I fixed it! Here is what to do:

    Check what SDK your project is using by opening the code of <your project>.csproj

    Then look at the <TargetFramework> tag to get the SDK version:

    <PropertyGroup>

    <OutputType>WinExe</OutputType>

    <TargetFramework>net6.0-windows</TargetFramework>

    <Nullable>enable</Nullable>

    <UseWindowsForms>true</UseWindowsForms>

    <ImplicitUsings>enable</ImplicitUsings>

    <Platforms>AnyCPU;x64</Platforms>

    </PropertyGroup>

    Then, check what SDKs you have installed. To do this, open the command prompt (Run -> cmd). In the command prompt, type

    dotnet --list-sdks

    This will list the SDKs you have installed. From here, you have 2 options:

    1. Change the .NET version in your .csproj code to one you have installed already.
    2. Download the SDK your .csproj is expecting to read.

    Once finished with either, restart Visual Studio and reopen your project. It should now load properly.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.