zoukankan      html  css  js  c++  java
  • ASP.NET Core 设置运行端口,启动多个服务实例

    想要通过命令行方式运行ASP.NET Core,指定运行的端口号,该怎么做呢?

    两种方法:

    第一种方法:通过配置文件指定端口

    1.添加一个json配置文件,一般命名为 host.json,也可以使用默认配置文件 appsettings.json

      添加配置项,多个端口可以通过分号(;)隔开。

      "server.urls": "http://*:5025;http://*:5026"

    2.修改 Program.cs 的 Main 方法,定义 ConfigurationBuilder 对象。

        public class Program
        {
            public static void Main(string[] args)
            {
                var config = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json", optional: true)
                    .Build();
    
                CreateWebHostBuilder(args)
                    .UseConfiguration(config)
                    .Build()
                    .Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>();
        }

    第二种方法:通过运行时参数指定端口号

    1.添加引用:Microsoft.Extensions.Configuration.CommandLine

    2.修改 Program.cs 的 Main 方法,定义 ConfigurationBuilder 对象。 

        public class Program
        {
            public static void Main(string[] args)
            {
                var config = new ConfigurationBuilder()
                    .AddCommandLine(args)
                    .Build();
    
                CreateWebHostBuilder(args)
                    .UseConfiguration(config)
                    .Build()
                    .Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>();
        }

     3.运行时指定参数:dotnet AspnetDockerTest.dll --server.urls "http://*:5005;http://*:5006"

    思考:

    指定运行端口号并不是我的目的,我在考虑,通过第二种,指定运行时参数来启动服务的方式,我们可以对一个aspnet core程序启动多个服务实例,只要端口号不同就行,这样是不是更方便集群部署呢。

  • 相关阅读:
    【持续更新】GDB使用笔记
    PL/SQL Developer-官网下载地址
    kali 下程序卸载方法
    python2 安装scrapy出现错误提示解决办法~
    pyHook监听用户鼠标、键盘事件
    pip 安装pandas报UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5错
    Python模块常用的几种安装方式
    解决Kali Linux没有声音
    关于破解路由器密码
    解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误
  • 原文地址:https://www.cnblogs.com/flame7/p/13678933.html
Copyright © 2011-2022 走看看