Slide 1

Slide 1 text

Hidden gems in ASP.NET Core & .NET Core 3.0 David Fowler @davidfowl Damian Edwards @damianedwards

Slide 2

Slide 2 text

First, the usual suspects SingleFileExe!

Slide 3

Slide 3 text

Core CLR Let’s start at the bottom

Slide 4

Slide 4 text

GC limits & new defaults • Can now set limits for how much memory the GC has available to it • Limits can be % or size based • Set via environment variables • Runtime API to get limits: GC.GetGCMemoryInfo • New defaults use much less memory by default

Slide 5

Slide 5 text

AssemblyLoadContext • New plugin model: AssemblyDependencyResolver • Determines assembly paths given an application path and assembly name • Unloadable contexts, meaning you can now unload assemblies (yay!) • Support for resolving native and managed assemblies • Not an isolation boundary for security purposes • Logical successor to AppDomains • DEMO

Slide 6

Slide 6 text

Startup hooks • Register code to execute before Program.Main • Set an env var with assembly name or full path • Assembly must contain an internal type named StartupHook with a public static Initialize method • DEMO

Slide 7

Slide 7 text

Diagnostics improvements • New dotnet global diag tools: counters, trace, dump • Default diagnostics pipe open on every .NET Core process • Runtime APIs for accessing runtime counters for GC, ThreadPool, etc. • New “dumpasync” sos command • Dump on crash: export COMPlus_DbgEnableMiniDump=1 • DEMO

Slide 8

Slide 8 text

Publish options: ReadyToRun (pre-JIT) • During publish, generate “ReadyToRun” native images for your app • A form of AOT or pre-JIT similar to ngen • Slightly larger file sizes but faster startup • Tiered Compilation will re-JIT after startup to improve throughput dotnet publish /p:PublishReadyToRun=true -r win-x86

Slide 9

Slide 9 text

Publish options: SingleFile • Publish your app to a single executable file for selected platform • Produces a bootstrapper executable with your app files embedded • Self-extracts on first launch to the user profile and runs from there dotnet publish /p:PublishSingleFile=true –r win-x86 DEMO

Slide 10

Slide 10 text

Publish options: Assembly linking • Linker tech from Mono incorporated into the SDK • Trims unused code from your code and your dependencies • Configurable to keep assemblies the tool can’t detect you’re using dotnet publish /p:PublishTrimmed=true –r win-x86 DEMO

Slide 11

Slide 11 text

Native interop improvements • Managed C++ CLI • COM interop • WinRT • NativeLibrary • New API that’s the imperative equivalent of DllImport • Uses the CLR host logic to find native libraries from known locations • Platform agnostic LoadLibrary and GetProcAddress • SAMPLES

Slide 12

Slide 12 text

Misc. for CoreCLR • ARM64 support (Linux only) • Hardware intrinsics (UTF8 encoding is optimized using these now)

Slide 13

Slide 13 text

BCL APIs for all

Slide 14

Slide 14 text

IAsyncDisposable • Ability to call async methods in objects’ dispose methods in a non- blocking way • Implemented on Stream, StreamReader, and other types that do I/O • Use with C# 8 await using

Slide 15

Slide 15 text

Distributed tracing • Updates to Activity type to support W3C Trace-Context • On by default • Provides correlated logs for requests that span multiple processes • Integrates with Open Telemetry (prev. Open Census) • DEMO

Slide 16

Slide 16 text

Runtime feature detection • IsDynamicCodeCompiled • Is the generated code optimized or interpreted (JIT vs interpreter) • IsDynamicCodeSupported • Can I generate code at runtime (RefEmit) • Used by serializers, DI containers, ORMs, and other dynamic code generators

Slide 17

Slide 17 text

ThreadPool enhancements • UnsafeQueueUserWorkItem • Advanced API that runs the enqueued work item without the current ExecutionContext • IThreadPoolWorkItem • Allows you to implement your own type that gets queued to the ThreadPool, which allows for advanced optimizations in appropriate cases • Used by Kestrel & ANCM and various parts of the BCL to reduce allocations for enqueued work

Slide 18

Slide 18 text

Misc. • Process.Kill(bool entireProcessTree) • BitOperations for advanced bit twiddling • Microsoft.VisualBasic is back • DbCommand • Async transactions

Slide 19

Slide 19 text

ASP.NET Core Up to the server now

Slide 20

