1
我需要編譯啓用預編譯的ASP.Net Web應用程序(Web窗體和MVC)。我使用Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace來打開項目併發出項目級別的程序集。我已經確定RazorGenerator可以用於預編譯,但這會給我的解決方案增加額外的複雜性,有沒有辦法簡單地使用Roslyn Workspace?如何使用Roslyn Workspace預編譯項目
我需要編譯啓用預編譯的ASP.Net Web應用程序(Web窗體和MVC)。我使用Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace來打開項目併發出項目級別的程序集。我已經確定RazorGenerator可以用於預編譯,但這會給我的解決方案增加額外的複雜性,有沒有辦法簡單地使用Roslyn Workspace?如何使用Roslyn Workspace預編譯項目
我想出了下面這段代碼來生成預編譯構建,但是在沒有很多文件級操作的情況下尋找更強大的方法。
static void Main(string[] args)
{
//MVC APP
string projectFileName = @"C:\MVCWebApplication\MVCWebApplication\MVCWebApplication.csproj";
string appName = "MVCWebApplication";
//ASP.NET Web Forma App
//string projectFileName = @"C:\AapApp\WebGoat\WebGoat.NET.csproj";
//string appName = "WebGoat.NET";
//Build Artifacts will be drop to this location
string publishDrop = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
//aspnet compiler will drop precompiled artifacts to this location
string precompiledDrop = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
//Publishing the extension without precompile
var pc = new ProjectCollection();
var globalProperty = new Dictionary<string, string>();
globalProperty.Add("Configuration", "Debug");
globalProperty.Add("Platform", "x86");
globalProperty.Add("OutputPath", publishDrop);
globalProperty.Add("WebPublishMethod", "FileSystem");
globalProperty.Add("EnableUpdateable", "false");
globalProperty.Add("DebugSymbols", "true");
globalProperty.Add("VisualStudioVersion", "14.0");//TODO: Get from IDE
var buidlRequest = new BuildRequestData(projectFileName, globalProperty, null, new string[] { "Build" }, null);
var buildResult = BuildManager.DefaultBuildManager.Build(new BuildParameters(pc), buidlRequest);
//If build errors; then trow
if (buildResult.Exception != null)
{
throw buildResult.Exception;
}
//Rslyn folder not getting created in required location in VS 2015 MVC app template
//https://stackoverflow.com/questions/32780315/could-not-find-a-part-of-the-path-bin-roslyn-csc-exe
//Moving it to proper location manually
string publish = Path.Combine(publishDrop, "_PublishedWebsites", appName);
if (!Directory.Exists(Path.Combine(publish, "bin", "Roslyn")) &&
Directory.Exists(Path.Combine(publishDrop, "Roslyn")))
{
Directory.Move(Path.Combine(publishDrop, "Roslyn"), Path.Combine(publish, "bin", "Roslyn"));
}
//Executing aspnet compiler to get the final output
using (var cmd = new Process())
{
cmd.StartInfo.FileName = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe";
cmd.StartInfo.Arguments = @"-v/-p " + publishDrop + @"\_PublishedWebsites\" + appName + " " + precompiledDrop;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
//TODO: Check result for any precompilation errors,
}
//Cleanup files in final precopiled artifact drop
if (Directory.Exists(Path.Combine(precompiledDrop, "bin", "Roslyn")))
{
Directory.Delete(Path.Combine(precompiledDrop, "bin", "Roslyn"), true);
}
Console.ReadKey();
}
此代碼可以正常工作併發出預期的吞吐量,但是有沒有更強大的方法來執行此操作? – Bandara
MSBuildWorkspace是一個Roslyn工作區。我懷疑,儘管RazorGenerator正在爲您的項目中的內容生成額外的源代碼,因此從MSBuildWorkspace發佈編譯文件不太可能奏效,因爲它不具備所有源代碼。 –
@MattWarren:是的Matt Warren,我必須使用MSBuild將web應用程序發佈到一個文件夾中,並使用aspnet_comiler.exe預編譯發佈的工件(請參閱我的答案)。此方法會生成所需的二進制文件,但是我正在爲此尋找更強大的代碼片段,可能是爲了避免MSBuild和aspnet_compiler.exe – Bandara