首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用"BuildManager“从C#代码构建.Net核心应用程序?

如何使用"BuildManager“从C#代码构建.Net核心应用程序?
EN

Stack Overflow用户
提问于 2019-06-06 18:44:45
回答 1查看 303关注 0票数 4

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

代码语言:javascript
复制
               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();

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

代码语言:javascript
复制
        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:

代码语言:javascript
复制
            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")

现在我有了构建错误。

代码语言:javascript
复制
    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)
EN

回答 1

Stack Overflow用户

发布于 2021-04-21 06:14:06

我能够通过安装'Microsoft.Build.Runtime‘nuget包来解决这个问题,这个包解决了缺少的*.overridestasks和*.tasks错误

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56475971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档