【问题标题】:How to set remote url when web app is running as a service in .net core (2.2)当 web 应用程序在 .net core (2.2) 中作为服务运行时如何设置远程 url
【发布时间】:2019-04-16 05:07:46
【问题描述】:

我已将.net core(2.2) Web 应用程序部署为 Windows 服务,但无法从远程 PC 访问此应用程序。

应用程序在本地计算机上使用 Url = "http://localhost:5000" 正常运行。

我在“launchSettings.json”文件中指定了带有 IP 地址的 Url =“http://XXX.XXX.XX.XX:5000”,但无法从远程 PC 访问它。

出于测试目的,我已经关闭了我的防火墙设置,但它没有任何效果。

Web应用部署环境:Windows7

现有源代码:

launchSetting.json

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:56032",
      "sslPort": 44324
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "TestApp": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Program.cs

public static void Main(string[] args)
{
    var isService = !(Debugger.IsAttached || args.Contains("--console"));
    if (isService)
    {
            var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
            var pathToContentRoot = Path.GetDirectoryName(pathToExe);
            Directory.SetCurrentDirectory(pathToContentRoot);
    }

    var builder = CreateWebHostBuilder(args.Where(arg => arg != "--console").ToArray());
    var host = builder.Build();

    if (isService)
        host.RunAsService();
    else
        host.Run();
}

预期结果: 能够从远程 PC 访问作为 Web 服务安装的 .net 核心 Web 应用程序。

【问题讨论】:

    标签: c# asp.net-core .net-core asp.net-core-2.2


    【解决方案1】:

    您是否尝试在具有高级安全性的 Windows Defender 防火墙中打开入站端口 -> 入站规则 -> 新规则 -> 端口?

    【讨论】:

      【解决方案2】:

      我必须在此处添加 UseUrls()

      .ConfigureWebHostDefaults(webBuilder =>
      {
          webBuilder.UseStartup<Startup>();
          // allow remote connections
          webBuilder.UseUrls("http://0.0.0.0:5000");
      })
      

      并在 Windows 防火墙中为端口 5000 制定规则。

      【讨论】:

        猜你喜欢
        • 2018-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-01
        • 1970-01-01
        • 2019-08-12
        相关资源
        最近更新 更多