Unable to build C# Solution using MSBuild programmatically

95 Views Asked by At

i want to build a C# Solution (.sln) using MSBuild from another C# program. I am using the Microsoft.Build API, however i could not make it work so far.

Please see below the code to build the solution:

 private void BuildSolution(string solutionPath)
    {
        BuildResult result = null;

        using (var pc = new ProjectCollection())
            result = BuildManager.DefaultBuildManager.Build(
                new BuildParameters(pc) { Loggers = new[] { new FileLogger() {
                    Parameters = SolverConfig.Path + "build.log",
                    Verbosity = LoggerVerbosity.Diagnostic,
                    SkipProjectStartedText = true,
                } } },
                // Change this path to your .sln file instead of the .csproj.
                // (It won't work with the .csproj.)
                new BuildRequestData(SolverConfig.Path + solutionPath,
                    // Change the parameters as you need them,
                    // e.g. if you want to just Build the Debug (not Rebuild the Release).
                    new Dictionary<string, string>
                    {
                        { "Configuration", "Release" },
                        { "Platform", "Any CPU" }
                    }, "6.0", new[] { "Rebuild" }, null));

        if (result.OverallResult == BuildResultCode.Failure)
            throw new Exception("Solver build failed");

    }

Here is the log file data after trying to build the solution:

Target "ValidateSolutionConfiguration" in file "\ProjetosSolver\BILS_Solver\BILS_Solver.sln.metaproj":
  Task "Error" skipped, due to false condition; (('$(CurrentSolutionConfigurationContents)' == '') and ('$(SkipInvalidConfigurations)' != 'true')) was evaluated as (('<SolutionConfiguration>
    <ProjectConfiguration Project="{73067654-AEC8-4FA1-98E7-E5C5C93DC264}" AbsolutePath="\ProjetosSolver\BILS_Solver\BILS_Solver.csproj" BuildProjectInSolution="True">Debug|AnyCPU</ProjectConfiguration>
    <ProjectConfiguration Project="{D535A4DF-2FF6-4A10-8CC0-EE3EBB8EE520}" AbsolutePath="\ProjetosSolver\BILS_Commons\BILS_Commons.csproj" BuildProjectInSolution="True">Debug|AnyCPU</ProjectConfiguration>
    <ProjectConfiguration Project="{FC94B850-3E0D-455D-97D4-A84BDC43712B}" AbsolutePath="\ProjetosSolver\BILS_Evaluator\BILS_Evaluator.csproj" BuildProjectInSolution="True">Debug|AnyCPU</ProjectConfiguration>
    <ProjectConfiguration Project="{A6D612E3-DB96-4CA8-A69F-BD77E7001A25}" AbsolutePath="\ProjetosSolver\bmhf\BMHF\BMHF.csproj" BuildProjectInSolution="True">Debug|AnyCPU</ProjectC...' == '') and ('' != 'true')).
  Task "Warning" skipped, due to false condition; (('$(CurrentSolutionConfigurationContents)' == '') and ('$(SkipInvalidConfigurations)' == 'true')) was evaluated as (('<SolutionConfiguration>
    <ProjectConfiguration Project="{73067654-AEC8-4FA1-98E7-E5C5C93DC264}" AbsolutePath="\ProjetosSolver\BILS_Solver\BILS_Solver.csproj" BuildProjectInSolution="True">Debug|AnyCPU</ProjectConfiguration>
    <ProjectConfiguration Project="{D535A4DF-2FF6-4A10-8CC0-EE3EBB8EE520}" AbsolutePath="\ProjetosSolver\BILS_Commons\BILS_Commons.csproj" BuildProjectInSolution="True">Debug|AnyCPU</ProjectConfiguration>
    <ProjectConfiguration Project="{FC94B850-3E0D-455D-97D4-A84BDC43712B}" AbsolutePath="\ProjetosSolver\BILS_Evaluator\BILS_Evaluator.csproj" BuildProjectInSolution="True">Debug|AnyCPU</ProjectConfiguration>
    <ProjectConfiguration Project="{A6D612E3-DB96-4CA8-A69F-BD77E7001A25}" AbsolutePath="\ProjetosSolver\bmhf\BMHF\BMHF.csproj" BuildProjectInSolution="True">Debug|AnyCPU</ProjectC...' == '') and ('' == 'true')).
  MSBUILD : warning MSB4196: The "*.overridetasks" files could not be successfully loaded from their expected location "\src\WebUI\bin\Debug\net6.0". Default tasks will not be overridden. 
  MSBUILD : warning MSB4010: The "*.tasks" files could not be successfully loaded from their expected location "\src\WebUI\bin\Debug\net6.0". Default tasks will not be available. 
  \ProjetosSolver\BILS_Solver\BILS_Solver.sln.metaproj : error MSB4036: The "Message" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the "\src\WebUI\bin\Debug\net6.0" directory.
Done building target "ValidateSolutionConfiguration" in project "BILS_Solver.sln" -- FAILED.

Done building project "BILS_Solver.sln" -- FAILED.

I have tried to compile the same project from the command line using dotnet build and MSBuild.exe and it worked just fine.

I have also tried to use the following code:

private void BuildSolution(string solutionPath)
{
    Project project = new Project(SolverConfig.Path + solutionPath);
    project.Build();
}

Which resulted in the following error: error MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found.

Is there something wrong with the code that i am using? How can i solver error MSB4036: The "Message" task was not found.? Is there a better way to build a Solution programmatically from another C# project?

Thank you very much for your time!

0

There are 0 best solutions below