Creating Custom Build Task for building of NET7+ projects - having problems

Páll Björnsson 0 Reputation points
2024-02-08T14:35:46.67+00:00

I am trying to create a custom build task that will be invoked by overriding the "AfterResGen" target. The build task receives the list of files (FileWrites) and needs to go through all the build generated ".resources" files, deserialize them, manipulate them a bit and finally update them.

According to the Task documentation, the assembly containing the custom Task must be a netstandard2.0 project, so I've been trying that. However, reading and deserializing the ".resources" files (at that point on the obj folder since this is mid build) using either the ResourceReader type or the DeserializingResourceReader type, throw exception on every .resource file with "Stream is not a valid resource file". Debugging this down to the ResourceReader's exception, seems that some "MagicNumber" doesn't match.  For the record, I do have System.Resources.Extensions v8.0.0 Nuget referenced on that netstandard2.0 project.

I have also attempted to create the build task in a net8 assembly but that fails loading of the assembly during the build, complaining that System.Runtime v8.0.0 can't be found. Interestingly, the net8 build task is loaded successfully during build using "dotnet build" outside of VS, so I guess this problem has to do with the inability of VS to load build tasks from net8 assemblies. I tried using TaskFactory="TaskHostFactory" on the UsingTask element in the .targets file, but then it just complains about inability to load some other assemblies so that doesn't work either. At the moment, I'm stuck!

So, any thoughts on either a) How can I make the netstandard2.0 build task read the .resources files, deserialize and update during build or b) How can I make the net8 build task load and run when build is initiated from within Visual Studio ?

Palli        

Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
228 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 54,901 Reputation points
    2024-02-08T15:52:18.4466667+00:00

    VS doesn't build your app, MSBuild does. MSBuild is what hosts the build tasks and it is a NET 4.x app so you are limited to netstandard2.0 for compiled build tasks. dotnet is compiled against whatever the latest framework version is that you're targeting so you could use any compiled build task but of course only with dotnet.

    Can you please post the code you're using to try to read the resource file?

    Have you tried using your code in a standalone app and reading a binary resource file directly? This would help ensure your code is working against a valid resources file before introducing the complexity of build outputs.

    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.