ASP.NET Core deploy only applicable appsettings.{EnvironmentName}.json - asp.net-core

I've made my first proof of concept ASP.NET Core application, I have 5 appsettings files:
appsettings.json
appsettings.Development.json
appsettings.Test.json
appsettings.Staging.json
appsettings.Production.json
I'm running the app in IIS so I actually have web.config files so that I can set the ASPNETCORE_ENVIRONMENT environment variable on a per application basis:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<aspNetCore processPath="dotnet" >
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development"></environmentVariable>
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
I have 4 web.config transforms so I can publish from Visual studio, and it will set the correct environment variable value for each environment.
When I publish to each environment though it publishes all the appsettings files. Is it possible to get it to publish on the root appsettings.json and the applicable environment specific one, but omit the others?

No, it's not possible. ASP.NET Core is not like ASP.NET. In ASP.NET, you literally published for the environment; if you wanted to switch to a different environment, you'd need to republish. ASP.NET Core is published for all environments. The same published app can be picked up and moved to any environment, without change. The actual environment is generally externalized, such as via and environment variable, and can be changed on a whim, without requiring new code to be deployed. This is actually a feature of ASP.NET Core.
Now, the way you're handling the environment variable does somewhat make it dependent on the publish, but that's just modifying the web.config, which itself only has meaning when deploying to IIS. ASP.NET Core itself doesn't care about or use web.config, and honestly doesn't even care about or use release configurations.

Related

What are the config for NewRelic ASP.NET Core appsettings.json

The settings of referenced here was introduced as
Here are instructions for enabling the agent for .NET Framework and.NET Core.
https://docs.newrelic.com/docs/apm/agents/net-agent/installation/install-net-agent-windows/#app-config
It shows the following configuration:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="NewRelic.AgentEnabled" value="true" />
<add key="NewRelic.AppName" value="DataServices" />
</appSettings>
But for ASP.NET Core, you don't use XML configuration, you use appsettings.json:
What are the configuration for ASP.NET Core?
Can the configuration be added to the Configuration of Azure Web App Service rather than in the local appsettings.json ? And if so, what are the keys?
In the doc page you provide, it is explained how to set it up in dotnet core.
You don't need to add anything to the appsettings file; just set an environment variable. Beware that it must be set only in the project's scope or all dotnet core applications running on the host will be monitored.
CORECLR_ENABLE_PROFILING=1
They provide a helpful link to a page with even more details: https://discuss.newrelic.com/t/setting-net-core-agent-environment-variables-per-process/157750
In addition to the methods they mention, you could also put the variable in a .env file and load it, for example as explained here https://dusted.codes/dotenv-in-dotnet

Unable to run my .net core app in local as well as on server with IIS

I created a .net core api (version 2.2.0) and I'm getting the following error when I try to run on IIS.
The application process failed to start The application process
started but then stopped The application process started but failed to
listen on the configured port
I verified my web config and it appears to have correct set of values.Enabled logging and log file comes as empty.
Event log shows the following error.
I installed hosting bundle and checked still getting same error. Also tried giving permission to IIS_IUSRS to the output folder. It didn't make any difference. Having said that error code "'0x80004005' " suggests a permission error. Still couldn't figure it out. Appreciate any help !
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=".\MyApi.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
</system.webServer>
</location>
</configuration>`enter code here`
<!--ProjectGuid: 4d7644bb-9348-46f9-8397-95f01e03d599-->
Make sure you publish the site properly in iis. Your site root folder has enough permission to access it by iis. assign the iis_iusrs and iusr permission to the site folder. your application pool identity is set application pool identity or local system. anonymous authentication is enabled. and you installed the iis asp.net feature. make sure your site binding is correct and the application pool is using correct .net version and running under integrated application pool.
The reason behind the error message
The HTTP Error 502.5 - Bad Gateway and HTTP Error 502.5 - Process
Failure error messages occur in ASP.NET Core when IIS fails to execute
the dotnet process.
.NET Core Runtime is not installed
web.config file has not been transformed
To resolve this issue you could refer one the below-suggested way:
Install the .NET Core Runtime
The most common reason for this to occur is when you haven't installed the .NET Core runtime on the server.
You can download the latest .NET Core runtime from Microsoft's .NET download page.
After installing Bundle stop iis and start again.
Publish a Self-Contained Deployment
If you don't want to install the .NET Core Runtime. An alternative for .NET Core web applications is to publish them in the Self-Contained deployment mode, which includes the required .NET Runtime files alongside your application.
If you go with this option, you'll also need to choose a target runtime: win-x86, win-x64, osx-x64, or linux-x64. Because self-contained deployments are not portable.
Transform your web.config file
Another reason for this error to occur is when you deploy an untransformed web.config file.
This is likely to be your issue if you had a previously working web application and merely deployed a new version of it.
In ASP.NET Core applications, the web.config file contains a handler that directs requests to the AspNetCoreModule and an aspNetCore element that defines and configures the ASP.NET Core process to execute your web application.
Here is a minimal web.config file for an ASP.NET Core application:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true"
stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>
The issue is The untransformed web.config contains the variables %LAUNCHER_PATH% and %LAUNCHER_ARGS% rather than the correct paths. When IIS tries to run ASP.NET Core, it uses %LAUNCHER_PATH% and %LAUNCHER_ARGS% rather than the correct path and arguments.
To fix the HTTP Error 502.5 in ASP.NET Core, you need to transform the web.config and replace the untransformed web.config file on the IIS web server.
steps to transform web.config file:
This transformation takes place when you choose to publish your web application. The transformed web.config ends up in the published output folder. Therefore, you simply need to publish your web application and copy the resulting web.config file onto the server.
In a transformed web.config file, the aspNetCore element will look something like this:
<aspNetCore processPath="dotnet" arguments=".\MyApplication.dll" stdoutLogEnabled="true"
stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
%LAUNCHER_PATH% has been replaced by dotnet and %LAUNCHER_ARGS% has been replaced by the path to the main web application dll .\MyApplication.dll.
links:
Publish an ASP.NET Core app to IIS

