Adam Maras
Adam Maras

Reputation: 26883

Can MSBuild (via Microsoft.Build namespace) load a project from memory?

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.

Upvotes: 0

Views: 493

Answers (2)

Adam Maras
Adam Maras

Reputation: 26883

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.

Upvotes: 1

Rafael Rivera
Rafael Rivera

Reputation: 916

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);

Upvotes: 3

Related Questions