I'm trying to change my asp.net core hosting environment to development.
What I already did is :
Run this command:
set ASPNETCORE_ENVIRONMENT=Development
Change the environment variables in system:
Run these commands:
dotnet restore
dotnet watch run
I just see in the projectName.csproj file there is comment that say that it run the production, maybe that's the problem.
<Target Name="RunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec Command="npm install" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />
But for now what I see in my project is Hosting environment : Production
and I want to change it to environment because I can't see the changes in live when I change client side changes like HTML, CSS.
To set the ASPNETCORE_ENVIRONMENT environment variable in windows, run this command in cmd:
setx ASPNETCORE_ENVIRONMENT "Development"
Or
Use try in web.config like:
<configuration>
<!--
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
-->
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\MyApplication.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
After setting the ASPNETCORE_ENVIRONMENT variable for your system you must open a new command window before running dotnet run.
Environment variables are cached for the lifetime of the shell, so an existing window won't pick up changes to the environment variable
I wrote a post on how to achieve this here: https://andrewlock.net/how-to-set-the-hosting-environment-in-asp-net-core/#atthecommandline
Related
There has a scenario about testing the dark/light mode.
I found a blog that it can set the browser or system settings before running test:
args.push('--force-dark-mode=true')
I curious if ASP.Net can do this within the testing framework or others nuget packages like anglesharp?
Are you looking for:
adding this ASPNETCORE_ENVIRONMENT to web.config
<aspNetCore processPath="dotnet" arguments=".\MyCustomers.Services.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
and then change ASPNETCORE_ENVIRONMENT to be
Development
Staging
Production
We have appsettings.Production.json to define some settings overrides for the appsettings.json. This seems to work when testing locally and setting the launch settings to define ASPNETCORE_ENVIRONMENT.
After deployment with Azure DevOps pipeline and debloymen job the Web App does not work due to clearly using the default appsettings.json. Even setting the ASPNETCORE_ENVIRONMENT to App Service configuration does not have any effect and the value should anyways default to Production.
On the deployed web app, Kudu shows all definitions of ASPNETCORE_ENVIRONMENT to be set to Production as expected with App Settings as well as in environment variables.
Deployment job is defined as follows:
- deployment: deployWebAPI
displayName: 'Deploy WebApi'
dependsOn: fanOut
variables:
environmentName: $[ stageDependencies.build.setParameters.outputs['setParametersStep.environmentName'] ]
appServiceName: $[ stageDependencies.build.setParameters.outputs['setParametersStep.appServiceName'] ]
resourceGroupName: $[ stageDependencies.build.setParameters.outputs['setParametersStep.resourceGroupName'] ]
environment: $(environmentName)
strategy:
runOnce:
deploy:
steps:
- task: DownloadBuildArtifacts#0
displayName: 'Download webApp artifact'
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: $(webAppArtifactName)
downloadPath: '$(System.ArtifactsDirectory)'
- task: AzureWebApp#1
inputs:
azureSubscription: $(azureSubscription)
appName: $(appServiceName)
deployToSlotOrASE: true
resourceGroupName: $(resourceGroupName)
package: $(System.ArtifactsDirectory)/**/$(webAppArtifactName)/MyWebApi.zip
deploymentMethod: zipDeploy
The deployment job variable environmentName must be the significant part here. It was defined as follows in beginning of our pipeline definition yml:
variables:
environmentName: 'test-dev'
This seems to generate a Web.Config with ASPNETCORE_ENVIRONMENT defined as environment variable:
<?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="dotnet" arguments=".\MyWebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
</configuration>
Changing that variable to correct value Production fixed the issue.
Some debugging notes:
Logging following variable on startup can be useful as you cannot rely even on Kudu:
hostingContext.HostingEnvironment.EnvironmentName
Strangely this web.config is in place already in build artifacts on pipeline although local publish package creation with dotnet publish --configuration Release does not set the env variable.
Direct publish from local env worked, so when in doubt diff your locally created publish package to the one deployed to the server by pipeline.
UPDATE: I fixed this by changing to AspNetCoreModule and used out of process.
This mvc site has been running for a while but after the last deploy, it will no longer run. The event log shows this error:
Application <path to app> failed to start. Exception message:
Application DLL was not found at <path to app>\.\Proxy.dll. Confirm the application dll is present. Single-file deployments are not supported in IIS.
In the csproj file, I have this:
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
And, I publish to a local folder like so:
dotnet publish <path to source>/Proxy.csproj -c Release -o ./build -r win10-x64 --self-contained true
Finally, the web config is this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\Proxy.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</configuration>
And, since this ran up until last update, I believe IIS (version 8.5- Win Server 2012 R2) is setup correctly. Under modules, I see AspNetCoreModuleV2. The Apppool is setup with Integrated pipeline and no managed code.
Can somebody help me with this? Thanks. Eric
I have followed (as far as I can tell) all of the instructions located here for a self contained .NET core deployment:
https://learn.microsoft.com/en-us/aspnet/core/publishing/iis
According to the page, I only need to run this command to get the module installed in IIS
DotNetCore.1.0.3_1.1.0-WindowsHosting.exe OPT_INSTALL_LTS_REDIST=0 OPT_INSTALL_FTS_REDIST=0
My web.config looks like this:
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\mypackage.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
But, the dotnet.exe is no where to be found on my server. What am I missing?
You seem to have published your application as a portable application (i.e. one that is trying to using shared runtime) but did not install the runtime. You either need to install the runtime, or you need make changes to your application (project.json/*.csproj really) to make it standalone in which case when publishing it will contain the runtime with it.
From web.config perspective - when publishing a standalone application the processPath is {application}.exe instead of dotnet
Here is how the deployed content looks like after publishing (on Azure Website):
/approot/packages - NuGet packages
/approot/src - all the source files from the solution
/approot/global.json - a list of global NuGet packages
/wwwroot/bin/AspNet.Loader.dll - the only .dll file in wwwroot folder
/wwwroot/css - front-end code
/wwwroot/lib - front-end code
/wwwroot/web.config - auto-generated
web.config generated during publishing
I'm wondering, how to make it deploy only the compiled output and not the source files?
If you are publishing through VS 2015, then make the following selection to not deploy the source files:
If you are not using VS, then you can use kpm pack command to achieve this. For example, following is the command that VS uses to create the package to deploy (You can enable Detailed logging in Tools | Options | Projects and Solutions | Build and Run, to see this)
"C:\Users\kiranchalla\.kre\packages\kre-clr-x86.1.0.0-beta2-10690\bin\kpm.cmd" pack --runtime KRE-CLR-x86.1.0.0-beta2-10690 --out "C:\Users\kiranchalla\AppData\Local\Temp\AspNetPublish\WebApplication5-91" --wwwroot-out wwwroot --no-source --configuration Release --quiet
Some info:
The effect of the above is that now your application is pre-compiled and you should see a package under the packages folder and the kre-app-base flag in web.config points to this package. Example:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="kpm-package-path" value="..\approot\packages" />
<add key="bootstrapper-version" value="1.0.0-beta1" />
<add key="kre-package-path" value="..\approot\packages" />
<add key="kre-version" value="1.0.0-beta2-10690" />
<add key="kre-clr" value="CLR" />
<add key="kre-app-base" value="..\approot\packages\WebApplication5\1.0.0\root" />
</appSettings>
</configuration>