问题
I want to build asp.net core project using another C# project. Here is the sample code. ( I am using Visual Studio 2015)
var projectCollection = new Microsoft.Build.Evaluation.ProjectCollection(Microsoft.Build.Evaluation.ToolsetDefinitionLocations.Registry);
projectCollection.DefaultToolsVersion = "14.0";
projectCollection.RegisterLogger(new Microsoft.Build.Logging.ConsoleLogger(Microsoft.Build.Framework.LoggerVerbosity.Detailed));
var project = projectCollection.LoadProject(@"H:\Development\WebApplication22\src\WebApplication22\WebApplication22.xproj");
bool b = project.Build();
It gives me many errors. Following is one of them.
The "GetTransitiveNonDotNetProjectDependencies" task could not be loaded from the assembly C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Tasks.dll. Method 'get_BuildEngine' in type 'Microsoft.DotNet.Tasks.KProcess' from assembly 'Microsoft.DotNet.Tasks, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' does not have an implementation. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. Done building target "_GetDependencyFragmentFiles" in project "WebApplication22. xproj" -- FAILED.
Note : If I am using Visual Studio Command Prompt and use MSbuild then it works fine.
回答1:
No for now. When you run msbuild from Visual Studio Command Prompt to build a asp.net core project, it will call dotnet.exe to build.
MS is working on moving from .xproj/project.json to .csproj/MSBuild currently and most possible to be available in Visual Studio 15 RTM. You can refer to this link for more information: Changes to Project.json.
The first wave of this change will happen in the Visual Studio “15” RTM: when opening any .NET Core project in Visual Studio, it will automatically convert from .xproj to .csproj, moving the assets from the project.json file into configuration files and the .csproj file. We will also provide a tool for converting applications using the .NET command line tools as well.
来源:https://stackoverflow.com/questions/38934699/is-it-possible-to-build-asp-net-core-project-using-msbuild-from-code