RoslynCodeTaskFactory: The task factory "CodeTaskFactory" could not be loaded from the assembly

See original GitHub issue

Steps to reproduce

VS2019 win10 .net core console project v3.1 dotnet --version: 3.1.400-preview-015178

error MSB4175: The task factory “CodeTaskFactory” could not be loaded from the assembly “C:\Users\xx.nuget\packages\roslyncodetaskfactory\1.2.6\build\netstandard1.5\RoslynCodeTaskFactory.dll”. Method not found: ‘System.Xml.XmlNodeType System.Xml.Linq.XObject.get_NodeType()’.

Project file

<UsingTask
    TaskName="CustumCodeTask"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(RoslynCodeTaskFactory)"
    Condition=" '$(RoslynCodeTaskFactory)' != '' ">
    <ParameterGroup />
    <Task>
      <Reference Include="System.Xml" />
      <Using Namespace="System" />
      <Using Namespace="System.Collections.Generic" />
      <Using Namespace="System.Xml" />
      <Using Namespace="System.Xml.Linq" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[
			<!--public static void ScriptMain() {-->
				List<string> list = new List<string>();
				list.Add("Happy");
				list.Add("New");
				list.Add("Year");
				Console.WriteLine("Hello MSBuild Community Scripting World.");
				foreach(string s in list)
				{
				  Console.WriteLine(s);
				}
			<!--}-->
			]]>
      </Code>
    </Task>
  </UsingTask>

Command line

dotnet msbuild -v:d -t:CustumCodeTask

Expected behavior

see the messages

Actual behavior

error MSB4175: The task factory “CodeTaskFactory” could not be loaded from the assembly “C:\Users\xx.nuget\packages\roslyncodetaskfactory\1.2.6\build\netstandard1.5\RoslynCodeTaskFactory.dll”. Method not found: ‘System.Xml.XmlNodeType System.Xml.Linq.XObject.get_NodeType()’.

Environment data

msbuild /version output: Microsoft ® Build Engine version 16.6.0+5ff7b0c9e for .NET Framework

16.6.0.22303

OS info: win 10 x64 If applicable, version of the tool that invokes MSBuild (Visual Studio, dotnet CLI, etc):

dev console for VS2019 win10 .net core console project v3.1 (VS2019) dotnet --version: 3.1.400-preview-015178

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffklcommented, Jul 6, 2020

The RoslynCodeTaskFactory that you’re using is technically from here: https://github.com/jeffkl/RoslynCodeTaskFactory/

I’ve since built it into MSBuild so you don’t need to reference it as a package.

I think you just need to change the name of the task factory to RoslynCodeTaskFactory. The below sample works great:

<Project>
  <UsingTask
    TaskName="CustumCodeTask"
    TaskFactory="RoslynCodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
    <ParameterGroup />
    <Task>
      <Using Namespace="System" />
      <Using Namespace="System.Collections.Generic" />
      <Using Namespace="System.Xml" />
      <Using Namespace="System.Xml.Linq" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[
				List<string> list = new List<string>();
				list.Add("Happy");
				list.Add("New");
				list.Add("Year");
				Console.WriteLine("Hello MSBuild Community Scripting World.");
				foreach(string s in list)
				{
				  Console.WriteLine(s);
				}
			]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="Build">
    <CustumCodeTask />
  </Target>
</Project>
D:\Stuff>msbuild roslyncodetaskfactory.proj
Microsoft (R) Build Engine version 16.7.0-preview-20330-08+96403c598 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 7/6/2020 9:24:17 AM.
Hello MSBuild Community Scripting World.
Happy
New
Year

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.73
0reactions
Legendscommented, Jul 6, 2020

Ok, it works when I use:

"sdk": {
    "version": "3.1.301"
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

The task factory "CodeTaskFactory" could not be loaded ...
error MSB4175 : The task factory "CodeTaskFactory" could not be loaded from the assembly "C:\Users\xx.nuget\packages\roslyncodetaskfactory\1.2.6\ ...
Read more >
Error MSB4801: The task factory "CodeTaskFactory" is not ...
The error message is correct. Your UsingTask is trying to use .Net Framework which is not available. Update the UsingTask to use the ......
Read more >
The task factory "CodeTaskFactory" could not be loaded
Once I've changed this (locally) to RoslynCodeTaskFactory and $(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll things have started working ...
Read more >
MSB4175: The task factory 'task-factory-name' could not be ...
This error occurs when MSBuild encounters a problem during the creation of a task using a task factory.
Read more >
MSB4175: The task factory "CodeTaskFactory" could not be ...
The error message says that the system looks for the Microsoft.Build.Tasks.v12.0.dll file in the C:\Windows\Microsoft.NET\Framework64\v4.0.30319 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found