How to fix "WriteCodeFragment" error when build .net core

Soaad Abdelaziz 40 Reputation points
2023-06-09T16:32:20.0833333+00:00

When I run .net core api project "6.0" on mac I got this error

"

usr/local/share/dotnet/sdk/7.0.302/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.GenerateAssemblyInfo.targets(5,5): Error MSB4018: The "WriteCodeFragment" task failed unexpectedly.
System.IO.FileLoadException: Could not load file or assembly 'System.CodeDom, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621)
File name: 'System.CodeDom, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
 ---> System.IO.FileLoadException: Could not load file or assembly 'System.CodeDom, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyBinder, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Microsoft.Build.Locator.MSBuildLocator.<>c__DisplayClass15_0.<RegisterMSBuildPath>g__TryLoadAssembly|3(AssemblyName assemblyName)
   at Microsoft.Build.Locator.MSBuildLocator.<>c__DisplayClass15_0.<RegisterMSBuildPath>b__2(AssemblyLoadContext _, AssemblyName assemblyName)
   at System.Runtime.Loader.AssemblyLoadContext.GetFirstResolvedAssemblyFromResolvingEvent(AssemblyName assemblyName)
   at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingEvent(AssemblyName assemblyName)
   at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
   at Microsoft.Build.Tasks.WriteCodeFragment.GenerateCode(String& extension)
   at Microsoft.Build.Tasks.WriteCodeFragment.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) (MSB4018) (Redis_Demo)

" I had many version of .net core on my mac ,please I need your support

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,187 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 56,686 Reputation points
    2023-06-09T17:03:12.7266667+00:00

    you can list the sdk and the runtimes:

    dotnet --list-sdks
    dotnet --list-runtimes

    the build error implies you are building with net sdk 7.0.302 (same as my Mac) but the target runtime is missing some files.

    see the latest runtime version net 6 (my Mac its 6.0.16). be sure your have a matching set of the latest:

    Microsoft.AspNetCore.App 6.0.16 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
    Microsoft.NETCore.App 6.0.16 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

    the fix is install the latest net 6. (you might delete any existing .net 6 runtimes)

    more more help, show the output of the lists and the project file.

    dotnet --version
    dotnet --list-sdks
    dotnet --list-runtimes

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful