Windows Services中使用MSBuild實現Build Automation Management

來源:互聯網
上載者:User

Microsoft Build Engine (MSBuild)是Microsoft和Visual Studio的新的產生平台。它使得開發人員能夠在沒有安裝Visual Studio 的環境下進行編譯。

Windows Service應用程式是一種需要長期啟動並執行應用程式,它沒有使用者介面,並且也不會產生任何可視化的輸出(任何使用者訊息都會被寫入Windows事件記錄或者其它自訂的日誌中)。因此,它特別適合於伺服器環境。在電腦啟動時,Windows Service就會自動開始運行(需要設定服務類型為Automatic),它們不需要使用者一定登入才行。

Build Automation Management是為了標準化企業的自動化編譯過程。通過Windows Service與MSBuild的結合實現企業的Automation Build。Build Automation Management可以處理scheduled  Build,也可以處理unscheduled build。

建立Windows Service

將Windows Service的Account設定為User。其它賬戶可能無權使用Proces。

在Windows Service中添加Build Code。

MSBuild.exe位於%SystemRoot%\Micorsoft .NET\Framework\V2.0.502727目錄下。因此,擷取該目錄所謂的絕對路徑。

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

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

            if (!Directory.Exists(FrameworkPath))

            {

                throw new DirectoryNotFoundException("The framework directory could not be located.");

            }

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

 

獲得所需要編譯的設定選項。例如,需要編譯Debug和Release兩個版本。並將這些選項存放在一個臨時變數中。    

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

獲得項目路徑並調用MSBuild進行編譯。

            string projectPath = @"e:\temp\test\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();

            }       
完整代碼如下:

  string systemRoot = Path.GetDirectoryName(Environment.SystemDirectory);
            string FrameworkPath = systemRoot + @"\Microsoft.NET\Framework";
            if (!Directory.Exists(FrameworkPath))
            {
                throw new DirectoryNotFoundException("The framework directory could not be located.");
            }
            string msBuildDir = FrameworkPath + @"\v2.0.50727";         
            string[] configs = new string[] { "Debug", "Release};        
            string projectPath = @"e:\temp\test\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();
            }        
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.