.net 如何以编程方式启动Blazor Server App?

4ngedf3f  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(228)

是否可以通过控制台项目或单击Windows应用程序中的按钮以编程方式在本地计算机上启动Blazor Server App?
有些人喜欢在VS中打开Blazor Server App项目,然后单击“调试”按钮。

qjp7pelc

qjp7pelc1#

是的,下面是一个虚拟的例子在一个新的线程启动它,它可以在同一个线程启动的进程,但建议隔离单独启动的进程,一个运行启动器,如何决定?读一点关于多线程和内存管理,我不知道正确的答案。
下面的代码运行csproj,但是.dll可以用dotnet command.以同样的方式执行。

/// <summary>
/// Starts dotnet app in a new thread
/// </summary>
/// <param name="folder">"d:/whatever"</param>
/// <param name="project">"MyBlazor.Company.Whatever"</param>
/// <returns>Thread wrapping the process running the project</returns>
public Thread StartDotnetThread(string folder, string project)
{

    string arguments = string.Format(CultureInfo.InvariantCulture, " run -p {0}", Path.Combine(folder, project));

    ProcessStartInfo info = new ProcessStartInfo("dotnet")
    {
        WindowStyle = ProcessWindowStyle.Normal,
        ErrorDialog = true,
        CreateNoWindow = false,
        UseShellExecute = false,
        Arguments = arguments
    };

    Thread thread = new Thread(() =>
    {
        using Process process = Process.Start(info);
        process.WaitForExit();

        // to stop the process
        // process.Kill();
    });
    
    thread.IsBackground = true;
    thread.Start();

    // wait 1 second
    Task.Delay(1000).Wait();

    // Aborting the thread 
    // thread.Abort()

    return thread;
}

字符串

相关问题