警告 MSB4056:必须在单线程单元上调用 MSBuild 引擎

Warning MSB4056: The MSBuild engine must be called on a single-threaded-apartment
2021-11-25
  •  译文(汉语)
  •  原文(英语)

尝试使用 C# 控制台应用程序构建我的 ASP.Net 解决方案时出现此错误:

MSBUILD:警告 MSB4056:必须在单线程单元上调用 MSBuild 引擎.当前线程模型是"MTA".继续,但某些任务可能无法正常运行.

这是我的代码:

Engine engine = new Engine();
engine.BinPath = @"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319";

FileLogger logger = new FileLogger();
logger.Parameters = @"logfile=C:\temp\build.log";
engine.RegisterLogger(logger);

bool success = engine.BuildProjectFile(ConfigurationSettings.AppSettings["ProjectPath"]);

engine.UnregisterAllLoggers();
if (success)
    Console.WriteLine("Build succeeded.");
else
    Console.WriteLine(@"Build failed. View C:\temp\build.log for details");
解决过程1

只需用[STAThread].

也就是说,如果您可以调用 COM 方法的任务,可能会导致的唯一问题.换句话说,忽略它通常是安全的.

I am getting this error while trying to build my ASP.Net solution using a C# console application:

MSBUILD : warning MSB4056: The MSBuild engine must be called on a single-threaded-apartment. Current threading model is "MTA". Proceeding, but some tasks may not function correctly.

This is my code:

Engine engine = new Engine();
engine.BinPath = @"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319";

FileLogger logger = new FileLogger();
logger.Parameters = @"logfile=C:\temp\build.log";
engine.RegisterLogger(logger);

bool success = engine.BuildProjectFile(ConfigurationSettings.AppSettings["ProjectPath"]);

engine.UnregisterAllLoggers();
if (success)
    Console.WriteLine("Build succeeded.");
else
    Console.WriteLine(@"Build failed. View C:\temp\build.log for details");
Solutions1

Just decorate the main method with [STAThread].

That said, the only issues that could cause if you can tasks that calls into COM methods. In other words, it is generally safe to ignore it.

转载于:https://stackoverflow.com/questions/15239507/warning-msb4056-the-msbuild-engine-must-be-called-on-a-single-threaded-apartmen

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入