0

Recently I used Microsoft.Build package with VS2017 for building C# project programmatically. However, I always got BuildResultCode.Failure. Cannot figure out why, need help, thanks. Here's my code:

string projectFileName = @"C:\Downloads\ToBeBuilt\ToBeBuilt\ToBeBuilt.csproj";

ProjectCollection pc = new ProjectCollection();
pc.SetGlobalProperty("Configuration", "Debug");
pc.SetGlobalProperty("Platform", "x86");
pc.SetGlobalProperty("OutDir", @"C:\Downloads\Artifacts");

Dictionary<string, string> globalProperty = new Dictionary<string, string>();
globalProperty.Add("Configuration", "Debug");
globalProperty.Add("Platform", "x86");
globalProperty.Add("OutDir", @"C:\Downloads\Artifacts");

BuildRequestData BuidlRequest = new BuildRequestData(projectFileName, globalProperty, "15.0", new string[] { "Build" }, null);
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(new BuildParameters(pc), BuidlRequest);
BuildResultCode a = buildResult.OverallResult;

And here is the target project to be built:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  ...
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x86\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    ...
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Target Name="Build">  
    <Csc Sources="@(Compile)"/>    
  </Target> 
</Project>
John Wang
  • 29
  • 5
  • Check the `buildResult.Exception` property. What is it? – kennyzx Jul 20 '18 at 06:14
  • If possible, please check following thread https://stackoverflow.com/questions/27802603/building-a-sln-in-c-sharp-executable/27807830#27807830 – Joy Jul 20 '18 at 08:18
  • @Alex thanks for the help! How should I mark the answer? Actually, there're multiple causes leading to the failure of build. I followed the post you gave and kept debugging with the log file MSBuild provided, I finally made it. – John Wang Jul 22 '18 at 10:45
  • Since there are multiple possible causes to the failure, you should provide the detailed exception message to help the investigation, otherwise the question can not be answered. – kennyzx Jul 25 '18 at 01:49

1 Answers1

0

Always got BuildResultCode.Failure from Microsoft.Build library

You can check the similar issue to kept debugging with the log file MSBuild provided:

string projectFileName = Directory.GetCurrentDirectory() + "\\build\\Solution.sln";
ProjectCollection pc = new ProjectCollection();
Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();
GlobalProperty.Add("Configuration", "Release");
GlobalProperty.Add("Platform", "Any CPU");
GlobalProperty.Add("OutputPath", Directory.GetCurrentDirectory() + "\\build\\\bin\\Release");

BuildParameters bp = new BuildParameters(pc);
bp.Loggers = new[] {
  new FileLogger
  {
    Verbosity = LoggerVerbosity.Detailed,
    ShowSummary = true,
    SkipProjectStartedText = true
  }
};

BuildManager.DefaultBuildManager.BeginBuild(bp);

BuildRequestData BuildRequest = new BuildRequestData(projectFileName, GlobalProperty, null, new string[] { "Build" }, null);

BuildSubmission BuildSubmission = BuildManager.DefaultBuildManager.PendBuildRequest(BuildRequest);
BuildSubmission.Execute();
BuildManager.DefaultBuildManager.EndBuild();
if (BuildSubmission.BuildResult.OverallResult == BuildResultCode.Failure)
  {
    throw new Exception();
  }
}

Certify:Building a .sln in C# executable

Joy
  • 1,171
  • 9
  • 15