0

I'm working on a service that will download a .NET solution from a repository and build it (not unlike a continuous-integration build service). What I want to know is, using MSBuild programmatically via the Microsoft.Build namespace classes, can I can load the solution and project(s) into memory and build it without first saving them to disk in a temporary folder?

I'm still learning MSBuild and trying things, but I figure someone on Stack Overflow has tried this and has some insight.

Adam Maras
  • 26,269
  • 6
  • 65
  • 91

2 Answers2

3

I can't speak to whether this is a good idea or not, but it's possible to do.

ProjectInstance has a constructor that accepts a ProjectRootElement, which can be constructed via the Create(XmlReader) method. And as you may know, XmlReader can be attached to various Streams including a MemoryStream.

Here's how something like this may look:

var xmlReader = XmlTextReader.Create([your existing memory stream]);
var project = ProjectRootElement.Create(xmlReader);

var buildParams = new BuildParameters();
var buildData = new BuildRequestData(new ProjectInstance(project),
    new string[] { "Build", "Your Other Target Here" });

var buildResult = BuildManager.DefaultBuildManager.Build(buildParams, buildData);
Rafael Rivera
  • 871
  • 8
  • 22
  • +1 just because you solved the first half of the problem. The second half would be the actual code files with the projects that are to be compiled/linked/resource'd/etc.. is there any way to manually resolve them instead of having the tools look for them on disk? – Adam Maras Oct 19 '12 at 22:50
  • Yeah, I just realized the actual resources linked to via the project would still be on disk... hmm. – Rafael Rivera Oct 19 '12 at 22:51
  • Perhaps one way to attack this is to have a common (read-only?) network share that project files can reference. – Rafael Rivera Oct 19 '12 at 22:54
  • 1
    Doesn't quite fit my architecture. I think I need to dig in with Reflector and see how exactly what's going on under the hood. Perhaps I'll be able to do something similar to a [custom URI registration](http://stackoverflow.com/q/7862128/180043) as a root directory to be able to load files with custom code. – Adam Maras Oct 19 '12 at 23:02
  • 1
    @AdamMaras to be honest, that answers your _question_ perfectly, you didn't mention that you have additional requirements for the location of source files. – skolima Oct 20 '12 at 12:27
1

After researching MSBuild and all the involved components, it appears as though it's necessary to have the entire project set up on the file system before being able to build it. Unfortunately, what I'm trying to do just can't be done with the provided toolset.

Adam Maras
  • 26,269
  • 6
  • 65
  • 91