大佬教程收集整理的这篇文章主要介绍了为什么环境变量引用不适用于 MS Build Target? TL;DR 说明,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下配置,但 ASPNETCORE_ENVIRONMENT 变量没有进入 CsProj 配置。
.vscode/launch.Json
@GetMapPing("/")
public String home() {
return ("<h1>Welcome</h1>");
}
.vscode/tasks.Json
{
// UsE intelliSense to find out which attributes exist for C# deBUGging
// Use hover for the description of the exisTing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/deBUGger-launchJson.md
"version": "0.2.0","configurations": [
{
"name": ".NET Core Launch (web)","type": "coreclr","request": "launch","p@R_816_4404@Task": "build","program": "${workspaceFolder}/Web/bin/DeBUG/net5.0/Web.dll","args": [],"cwd": "${workspaceFolder}/Web","stopAtEntry": false,// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-Webbrowser
"serverReadyAction": {
"action": "openExternally","pattern": "\\bNow Listening on:\\s+(https?://\\S+)"
},"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},"sourcefileMap": {
"/VIEws": "${workspaceFolder}/VIEws"
}
}
]
}
Web/PropertIEs/launchsetTings.Json
{
"version": "2.0.0","tasks": [
{
"label": "build","command": "dotnet","type": "process","args": [
"build","${workspaceFolder}/Web/Web.csproj","/property:GenerateFullPaths=true","/consoleloggerparameters:NoSumMary"
],"problemmatcher": "$msCompile"
},{
"label": "publish","args": [
"publish",{
"label": "watch","args": [
"watch","run","problemmatcher": "$msCompile"
}
]
}
Web/Web.csproj
{
"IISSetTings": {
"windowsAuthentication": false,"anonymousAuthentication": true,"IISExpress": {
"applicationUrl": "http://localhost:15269","sslPort": 44386
}
},"profiles": {
"IIS Express": {
"commandname": "IISExpress","launchbrowser": true
},"Web": {
"commandname": "Project","dotnetRunmessages": "true","launchbrowser": true,"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<Target name="PreCompile" BeforeTargets="Build">
<Exec Command="$(MSBuildStartupDirectory)/node_modules/.bin/gulp build --environment $(ASPNETCORE_ENVIRONMENT)" />
</Target>
</Project>
输出
// gulpfile.ts
export { default as build } from './build/Sass';
// build/Sass.ts
import type { TaskFunction } from 'gulp';
import { dest,src } from 'gulp';
import * as fiber from 'fibers';
import * as sass from 'gulp-dart-sass';
const environment = process.argv[3] === '--environment' ? process.argv[4] : 'Staging';
const task: TaskFunction = () => {
console.log(`Hello!!!!! ${environment}`);
console.log(process.env);
return src('./Web/themes/**/*.sass')
.pipe(sass({ fiber }))
.pipe(dest('./Web/wwwroot/themes'));
};
export default task;
ExecuTing task: dotnet build /.../Web/Web.csproj /property:GenerateFullPaths=true /consoleloggerparameters:NoSumMary <
Microsoft (R) Build ENGIne version 16.8.0+126527ff1 for .NET
copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
Web -> /.../Web/bin/DeBUG/net5.0/Web.dll
Web -> /.../Web/bin/DeBUG/net5.0/Web.VIEws.dll
[17:50:28] requiring external module ts-node/register
[17:50:28] Working directory changed to ~/...
[17:50:30] Using gulpfile ~/.../gulpfile.ts
[17:50:30] StarTing 'build'...
Hello!!!!! undefined
{
TERM_PROGRAM: 'vscode',TERM: 'xterm-256color',SHelL: '/bin/zsh',TMPDIR: '/var/folders/pf/rmb77nvd2t99vjmq9z1xc6100000gn/T/',TERM_PROGRAM_VERSION: '1.52.1',DOTNET_HOST_PATH: '/usr/local/share/dotnet/dotnet',ORIGINAL_XDG_CURRENT_DESKtop: 'undefined',LC_ALL: 'en_US.UTF-8',DOTNET_Cli_TELEMETRY_SESSIONID: 'f3d95a71-5185-4c74-b373-da49d9b6f174',USER: 'roydukkey',COMMAND_MODE: 'unix2003',MSBuildLoadMicrosoftTargetsReadonly: 'true',SSH_AUTH_SOCK: '/private/tmp/com.apple.launchd.JpMQQSNA9S/Listeners',__CF_user_TEXT_ENCoding: '0x1F5:0x0:0x0',PATH: '/usr/local/php5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/library/Frameworks/Mono.framework/Versions/Current/Commands',__CFBundlEIDentifIEr: 'com.microsoft.VSCode',PWD: '.../Web',LANG: 'en_US.UTF-8',MSBuildSDKsPath: '/usr/local/share/dotnet/sdk/5.0.101/Sdks',xpc_FLAGS: '0x0',xpc_service_name: '0',SHLVL: '1',HOME: '...',VSCODE_GIT_ASKPASS_MAIN: '/Applications/Visual studio Code.app/Contents/resources/app/extensions/git/dist/askpass-main.Js',LOGname: 'roydukkey',VSCODE_GIT_IPC_HANDLE: '/var/folders/pf/rmb77nvd2t99vjmq9z1xc6100000gn/T/vscode-git-8f8e32c0ee.sock',GIT_ASKPASS: '/Applications/Visual studio Code.app/Contents/resources/app/extensions/git/dist/askpass.sh',VSCODE_GIT_ASKPASS_NODE: '/Applications/Visual studio Code.app/Contents/Frameworks/Code Helper (Renderer).app/Contents/MacOS/Code Helper (Renderer)',colorTERM: 'truecolor',MSBuildExtensionsPath: '/usr/local/share/dotnet/sdk/5.0.101/',_: '.../node_modules/.bin/gulp',INIT_CWD: '.../Web'
}
[17:50:30] Finished 'build' after 71 ms
Terminal will be reused by tasks,press any key to close it.
我认为您可能需要将环境变量从 env
文件中的 .vscode/launch.json
移动到 environmentVariables
中“Web”属性中的 Web/Properties/launchsetTings.json
。
docs.microsoft.com reference
,我想尝试在您的 tasks.json 调用中添加另一个包含“/p:ASPNETCORE_ENVIRONMENT=$(ASPNETCORE_ENVIRONMENT)”的 args 元素?
查看 process.env 的输出,似乎 dotnet.exe 的调用环境没有从 VSCode 继承环境变量。
,不管是否传递了环境变量,在我看来您是在尝试以 Development
模式运行程序,对吗?您似乎成功地做到了这一点,因为托管环境实际上在最后一个输出窗口中显示为 Development
。所以,如果这是你的目标,也许它可以per si解决。
另一方面,您会发现为生产用途定义 ASPNETCORE_ENVIRONMENT
变量很有用。如果是这种情况,在 MSBuild 中定义它可能不是您想要的,也绝对不是在 launchsetTings.json 中定义,因为这仅用于开发目的。所以,我认为在你想要运行应用程序的环境中将它定义为环境变量将是一个不错的选择——那么,无论值是 Development、Staging 还是 生产取决于部署管道。
但是,请注意,如果您不想访问 ASPNETCORE_ENVIRONMENT
变量本身,您实际上可能不需要定义它。已发布应用的默认托管环境为 Production
,并且仅在提供环境变量 (herE) 时才会更改。
这个 reference 有关于这个主题的很好的信息 :)
,将 build
中的 tasks.json
任务更改为
{
"label": "build","command": "dotnet","type": "process","options": {
"env": {
"ASPNETCORE_ENVIRONMENT":"development"
}
},"args": [
"build","${workspaceFolder}/Web/Web.csproj","/property:GenerateFullPaths=true","/consoleloggerparameters:NoSumMary"
],"problemMatcher": "$msCompile"
},
在 launch.json
的 Launch Targets 中设置的环境变量 (ENV) 仅传递给 program
,而不传递给任何启动任务。
即这些 ENV 仅传递给 ${workspaceFolder}/Web/bin/Debug/net5.0/Web.dll
,而不传递给 build
内的 preLaunchTask
任务。
仅供参考,构建您的项目 (.csproj) 是由 dotnet build
而不是 @H_170_9@msbuild 完成的,两者略有不同。
无论如何,这个构建是由 preLaunchTask
(build) 发起的。
因此,如果您希望将 ENV 传递给项目构建器(进而传递给 gulp 程序),那么您需要某种方式将它们支付给 tasks.json
中指定的 build 任务
ENV 可以通过 options
属性传递给任务。
https://code.visualstudio.com/docs/editor/tasks#_custom-tasks
不...你不能像这样重用 ENV 变量
"env": {
"ASPNETCORE_ENVIRONMENT":"${env:ASPNETCORE_ENVIRONMENT}"
}
原因如上;来自启动目标的 ENV 不会传递给任务。
以上是大佬教程为你收集整理的为什么环境变量引用不适用于 MS Build Target? TL;DR 说明全部内容,希望文章能够帮你解决为什么环境变量引用不适用于 MS Build Target? TL;DR 说明所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。