【问题标题】:How to build .Net Core Application from C# code using "BuildManager"?如何使用“BuildManager”从 C# 代码构建 .Net Core 应用程序?
【发布时间】:2019-10-21 21:04:20
【问题描述】:

我想从 C# 代码构建 .Net Core 2.1 控制台应用程序。我正在使用 BuildManager 构建解决方案。

               using (var buildManager = new BuildManager())
                {
                    var bp = new BuildParameters(projectCollection)
                    {
                        Loggers = new List<ILogger>
                        {
                            logger
                        },
                        OnlyLogCriticalEvents = false,
                        DetailedSummary = true,
                        NodeExeLocation = @"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\MSBuild\\Current\\Bin\\MSBuild.exe"
                    };

                    var buildRequest = new BuildRequestData(
                        "projectFullPath",
                        globalProperties, null, new[] { "Build" }, null,
                        BuildRequestDataFlags.ReplaceExistingProjectInstance);

                    BuildSubmission submission = null;

                    buildManager.BeginBuild(bp);
                    submission = buildManager.PendBuildRequest(buildRequest);

                    buildResult = submission.Execute();
                    buildManager.EndBuild();

我已经设置了这样的环境变量:

        Environment.SetEnvironmentVariable("MSBuildExtensionsPath", globalProperties["MSBuildExtensionsPath"]);
        Environment.SetEnvironmentVariable("MSBuildFrameworkToolsPath", globalProperties["MSBuildFrameworkToolsPath"]);
        Environment.SetEnvironmentVariable("MSBuildToolsPath32", globalProperties["MSBuildToolsPath32"]);
        Environment.SetEnvironmentVariable("MSBuildSDKsPath", globalProperties["MSBuildSDKsPath"]);
        Environment.SetEnvironmentVariable("RoslynTargetsPath", globalProperties["RoslynTargetsPath"]);

其中 globalProperties:

            Path.Combine(programFilesX86, @"Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\msbuild.exe"),
            Path.Combine(programFilesX86,
                @"Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe"),
            Path.Combine(programFilesX86, @"Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe"),
            Path.Combine(programFilesX86, @"Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe"),
            Path.Combine(programFilesX86, @"MSBuild\14.0\Bin\MSBuild.exe"),
            Path.Combine(programFilesX86, @"MSBuild\12.0\Bin\MSBuild.exe")

现在我有构建错误。

    Build FAILED.

    MSBUILD : warning MSB4196: The "*.overridetasks" files could not be successfully loaded from their expected location "C:\Learn\NetCoreApplicationTestingBuild\NetCoreApplicationTestingBuild\bin\Debug\netcoreapp2.1". Default tasks will not be overridden. 
    MSBUILD : warning MSB4010: The "*.tasks" files could not be successfully loaded from their expected location "C:\Learn\NetCoreApplicationTestingBuild\NetCoreApplicationTestingBuild\bin\Debug\netcoreapp2.1". Default tasks will not be available.
    C:\Learn\NetCoreTestBuild2\NetCoreTestBuild2.sln.metaproj : error MSB4036: The "Message" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the "C:\Learn\NetCoreApplicationTestingBuild\NetCoreApplicationTestingBuild\bin\Debug\netcoreapp2.1" directory.

    2 Warning(s)
    1 Error(s)

【问题讨论】:

  • 看起来错误信息告诉你在哪里检查什么,你检查了吗?错误消息为:error MSB4036: The "Message" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with &lt;UsingTask&gt; in the project file, or in the *.tasks files located in the "C:\Learn\NetCoreApplicationTestingBuild\NetCoreApplicationTestingBuild\bin\Debug\netcoreapp2.1" directory.
  • @JamieTaylor 我没有创建任何任务,我的项目中也没有任何任务。我的目标是使用 Build Manager 从 c# 代码构建 .Net Core Application,当我尝试执行此操作时出现此错误。也许,你知道如何从代码构建 .Net Core。我将非常感谢任何帮助。谢谢。

标签: c# .net-core msbuild


【解决方案1】:

我能够通过安装“Microsoft.Build.Runtime”nuget 包来解决这个问题,该包解决了丢失的 *.overridestasks 和 *.tasks 错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多