Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自托管Net Core 3应用程序不接受端口设置_C#_Self Hosting_.net Core 3.0 - Fatal编程技术网

C# 自托管Net Core 3应用程序不接受端口设置

C# 自托管Net Core 3应用程序不接受端口设置,c#,self-hosting,.net-core-3.0,C#,Self Hosting,.net Core 3.0,我觉得问这个问题很傻,因为有几十篇关于NetCore托管的文章,但是我已经尝试了所有的方法,我仍然有这个问题 我正在尝试更改自托管web服务使用的端口。我已经更改了launchSettings.json文件 "MyService": { "commandName": "Project", "environmentVariables": { "ASPNETCORE_URLS": "http://*:51248", "ASPNETCORE_EN

我觉得问这个问题很傻,因为有几十篇关于NetCore托管的文章,但是我已经尝试了所有的方法,我仍然有这个问题

我正在尝试更改自托管web服务使用的端口。我已经更改了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"
    }

我还尝试通过直接配置设置端口:

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();
    }
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureServices((主机上下文,服务)=>
{
services.AddHostedService();
})
.ConfigureWebHostDefaults(webBuilder=>
{
UseUrls(“http://*:51248”);
webBuilder.UseStartup();
})
.ConfigureWebHost(config=>{config.UseUrls(“http://*:51248”);})
.UseWindowsService()
.useserlog();
}
如果我通过VisualStudio运行,一切正常,但是如果我直接运行可执行文件,它仍然使用端口5000。如果我将其作为Windows服务运行,它似乎会选择一些随机端口


我访问了几十个网站,但没有找到解决方案。有人有什么建议吗?

有很多选择,其中一个是

使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.Hosting;
使用Microsoft.Extensions.Logging;
命名空间testmvccore31
{
公共课程
{
公共静态void Main(字符串[]args)
{
CreateHostBuilder(args.Build().Run();
}
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.UseStartup();
webBuilder.ConfigureKestrel(服务器选项=>
{
serverOptions.Listen(IPAddress.Any,51248);
});
});
}
}

我觉得自己很傻,但我会把这篇文章发出去,以防它对其他人有所帮助。我发现,如果应用程序作为Windows服务运行,
.UseUrls
确实有效。当从VisualStudio中启动时,
launchSettings.json
设置起作用。在作为控制台应用程序运行时,我无法更改侦听端口


事实证明,这个问题是我测试应用程序的方式造成的。希望没有其他人会浪费很多时间做同样的事情。

您已经能够尝试来自的新建议了吗?是否有一个
appsettings.json
文件,其中写入了端口号?我尝试了最新的建议(尽管有时很难判断它们引用的是哪个核心版本)。我正在使用一个appsettings.json文件进行其他配置,但它不包含端口信息。如果我找到了在代码中设置端口的方法,我会将该设置添加到appsetttings.json文件中。如果在
applicationUrl
字段中使用
0.0.0
而不是
localhost
,是否也会这样做?我看到了一些奇怪的行为,特别是在使用docker容器时。我尝试将其改为零。如果我作为控制台应用程序运行,它仍然在端口5000上运行。但是,我作为Windows服务运行,并且能够到达正确的端口。我开始认为,当作为控制台应用程序运行时,需要将端口号作为参数包括在内。我将尝试还原一系列内容,以找到一个需要正确的设置。我会回来的。谢谢你的回复。我尝试了这个方法,得到了以下警告:“覆盖地址”http://*:51248“。改为绑定到UseKestrel()中定义的端点。”至少这给了我一些需要寻找的东西。
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);
                    });
                });
    }
}