ASP.NET Core 1.0 Web.Config Issue

My first stab at trying to implement an ASP.NET Core Application to Hosted IIS Server.
I believe I could a malformed XML aspNetCore Attribute of the web.config
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
%LAUNCHER_PATH% is a placeholder.
based on this information
I changed the processPath="dotnet" and arguments=".\WebDevX1.dll" however running with that produces 502.5 Error. Running with just placeholders works fine. It was my understanding that I need to change those placeholders in order to publish to the Hosted Server.
Whether I modify the placeholders in those 2 attributes or not, the web.config aspNetCore Attribute still shows an error
When I run locally from Visual Studio, the web.config reverts back to the placeholders:
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%"
ASP.NET Core 1.0.0 is installed locally
And my Host indicates they support .NET Core defaultly.
If someone has any insight as to what I'm missing I would really appreciate it.
You should have the publish-iis tool configured to run as a post-publish script. This tool replaces LAUNCHER_PATH and LAUNCHER_ARGS. You can find more details about this in my post about running ASP.Net Core applications with IIS.
After project.json to .csproj transition the publish-iis tool was converted to an MSBuild task (the TransformWebConfig task)

IIS WCF Service not using app.config

I have a .NET 3.5 WCF service hosted in IIS. The project service library has an app.config file with some configuration settings (Database connection strings, etc.). I deploy the project via a website project in Visual Studio, which generates a web.config to manage the endpoints. Is there a way I can put the app.config settings from my service library in to the web.config? The IIS hosted service seems to be using default values from the settings designer, and ignoring even an expliclty copied in app.config. I'm guessing this has something to do with the fact that a DLL can not utliize an app.config.
My service application is set up to pull the settings settings from the [MyAssembly].Properties.Settings.Default namespace.
Can you use external configuration files?
Your web.config:
<config>
...
<connectionStrings configSource="myConnections.config"></connectionStrings>
</config>
And then your external myConnections.config file:
<connectionStrings>
<add ... />
</connectionStrings>
You can have multiple external configuration files referenced from your main web.config file. See see this blog post for a nice explanation of how/why to do this.
I hope this helps!

WCF 4 Router Service configuration issue

I've created a WCF 4 Router Service and am hosting it in IIS7. It works fine on my development machine, but when I deploy to the QA server is returns the following message:
The configuration section 'routing' cannot be read because it is missing a section declaration
The section it's complaining about is the standard WCF4 routing section:
<routing>
<filters>
<filter name="MatchAllFilter1" filterType="MatchAll" />
</filters>
<filterTables>
<filterTable name="ServiceRouterTable">
<add filterName="MatchAllFilter1" endpointName="WCF_XXXService" />
</filterTable>
</filterTables>
</routing>
This should be stock standard, but I'm receiving the above error from IIS. Does anyone have any suggestions on how I can fix this issue?
Ok, finally found the solution here:
The root configuration files (the
machine.config file and the root
Web.config file) for the .NET
Framework 4 (and therefore ASP.NET 4)
have been updated to include most of
the boilerplate configuration
information that in ASP.NET 3.5 was
found in the application Web.config
files. Because of the complexity of
the managed IIS 7 and IIS 7.5
configuration systems, running ASP.NET
3.5 applications under ASP.NET 4 and under IIS 7 and IIS 7.5 can result in
either ASP.NET or IIS configuration
errors.
We recommend that you upgrade ASP.NET
3.5 applications to ASP.NET 4 by using the project upgrade tools in Visual
Studio 2010, if practical. Visual
Studio 2010 automatically modifies the
ASP.NET 3.5 application's Web.config
file to contain the appropriate
settings for ASP.NET 4.
READ MORE HERE:
aspnet4 breaking-changes
A quick Google turned up this:
If you upgrade a site to .NET 4, and don’t upgrade the application pool to use .NET 4, you will get this. The machine.config file for .NET 4 declares this section, and earlier version did not have it (unless you manually added it).
In IIS 7, with the site select, choose Basic Settings in the right pane. This will bring up a dialog that contains the name of your application pool. Then select the application pools tree node and the application pool from the list that shows up. Choose Basic Settings from the right pane here, and you can change the framework version.
From here
Can you try re-running aspnet_regiis from the FrameworkXXX\microsoft.net\v4.0.30319 folder and reset IIS?
Check the .Net framework associated with the site's app pool.
If you need to change it, run iisreset -force after to ensure changes take effect.