Build Automation Management using MSBuild in Windows Services

Source: Internet
Author: User

Microsoft Build Engine (MSBuild) is a new generation platform for Microsoft and Visual Studio. It enables developers to compile without installing Visual Studio.

A Windows Service application is an application that requires long-term running. It does not have a user interface, and no visual output is generated (any user message is written to Windows event logs or other custom logs ). Therefore, it is particularly suitable for server environments. When the computer starts, Windows Service will automatically start running (you need to set the Service type to Automatic), they do not need to be logged on.

Build Automation Management is to standardize the automated compilation process of enterprises. The combination of Windows Service and MSBuild enables enterprise Automation Build. Build Automation Management can process scheduled Build or unscheduled build.

Create a Windows Service

Set Windows Service Account to User. Other accounts may not be authorized to use Proces.

Add Build Code to Windows Service.

MSBuild.exe is located in the % SystemRoot % \ Micorsoft. NET \ Framework \ V2.0.502727 directory. Therefore, obtain the so-called absolute path of the directory.

String systemRoot = Path. GetDirectoryName (Environment. SystemDirectory );

String FrameworkPath = systemRoot + @ "\ Microsoft. NET \ Framework ";

If (! Directory. Exists (FrameworkPath ))

{

Throw new DirectoryNotFoundException ("The framework directory cocould not be located .");

}

String msBuildDir = FrameworkPath + @ "\ v2.0.50727 ";

 

Obtain the setting options for compilation. For example, you need to compile the Debug and Release versions. And store these options in a temporary variable.

String [] configs = new string [] {"Debug", "Release ",};

Obtain the project path and call MSBuild for compilation.

String projectPath = @ "e: \ temp \ test. sln ";

EventLog1.WriteEntry ("Start Build ");

Foreach (string str in configs)

{

Process process = new Process ();

Process. StartInfo. FileName = msBuildDir + @ "\ msbuild.exe ";

Process. StartInfo. Arguments = projectPath + @ "/p: Configuration =" + str + "\"";

Console. WriteLine (process. StartInfo. Arguments );

Process. Start ();

}
The complete code is as follows:

String systemRoot = Path. GetDirectoryName (Environment. SystemDirectory );
String FrameworkPath = systemRoot + @ "\ Microsoft. NET \ Framework ";
If (! Directory. Exists (FrameworkPath ))
{
Throw new DirectoryNotFoundException ("The framework directory cocould not be located .");
}
String msBuildDir = FrameworkPath + @ "\ v2.0.50727 ";
String [] configs = new string [] {"Debug", "Release };
String projectPath = @ "e: \ temp \ test. sln ";
EventLog1.WriteEntry ("Start Build ");
Foreach (string str in configs)
{
Process process = new Process ();
Process. StartInfo. FileName = msBuildDir + @ "\ msbuild.exe ";
Process. StartInfo. Arguments = projectPath + @ "/p: Configuration =" + str + "\"";
Console. WriteLine (process. StartInfo. Arguments );
Process. Start ();
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.