Visual Studio在发布过程中使用launchSettings.json配置文件中定义的哪个?

编程入门 行业动态 更新时间:2024-10-28 16:20:00
本文介绍了Visual Studio在发布过程中使用launchSettings.json配置文件中定义的哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试配置ASP.NET Core项目发布配置文件以部署到暂存环境.在服务器上设置了预配置的ASPNETCORE_ENVIRONMENT变量,但是无论我尝试使用什么Visual Studio都会不断将ASPNETCORE_ENVIRONMENT变量定义添加到发布web.config文件中.消除它的唯一方法是从launchSettings.json中删除.

I'm trying to configure ASP.NET Core project publication profile for deployment to staging environment. There's preconfigured ASPNETCORE_ENVIRONMENT variable set on server, but whatever I try Visual Studio keeps adding ASPNETCORE_ENVIRONMENT variable definition to publishing web.config file. The only way to eliminate it is to delete from launchSettings.json.

这会导致几个问题:

  • 尽管docs.microsoft表示此选项应该完全覆盖环境变量,实际上是HostingEnvironment.EnvironmentName属性设置为"Staging; Environment",环境变量和web.config设置的串联值.
  • 这样可以防止不当行为appsettings.{environment} .json加载并破坏了所有环境感知逻辑.
  • 我希望将ASPNETCORE_ENVIRONMENT保留在launchSettings.json中进行调试.
  • 我找不到控制此行为的任何设置.有吗?

    I could not find any setting that controls this behaviour. Is there any?

    这是我的.pubxml

    This is my .pubxml

    <Project ToolsVersion="4.0" xmlns="schemas.microsoft/developer/msbuild/2003"> <PropertyGroup> <WebPublishMethod>MSDeploy</WebPublishMethod> <LastUsedBuildConfiguration>Release.Main</LastUsedBuildConfiguration> <LastUsedPlatform>Any CPU</LastUsedPlatform> <SiteUrlToLaunchAfterPublish /> <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish> <ExcludeApp_Data>False</ExcludeApp_Data> <TargetFramework>netcoreapp2.2</TargetFramework> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <ProjectGuid>433bb565-5e85-4f1a-9dd4-a7f437fdb534</ProjectGuid> <SelfContained>false</SelfContained> <_IsPortable>true</_IsPortable> <MSDeployServiceURL>192.168.0.22</MSDeployServiceURL> <DeployIisAppPath>auth/main</DeployIisAppPath> <RemoteSitePhysicalPath /> <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer> <MSDeployPublishMethod>RemoteAgent</MSDeployPublishMethod> <EnableMSDeployBackup>True</EnableMSDeployBackup> <_SavePWD>False</_SavePWD> </PropertyGroup> </Project>

    launchSettings.json

    launchSettings.json

    { "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iis": { "applicationUrl": "localhost/DocShellWeb.Sts", "sslPort": 0 } }, "profiles": { "DocShellWeb.Sts": { "commandName": "IIS", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }

    部署的web.config

    Deployed web.config

    <?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="..." arguments="..." stdoutLogEnabled="false" hostingModel="InProcess" stdoutLogFile=".\logs\stdout"> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> </environmentVariables> </aspNetCore> </system.webServer> </location> </configuration> <!--ProjectGuid: 706412a5-be0d-452d-a9b0-ce00d799f990-->

    推荐答案

    仅在调试期间以及通过 dotnet run运行应用程序时,Visual Studio才会使用 launchSettings.json 文件.代码>.发布时根本不使用它,甚至不考虑使用它.如果要在发布时指定特定的环境,则需要将添加< EnvironmentName> Staging</EnvironmentName> 添加到 pubxml 文件:

    The launchSettings.json file is only used by Visual Studio during debugging and when running the app via dotnet run. It is not used at all or even considered when publishing. If you want to specify a particular environment when you publish, then you need to add add <EnvironmentName>Staging</EnvironmentName> to the pubxml file:

    <Project ToolsVersion="4.0" xmlns="schemas.microsoft/developer/msbuild/2003"> <PropertyGroup> ... <EnvironmentName>Staging</EnvironmentName> </PropertyGroup> </Project>

    这将为在发布的web.config中为 ASPNETCORE_ENVIRONMENT 设置为 Staging 的环境变量声明添加环境效果.

    This will have the effect of adding an environment variable declaration for ASPNETCORE_ENVIRONMENT set to Staging in your published web.config.

    但是,这要假定一些条件:

    However, that assumes a few things:

  • 您正在使用IIS进行托管.只有IIS关心web.config文件,因此,如果您使用其他Web服务器托管,显然会需要其他策略.
  • 首先要真正将发布的应用程序绑定到特定的环境.ASP.NET Core应用程序的优点之一是它们不是针对特定环境构建的,例如ASP.NET应用程序就是这种情况.您可以采用相同的已发布代码,并将其放置在任何环境中.然后,可以使用服务器上的 ASPNETCORE_ENVIRONMEMT 环境变量来设置环境(即,将其设置为登台服务器的 Staging ,然后在那里发布的任何应用程序都会自动使用 Staging 环境;或者,如果您使用的是Azure,则可以通过应用设置进行设置.无论哪种情况,您都可以从实际发布的代码中抽象出环境,从而可以进行CI/CD管道,而不必不断发布.
  • That you're using IIS to host. Only IIS cares about a web.config file, so if you're hosting with a different web server, you'll obviously need a different strategy.
  • That you want to actually tie the published app to a particular environment in the first place. One of the nice things about ASP.NET Core apps is that they are not built for a particular environment, such as was the case for ASP.NET apps. You can take the same published code and drop it in any environment. The environment then, can be set with an ASPNETCORE_ENVIRONMEMT environment variable on the server (i.e. set it as Staging for a staging server, and then any app published there will automatically use the Staging environment. Or, it can be set with an app setting if you're using Azure. In either case, you benefit from abstracting the environment from the actual publish code, allowing you to do a CI/CD pipeline, without having to constantly publish.
  • 更多推荐

    Visual Studio在发布过程中使用launchSettings.json配置文件中定义的哪个?

    本文发布于:2023-11-09 09:05:48,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1571979.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:配置文件   过程中   定义   Visual   Studio

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!