我已经在本地计算机上开发并部署了 ASP.NET Core 应用程序以进行离线操作,并为该应用程序生成了可执行文件。因此,我将 Blazor WASM 交互式服务器与 .NET 8.0 一起使用,我不确定在窗口启动时自动运行 .exe 文件的最佳方式是什么?当我运行 .exe 文件时,部署的应用程序将在计算机上的默认浏览器上打开。谢谢大家
除了将 .exe 文件附加到窗口任务管理器的启动选项卡上之外,我还希望看到一种有效的方法。我听说过在 Visual Studio 上使用窗口服务,但不确定它是如何工作的? Windows服务上的脚本是否执行Blazor .exe文件或其他东西?
1。添加
Microsoft.Extensions.Hosting.WindowsServices
套餐。
2。如下更改您的 Program.cs 文件。
using BlazorApp1.Components;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// Add this line
builder.Services.AddWindowsService();
// Add this line
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.ListenAnyIP(5100); // http port
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();
3.发布项目并导航到该文件夹,然后运行命令创建 Windows 服务。这是我的示例命令。
sc.exe create ".NET Joke Service20210829" binpath= "C:\Users\Jason\source\repos\BlazorApp-empty1\BlazorApp1\bin\Release\net8.0\publish\BlazorApp1.exe"