3

I'm writing a console application to get a solution from a tfs server, build it and publish on iis, but I'm stuck at building...

I found this code, which works like a charm

public static void BuildProject()
{
    string solutionPath = Path.Combine(@"C:\MySolution\Common\Common.csproj");

    List<ILogger> loggers = new List<ILogger>();
    loggers.Add(new ConsoleLogger());
    var projectCollection = new ProjectCollection();
    projectCollection.RegisterLoggers(loggers);
    var project = projectCollection.LoadProject(solutionPath);
    try
    {
        project.Build();
    }
    finally
    {
        projectCollection.UnregisterAllLoggers();
    }
}

but my solution it's pretty big and contains multiple projects which depends from each other (e.g. project A has a reference to project B)

how to get the correct order to build each project? is there a way to build the entire solution from the .sln file?

Doc
  • 5,078
  • 6
  • 52
  • 88
  • If they're all in the same sollution, why would you link to eachother's dll rather than adding a project reference? – Kippie Mar 06 '14 at 14:48
  • i mean that them are linked between each other, so i can't build a project without building another one before – Doc Mar 06 '14 at 14:51
  • Is there a specific reason why you need to code this buildrunner instead of just calling [MSBuild](http://msdn.microsoft.com/en-us/library/0k6kkbsd.aspx) to build your solution? Together with the TFS PowerShell Cmdlets you'll have anything you need. – Filburt Mar 06 '14 at 14:51
  • I want to know if there's a way to do this from code for learning reasons... If there's not, I will use `devenv /build Debug Solution.sln` – Doc Mar 06 '14 at 14:55

1 Answers1

9

Try using the following code to load a solution and compile it:

string projectFilePath = Path.Combine(@"c:\solutions\App\app.sln");

ProjectCollection pc = new ProjectCollection();

// THERE ARE A LOT OF PROPERTIES HERE, THESE MAP TO THE MSBUILD CLI PROPERTIES
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
globalProperty.Add("OutputPath", @"c:\temp");

BuildParameters bp = new BuildParameters(pc);
BuildRequestData buildRequest = new BuildRequestData(projectFilePath, globalProperty, "4.0", new string[] { "Build" }, null);
// THIS IS WHERE THE MAGIC HAPPENS - IN PROCESS MSBUILD
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, buildRequest);
// A SIMPLE WAY TO CHECK THE RESULT
if (buildResult.OverallResult == BuildResultCode.Success)
{              
    //...
}
Erwin
  • 3,060
  • 26
  • 24
  • yeah, i found that code on http://bogdangavril.wordpress.com/2012/03/15/take-control-of-msbuild-using-msbuild-api/ me too... but it doesn't work. It fails always, even if i can build manaully the solution from visual studio ide – Doc Mar 06 '14 at 15:05
  • Are all your used projects in your solution? Did you add all your referenced dll's (from your own projects in the solution) through Add References/Projects? I've compiled a rather big solution like this ... – Erwin Mar 06 '14 at 15:14
  • In your example your compiling a csproj not a sln, typo? – Erwin Mar 06 '14 at 15:16
  • it's not a typo, i can build a single project with that code but i want to build the whole solution at once, if possible – Doc Mar 06 '14 at 15:27
  • I used the sln and that worked, did you try adding a logger to see which error the build is throwing? – Erwin Mar 06 '14 at 15:43
  • 1
    Did you check your GlobalProperties, for example wrong use of Platform (if you use x86 instead of Any CPU) can cause build to fail. – Erwin Mar 06 '14 at 15:52
  • Try GlobalProperty.Add("Platform", "Any CPU"); with a space between Any and CPU. – Erwin Mar 07 '14 at 07:37
  • 1
    i found what it may cause the error: the output path is a "global" /bin/debug. but i need to have a /bin/debug folder for each project in my solution – Doc Mar 07 '14 at 09:44