Skip to content

Commit 2b178cd

Browse files
committed
fix: CLI debug 不工作
1 parent a9b578e commit 2b178cd

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

MaiChartManager.CLI/Commands/DebugCommand.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,29 @@ public class DebugCommand : Command
66
{
77
public override int Execute(CommandContext context, CancellationToken cancellationToken)
88
{
9-
Program.Main();
9+
// WebView2 (COM) 要求 STA 线程;CLI 的 async 入口运行在 MTA 上下文中,
10+
// 而 Program.Main 上的 [STAThread] 作为普通方法调用时不生效,需手动建 STA 线程
11+
Exception? exception = null;
12+
var thread = new Thread(() =>
13+
{
14+
try
15+
{
16+
Program.Main();
17+
}
18+
catch (Exception e)
19+
{
20+
exception = e;
21+
}
22+
});
23+
thread.SetApartmentState(ApartmentState.STA);
24+
thread.Start();
25+
thread.Join();
26+
27+
if (exception != null)
28+
{
29+
Console.Error.WriteLine($"发生错误: {exception}");
30+
throw exception;
31+
}
1032
return 0;
1133
}
1234
}

MaiChartManager/ServerManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public static void StartApp(bool export, Action<string>? onStart = null)
136136
}
137137
)
138138
.AddControllers()
139+
.AddApplicationPart(typeof(ServerManager).Assembly)
139140
.AddJsonOptions(options =>
140141
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
141142

0 commit comments

Comments
 (0)