Slide 20 text

UnixDomainSocket in Kestrel w/o libuv • Kestrel can now listen via a unix domain sockets without libuv • Provides higher speed transport for reverse-proxying scenarios, e.g. ngnix • Supported thanks to updates to System.Net.Sockets • Works on Linux and Windows 10

Slide 21

Slide 21 text

Static web assets in class libraries • Razor class libraries now support embedding static web assets • Allows you to include images, scripts, CSS, etc. along with views, pages, and components • Projects referencing the class library will be automatically configured to include the static assets from the class library • Reference the static assets from libraries via “_content/libname/…”

Slide 22

Slide 22 text

Access IIS server variables • IIS server variables are now available to ASP.NET Core applications when hosted in IIS • Allows you to read values about the server, the connection, and the current request

Slide 23

Slide 23 text

System.IO.Pipelines • Can now get access to the underlying pipeline instance backing each request/response • Pipeline is created and managed by the web server including allocation of memory buffers for reading and writing to the connection • Allows directly reading and writing to the request using Span and avoids going through the Stream adapter • HttpRequest.BodyReader • HttpRequest.BodyWriter

Slide 24

Slide 24 text

New networking abstractions (Bedrock) • Kestrel's transport layer is now isolated as a public API • You can implement your own servers and clients based on Kestrel's high-performance networking stack • Handles the networking essentials, e.g. exposing endpoints, managing memory, etc. • Used in SignalR .NET client • WebSocket pipe abstraction • DEMO

Slide 25

Slide 25 text

Routing HostMatcherPolicy and HostAttribute • Ability to have route matching consider the host and port • Can be used anywhere Endpoint Routing is used for dispatching • e.g. could isolate a specific Controller to only match when served via a specific host name • Can be useful in multi-tenancy scenarios, or when exposing extra endpoints on internal-only ports • DEMO

Slide 26

Slide 26 text

Support for trailing headers • Support for HTTP response trailers • Required to support gRPC • Literally response headers that are sent *after* the response body • Useful for sending information to client that is dependent on body, e.g. server timing

Slide 27

Slide 27 text

Developer exception page improvements • Support for non-HTML aware clients, error will be returned in plaintext • Extensibility via IDeveloperPageExceptionFilter to allow for custom views per exception type, e.g. Entity Framework Core's specific exception page will move to use this instead • DEMO

Slide 28

Slide 28 text

Compression improvements • Previously, compression was disabled over HTTPS connections due to security concerns, e.g. CRIME, BREACH • Requests can now declare they're safe for compression • Static files middleware utilizes this to enable compression over HTTPS • Compression middleware now supports Brotli by default • DEMO

Slide 29

Slide 29 text

Microsoft.Extensions.* That other layer

Slide 30

Slide 30 text

Systemd integration • Linux equivalent to support for Windows Services hosting • Supports ASP.NET Core and Worker Service apps • Handles lifetime management of the application including start, ready, shutdown • Interacts with sd_notify • Community contributed and coming in preview7

Slide 31

Slide 31 text

IConfigurationRoot.GetDebugView • Provides a way to dump all configuration values and their sources • Useful for debugging configuration issues • DEMO

Slide 32

Slide 32 text

ServiceProviderOptions.ValidateOnBuild • Validates when the container is built, that dependencies or all registered types are fully satisfied • Enabled by default in Development environment • Causes the app to fail on startup rather than when an invalid service is first requested • DEMO

Slide 33

Slide 33 text

Options validation • Support for validation of Options types in DI during app startup • Validate using custom logic or existing validation attributes • Use to ensure all require configuration is present during app startup • Usually better to fail fast on boot rather than fail later during a request

Slide 34

Slide 34 text

LoggerFactory.Create • New API to easily enable creating a logger factory in non-hosted scenarios, i.e. when a DI container is not in use • Makes using ILogger in other app types much easier, e.g. console apps, WinForms, etc. • DEMO

Slide 35

Slide 35 text

StreamConfigurationSource • Enables reading configuration from stream-based sources • Existing file-based providers have been refactored to extract stream- based implementations as well, e.g. JSON • Means you can now ready JSON configuration from other sources such as embedded resources, KeyVault, blobs, etc.

Slide 36

Slide 36 text

Thank you! • Follow us on twitter: • @davidfowl • @damianedwards • Code samples and this deck will be available at • https://github.com/davidfowl/Core3HiddenGems