为什么我的 Web 应用程序在调试时运行良好,但在 Windows 服务时运行不佳?

问题描述 投票:0回答:1

我有一个控制台应用程序,我使用 Topshelf 扩展将其安装为 Windows 服务。 我想我应该给它一个UI所以我改变了.csproj中的sdk 来自

项目 Sdk="Microsoft.NET.Sdk"

项目 Sdk="Microsoft.NET.Sdk.Web"

然后,我从 ASPNET Core WebApp 项目复制代码和文件夹(wwwroot、控制器等)。 我将以下代码添加到 Start 方法来运行 WebApplication。

 var builder = WebApplication.CreateBuilder(args);

 // Add services to the container.
 builder.Services.AddControllersWithViews();

var app = builder.Build();

 // Configure the HTTP request pipeline.
 if (!app.Environment.IsDevelopment())
 {
     app.UseExceptionHandler("/Home/Error");
     // 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.UseRouting();

 app.UseAuthorization();

 app.MapControllerRoute(
     name: "default",
     pattern: "{controller=Home}/{action=Index}/{id?}");

 app.Start(); //NOTE use of Start and not Run.  Because Run blocks.

然后我在调试中运行解决方案。 一切正常。 网页启动,我的旧控制台应用程序执行此操作(这基本上是 ping 外部网站以检查我的 ISP 提供商是否再次掉线)。

但是,当我运行 Topshelf 命令将 exe 安装为 Windows 服务并启动该服务时,控制台代码会运行,但 Web 部件在 launchSettings.json 中设置的预期 URL 上不可用。

有什么我应该注意的设置吗? 这是可以做的事情吗?

c# windows-services asp.net-core-webapi
1个回答
0
投票

Program.cs
文件中,您可以根据
launchSettings.json
文件配置Kestrel的监听端口。

builder.WebHost.ConfigureKestrel(serverOptions =>
{
    serverOptions.ListenAnyIP(42198); // http
    serverOptions.ListenAnyIP(5005); // http
}); 
var app = builder.Build(); 
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // 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();

launchSettings.json 文件:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:42198",
      "sslPort": 44337
    }
  },
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5005",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7199;http://localhost:5005",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

然后,发布应用程序后,使用以下命令创建窗口服务。

sc.exe create MyTestService1 binPath= "{EXE FILE PATH (for example: E:\Test\Test Windoes Services\service1\ConsoleServices.exe)}"

之后我们就可以通过“http://localhost:42198/”或“http://localhost:5005/”访问网页了

© www.soinside.com 2019 - 2024. All rights reserved.