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

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

问题描述

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

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

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

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] 主机已启动(773 毫秒)[5/23/2019 7:00:20 PM] 作业主机开始托管环境:生产内容根路径:C:MyprojectinDebug etcoreapp2.1现在收听:http://0.0.0.0:7071

[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:MyprojectinDebug etcoreapp2.1 Now listening on: http://0.0.0.0:7071

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

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.

推荐答案

控制台上的托管环境"来自环境变量ASPNETCORE_ENVIRONMENT.未设置此变量时,默认为Production".

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

设置在这里:HostingEnvironment.cs

github 问题中描述了此默认值背后的原因.

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

这个变量在 dotnet core web 应用程序中很流行,但在 Azure 函数的官方文档中没有提到(我不知道为什么).如果您编写一个 for 循环并从函数中将所有环境变量输出到控制台,您会发现默认情况下未设置此变量 - 无论是在生产中,还是在 Visual Studio 中运行时.

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.

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

setx ASPNETCORE_ENVIRONMENT "Development"

在 PropertieslaunchSettings.json 中定义:

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

在 local.settings.json 中定义:

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

请注意,当您将应用发布到 azure 时,此变量不会自动定义到生产环境.您必须在 Azure 门户的配置"->应用程序设置"中定义此变量.

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.

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

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.

host.json 和 local.settings.json 的区别:

host.json 用于配置功能应用基础架构可以理解的预定义设置.它适用于本地和生产环境.但它不允许自定义设置.另一方面,local.settings.json 对于定义自定义设置很有用.host.json 被提交到源代码控制中,但 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.

有关差异的更多详细信息:https://docs.microsoft.com/en-us/azure/azure-functions/functions-develop-vs#create-an-azure-functions-project(滚动到该部分的结尾)

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 参考

local.settings.json参考

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

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