通常我们在 Prgram.cs 中使用硬编码的方式配置 ASP.NET Core 站点的 Hosting 环境,最常用的就是 .UseUrls() 。

public class Program
{   
    public static void Main(string[] args){     
        var host = new WebHostBuilder()     
            .UseUrls("http://*:5000").UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseStartup<Startup>().Build();host.Run();}
}

但这种硬编码绑定端口的方式会给在同一台 Linux 服务器上部署多个站点造成麻烦,因为不同站点需要绑定不同的端口。除非你在开发时就已经约定好各个项目使用的端口,否则很容易在部署时遇到端口冲突问题,从而被迫修改代码。

如果能通过配置文件设置绑定的端口,这个问题就迎刃而解。ASP.NET Core 中有没有提供相应的解决之道呢?带着这个问题,今天签出 aspnet/Hosting 的源码浏览一番,在 SampleStartups 的 StartupFullControl.cs 中找到了答案:

var config = new ConfigurationBuilder().AddCommandLine(args).AddEnvironmentVariables(prefix: "ASPNETCORE_").AddJsonFile("hosting.json", optional: true).Build();var host = new WebHostBuilder().UseConfiguration(config)

原来可以通过 hosting.json 进行配置,下面实际体验一下。

首先创建一个 hosting.json 文件:

{"server.urls": "http://*:5000;http://*:8001","environment": "Development"
}

上面的配置中除了配置 server.urls ,也顺带配置了一下 environment (默认是Production)。

然后在 Program.cs 中使用 hosting.json 中的配置:

public class Program
{    public static void Main(string[] args){        var config = new ConfigurationBuilder().AddJsonFile("hosting.json", optional: true).Build();        var host = new WebHostBuilder()            .UseUrls("http://*:5000").UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseStartup<Startup>()            .UseConfiguration(config).Build();host.Run();}
}

注意一定要把上面的 .UseUrls() 删除,不然 hosting.json 中的配置会被它覆盖。

另外还要注意,在 project.json 中除了在 "publishOptions" 中添加 "hosting.json" ,还要在 "buildOptions" -> "copyToOutput" 中添加 "hosting.json",不然运行时在 bin 文件夹会找不到 hosting.json 文件。

"buildOptions": {"emitEntryPoint": true,"preserveCompilationContext": true,  "copyToOutput": "hosting.json"},
"publishOptions": {"include": [    "hosting.json"]
}

最后用 dotnet run 命令运行站点,体验一下实际效果。

Hosting environment: Development
Content root path: C:\Dev\Cnblogs.WebDemo
Now listening on: http://*:5000
Now listening on: http://*:8001
Application started. Press Ctrl+C to shut down.

原文地址:http://www.cnblogs.com/dudu/p/6233131.html


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注

用quot;hosting.jsonquot;配置ASP.NET Core站点的Hosting环境相关推荐

  1. 用hosting.json配置ASP.NET Core站点的Hosting环境

    通常我们在 Prgram.cs 中使用硬编码的方式配置 ASP.NET Core 站点的 Hosting 环境,最常用的就是 .UseUrls() . public class Program {pu ...

  2. ASP.NET Core macOS 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程

    ASP.NET Core macOS 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程 原文:ASP.NET Core macOS 环境配置 - ASP.NET Core 基础 ...

  3. ASP.NET Core Windows 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Windows 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Windows 环境配置 ASP.NET Core ...

  4. Consul作为配置中心,配置Asp.Net Core应用程序 依据key/value动态更新

    Consul作为配置中心,配置Asp.Net Core应用程序 依据key/value自动更新 https://github.com/wintoncode/Winton.Extensions.Conf ...

  5. 基于VS2017的Docker Support体检ASP.NET Core站点的Docker部署

    最近在学习如何用 Docker 部署生产环境中的 ASP.NET Core 站点,作为一个 Docer 新手,从何处下手更容易入门呢?一开始就手写 Docker 配置文件(Docfile, docke ...

  6. Cenos7 部署asp.net core站点

    系统版本 rpm -q centos-release --- centos-release-7-5.1804.el7.centos.x86_64 安装libicu yum install libunw ...

  7. DataProtection设置问题引起不同ASP.NET Core站点无法共享用户验证Cookie

    这是这两天ASP.NET Core迁移中遇到的一个问题.2个ASP.NET Core站点(对应于2个不同的ASP.NET Core Web应用程序),2个站点都可以登录,但在其中任1个站点登录后,在当 ...

  8. .NET跨平台之旅:将QPS 100左右的ASP.NET Core站点部署到Linux服务器上

    天下午我们将生产环境中一个单台服务器 QPS(每秒请求数)在100左右的 ASP.NET Core 站点部署到了 Linux 服务器上,这是我们解决了在 .NET Core 上使用 EnyimMemc ...

  9. ASP.NET Core 新建项目 - macOS 环境 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 新建项目 - macOS 环境 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 新建项目 - macOS 环境 对于任何语言和 ...

最新文章

  1. go6---slice切片
  2. 从城市大脑到世界数字大脑 构建人类协同发展的超级智能平台
  3. CentOS6.4_X86_64 安装Drupal-7.31必须成功版!
  4. 在博客园写了一年博客,收获的不仅仅是写作技能——我能一直保持积极的学习和工作态度...
  5. agv调度matlab程序,AGV调度方法入门
  6. General texture mapping resources
  7. MySQL中间件之ProxySQL(14):ProxySQL+PXC
  8. 牛客14605 画三角
  9. 基于JavaWeb SSM mybatis 学生信息管理系统设计和实现以及文档报告
  10. 【华为云技术分享】玩转华为物联网IoTDA服务系列三-自动售货机销售分析场景示例
  11. python2 print_【学习笔记】python2的print和python3的print()
  12. redis运行redis-server.exe错误
  13. M2Det论文解读和开源代码
  14. 移动端日期插件rolldate
  15. springboot + vue + FastDFS实现文件上传(支持预览)
  16. Weights Biases
  17. 一看就懂系列:java8流的扁平化/什么是扁平化流(图解)
  18. linux系统认证中级是什么,目前国内常见的几种Linux认证及其所需价格
  19. Coddington shape factor
  20. 神兽麒麟kylin驾驭指南

热门文章

  1. 酷桌面:随身携带你的企业
  2. Vim as Python IDE on windows
  3. getopt()简介
  4. 我居然手写了Spring框架
  5. NLog源码解读——StringBuilderPool
  6. 张善友: .NET社区运营 | 2021 中国开发者生态峰会
  7. ABP Framework V4.4 RC 新增功能介绍
  8. 在 CAP 中使用 AOP ( Castle.DynamicProxy )
  9. C#连接MySQL数据库实例
  10. 如何使用 C# 中的 Action, Func,Predicate