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