3
votes

I have a very simple console project:

class Program
{
    static void Main(string[] args)
    {
        var project = new Project(
            "FishStory.csproj", 
            null, 
            null, 
            new ProjectCollection());

        Console.ReadLine();
    }
}

This is a .NET Core 3.0 console application, and it has Microsoft.Build (16.4.0) NuGet package referenced.

The .csproj file FishStory.csproj is found (I don't get an exception that the .csproj is missing), but I do get the following error.

Microsoft.Build.Exceptions.InvalidProjectFileException: 'The imported project "C:\Users\vchel\source\repos\ForDave\ForDave\bin\Debug\netcoreapp3.0\Microsoft.CSharp.targets" was not found. Confirm that the expression in the Import declaration "C:\Users\vchel\source\repos\ForDave\ForDave\bin\Debug\netcoreapp3.0\Microsoft.CSharp.targets" is correct, and that the file exists on disk. C:\Users\vchel\source\repos\ForDave\ForDave\bin\Debug\netcoreapp3.0\FishStory.csproj'

I'd expect that such a simple MSBuild test would "just work", but it seems like I'm missing something. What can I do to load this .csproj file?

2

2 Answers

1
votes

It seems the NuGet package adds the necessary .dlls to use the Project object, but the various project types must have .target and .props files which are used when the Project .csproj is loaded.

To load my specific project, I had to add the following files to the output directory. I accomplished this by placing the files in my project and marking them as Copy if Newer.

I added the following files:

  • Microsoft.Common.targets
  • Microsoft.CSharp.targets
  • Microsoft.NETFramework.props
  • Microsoft.NETFramework.targets

In my case the .csproj is a MonoGame project, so I also had to add:

  • MonoGame.Build.Tasks.dll (not sure if I needed this or not)
  • MonoGame.common.props
  • MonoGame/v3.0/MonoGame.Content.Builder.targets

To add these files so they are copied to the output folder:

  1. Add the file to your project (for .NET Core you just have to add the file to the directory)
  2. Right-click on the file in the Solution Explorer and select Properties
  3. Set the Copy to Output Directory to Copy if newer

I pulled the targets/props/.dll files from:

  • C:\Windows\Microsoft.NET\Framework64\v4.0.30319
  • C:\Program Files (x86)\MSBuild\MonoGame\v3.0

I am guessing that other project types (such as an Android Xamarin project) may require different .targets files.

Finally, I also had to manually add the NuGet Package Microsoft.Build.Utilities.Core. Not sure why that wasn't automatically added when adding Microsoft.Build

Solution Explorer Screenshot

1
votes

I have catched the same exception while I have tried to load a project targeted to netcore31 platform.
But after that I have installed

  • Microsoft.Build.Utilities.Core(16.5.0)
  • Microsoft.Build (16.4.0)

nuget packages, there is no more the exception. The project is loaded successfully.

Maybe, it will help someone more.