ASP.NET Core 3 Things I Learned / Pro Tips / Migration to ASP.NET Core 3


Moving from ASP.NET Core 2 to ASP.NET 3 (Migration) there were a number of 'things' I picked up and decided I would document as I moved through the application. New Program.cs and Startup.cs changes, new logging configuration changes, and default ASP.NET Core tips included.

- December 9, 2019

Rest of the Story:

  • Starting up the web application with dotnet run

  • there are CLI parameters that you can use to set environment, port etc.

  • uses Kestrel to host the application

  • windows authentication cannot be turned on with dotnet run

  • can configure default behavior from within Visual Studio project properties

  • Debug, select Launch “Project” then you can set the App Url, default page, etc.

  • Hitting debug F5 within Visual Studio when the {Project Name} profile is selected hosts the application on Kestrel similar to dotnet run

  • dotnet run –e development will set the environment variable to ‘production’ (

  • in the following image you can see that I set the default environment for Kestrel runs to Development

ASPNETCORE_ENVIRONMENT Development image

  • JSON properties are now lower case in asp.net core (i.e. when return json from a method Json({object here}) would have all properties changed to lower case)

  • use the following code to avoid camel case names by default 

services.AddControllersWithViews()        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

{"Id":9000,"FullName":"John Smith"} would be serialized to {"id":9000,"fullName":"John Smith"}       

  • Debug ASP.NET Core apps in Visual Studio IIS/IISExpress etc. reference here        
  • After setting a application within IIS, setting authentication to windows, selecting application pool with no managed code – you may get the following…

The in process request handler, Microsoft.AspNetCore.Server.IIS, was not referenced in the application.

image

IWebHostbuilder remains for backward compatibility only, and was used to encapsulate DI, Logging and configuration.  This was used in earlier versions of ASP.NET Core 3 for http workloads.

The default .NET Core 3 ASP.NET Core template sets up by default in the program.cs IHostBuilder, which is the recommended for all app types.  Documentation can be found at docs.microsoft.com.  A host can encapsulates dependency injection, logging, configuration, IHostedService implementations.  Hosting is no longer bound to Kestrel and no longer bound to ASP.NET Core (oddly, this means you can start a host that doesn’t require Kestrel and doesn’t even need the ASP.NET Core Framework)

CreateHostBuilder has the following defaults

----- Environment variables prefixed with "DOTNET_". ----- Command-line arguments.

  • Loads app configuration from:

----- appsettings.json. ----- appsettings.{Environment}.json. ----- Secret Manager when the app runs in the Development environment. ----- Environment variables. ----- Command-line arguments.

  • Adds the following logging providers:

----- Console ----- Debug ----- EventSource ----- EventLog (only when running on Windows)

ConfigureWebHostDefaults

  • Loads host configuration from environment variables prefixed with "ASPNETCORE_".
* the startup constructor use to look like public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) * new to ASP.NET Core 3 it is no longer possible to inject ILogger in Startup.cs and Program.cs (Reference: Github) * Another reference to this change specifically calls this out * Changes with ASP.NET Core 3 to Program.cs / Startup.cs Reference: Andrew Lock