简体   繁体   English

自托管 Net Core 3 应用程序不采取端口设置

[英]Self-hosted Net Core 3 application does not take port settings

I feel silly asking this as there are dozens of articles on Net Core hosting, but I have tried everything and I am still having the issue.我觉得问这个很傻,因为有很多关于 Net Core 托管的文章,但我已经尝试了所有方法,但仍然有问题。

I am attempting to change the port used by a self-hosted web service.我正在尝试更改自托管 Web 服务使用的端口。 I have altered the launchSettings.json file.我已经更改了 launchSettings.json 文件。

"MyService": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_URLS": "http://*:51248",
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:51248"
    },
    "MyService": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_URLS": "http://*:51248",
        "ASPNETCORE_ENVIRONMENT": "Release"
      },
      "applicationUrl": "http://localhost:51248"
    }

I have also attempted to set the port through direct configuration:我还尝试通过直接配置设置端口:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<MyServiceWorker>();
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls("http://*:51248");
                    webBuilder.UseStartup<Startup>();
                })
                .ConfigureWebHost(config => { config.UseUrls("http://*:51248"); })
                .UseWindowsService()
                .UseSerilog();
    }

Everything works fine if I run through Visual Studio, but if I run the executable directly, it still uses port 5000. If I run it as a Windows service, it seems to pick some random port.如果我通过 Visual Studio 运行一切正常,但如果我直接运行可执行文件,它仍然使用端口 5000。如果我将它作为 Windows 服务运行,它似乎选择了一些随机端口。

I have hit dozens of web sites, but have not found a solution.我打了几十个网站,但没有找到解决方案。 Does anyone have any suggestions?有没有人有什么建议?

I feel pretty foolish, but I will post this in case it helps someone else.我觉得很愚蠢,但我会发布这个以防它对其他人有帮助。 What I have discovered is that .UseUrls does work if the application is run as a Windows service.我发现如果应用程序作为 Windows 服务运行, .UseUrls确实有效。 The launchSettings.json settings work when launching from within Visual Studio.从 Visual Studio 中启动时, launchSettings.json设置有效。 I have not been able to change the listening port when just running as a console application.仅作为控制台应用程序运行时,我无法更改侦听端口。

It turns out that the problem was an artifact of the way that I was testing the application.事实证明,问题是我测试应用程序的方式造成的。 Hopefully no one else will waste a lot of time doing the same thing.希望没有其他人会浪费很多时间做同样的事情。

There can be many options, and one of them is from here ,可以有很多选择,其中之一来自这里

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace testmvccore31
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.ConfigureKestrel(serverOptions =>
                    {
                        serverOptions.Listen(IPAddress.Any, 51248);
                    });
                });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自托管 .NET 核心控制台应用程序中的 Startup.cs - Startup.cs in a self-hosted .NET Core Console Application 如何将外部域绑定到 .NET Core 3.1 Kestrel 自托管应用程序? - How to bind external domain to .NET Core 3.1 Kestrel self-hosted application? 如何在自托管的.net核心mvc应用程序中运行长时间运行或重复运行的任务? - How should I run long-running or recurring taks in a self-hosted .net core mvc application? 自托管进程中 Web API 带点网核心 - Self-hosted In Process Web API with Dot net core Owin自托管WebApi超时设置 - Owin Self-Hosted WebApi Timeout Settings WCF“自托管”应用程序变得无法响应 - WCF “Self-Hosted” application becomes unresponsive .NET 6 中的自托管 Web API - Self-Hosted Web API in .NET 6 转换为ASP.Net Framework 4.5.1如何影响自托管的Web服务? - How does conversion to ASP.Net Framework 4.5.1 affect self-hosted web services? 自托管的OWIN .NET 4.5 Web API 2不适用于Windows身份验证和https - Self-hosted OWIN .NET 4.5 Web API 2 does not work with Windows authentication and https 具有每个请求生存期范围的ServiceStack自托管应用程序 - ServiceStack self-hosted application with per-request lifetime scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM