托管环境为何将其设置为Production,host.json和local.settings.json之间的区别 [英] Hosting environment why is it set to Production, Difference between host.json and local.settings.json

查看:157
本文介绍了托管环境为何将其设置为Production,host.json和local.settings.json之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于.net核心应用程序的功能,我有两个问题.我正在使用blobtrigger.

1)当我在本地运行项目时,在命令提示符控制台上获得了这个托管环境",我想了解此变量的设置位置以及如何将其更改为开发状态.由于我只是在本地开发,因此会产生误导.

[5/23/2019 7:00:20 PM]主机启动(773毫秒) [5/23/2019 7:00:20 PM]主持人开始了 托管环境:生产 内容根路径:C:Myproject \ bin \ Debug \ netcoreapp2.1 现在收听: http://0.0.0.0:7071

2)host.json和local.settings.json有什么区别.什么时候可以使用host.json?到目前为止,我只使用过local.settings.json,当我发布到天蓝色时,我正在创建local.settings.json中提到的配置,但是未使用Host.json的样子. host.json文件的目的是什么.

解决方案

控制台上的托管环境"来自环境变量ASPNETCORE_ENVIRONMENT.如果未设置此变量,则默认为生产".

在此处设置: HostingEnvironment.cs

此默认设置背后的原因在此 github问题中进行了描述.

此变量在dotnet核心Web应用程序中很流行,但是在Azure函数的官方文档中没有提及(我不确定为什么).如果编写for循环并将所有环境变量从函数中输出到控制台,则会发现默认情况下未设置此变量-既不在生产环境中运行,也不在Visual Studio中运行.

如果您希望在本地定义此变量,则有几种不同的方法.

通过命令行设置环境变量:

setx ASPNETCORE_ENVIRONMENT "Development"

在Properties \ launchSettings.json中定义:

  "commandName": "Project",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }

在local.settings.json中定义:

  "Values": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }

请注意,当您将应用程序发布为Azure时,此变量不会自动定义为正式版.您将必须在Azure门户的配置"->应用程序设置"中定义此变量.

在azure函数中,似乎还有另一个类似的环境变量,称为AZURE_FUNCTIONS_ENVIRONMENT.默认情况下, 是在本地定义的.

AZURE_FUNCTIONS_ENVIRONMENT = Development

默认情况下,未在生产环境中定义它,可以在azure门户中定义.

host.json与local.settings.json之间的区别:

host.json用于配置应用程序基础结构可以理解的预定义设置.它适用于本地和生产环境.但是,它不允许自定义设置.另一方面,local.settings.json对于定义自定义设置很有用. host.json已提交到源代码管理中,但是local.settings.json通常不在源代码管理中,并且被认为是存储秘密和连接字符串以进行开发的好位置.

此处提供有关差异的更多详细信息: host.json参考

local.settings. json参考

I have two questions on .net core applications for functions. I am using blobtrigger.

1) When I run my project locally I get this 'Hosting environment' on the command prompt console, I want to understand where is this variable set and how can I change it to development. Its misleading since I am only developing locally.

[5/23/2019 7:00:20 PM] Host started (773ms) [5/23/2019 7:00:20 PM] Job host started Hosting environment: Production Content root path: C:Myproject\bin\Debug\netcoreapp2.1 Now listening on: http://0.0.0.0:7071

2) What is the difference between host.json and local.settings.json. When can host.json be used? So far I have only used local.settings.json and when I publish to azure I am creating configurations mentioned in local.settings.json but Host.json is not used it looks like. Whats the purpose of host.json file is.

解决方案

"Hosting environment" on the console comes from the environment variable ASPNETCORE_ENVIRONMENT. When this variable is not set, it defaults to "Production".

It's set here: HostingEnvironment.cs

The reason behind this default is described in this github issue.

This variable is popular in dotnet core web apps, but it is not mentioned in official docs in Azure functions (I am not sure why). If you write a for loop and output all the environment variables to console from within a function, you will find that this variable is not set by default - neither in production, nor when running in Visual Studio.

If you wish to define this variable locally, you have a few different ways.

Setting the environment variable via command line:

setx ASPNETCORE_ENVIRONMENT "Development"

Defining this in Properties\launchSettings.json:

  "commandName": "Project",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }

Defining this in local.settings.json:

  "Values": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }

Note that this variable is not automatically defined to production when you publish your app to azure. You will have to define this variable in "Configuration" -> "Application Settings" in Azure portal.

In azure functions there appears to be another similar environment variable called AZURE_FUNCTIONS_ENVIRONMENT. This one is defined by default locally.

AZURE_FUNCTIONS_ENVIRONMENT = Development

This is not defined in production by default, and can be defined in the azure portal.

Difference between host.json and local.settings.json:

host.json is to configure pre-defined settings that function app infrastructure understands. It applies to both local and production environments. It doesn't allow custom settings though. local.settings.json on the other hand is useful for defining custom settings. host.json is committed into source control, but local.settings.json is usually left out of source control, and is considered to be a good location to store secrets and connection strings for development.

More details here about the differences: https://docs.microsoft.com/en-us/azure/azure-functions/functions-develop-vs#create-an-azure-functions-project (scroll to the end of that section)

host.json reference

local.settings.json reference

这篇关于托管环境为何将其设置为Production,host.json和local.settings.json之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