Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hidden gems in ASP.Core and .NET Core 3.0

Hidden gems in ASP.Core and .NET Core 3.0

You've likely heard about the headline features in .NET Core 3.0 including Blazor, gRPC, and Windows desktop app support, but what else is there?

This is a big release so come and see David Fowler and Damian Edwards from the .NET Core team showcase their favorite new features you probably haven't heard about in this demo-packed session.

David Fowler

June 19, 2019
Tweet

More Decks by David Fowler

Other Decks in Technology

Transcript

  1. Hidden gems in ASP.NET Core & .NET Core 3.0 David

    Fowler @davidfowl Damian Edwards @damianedwards
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. Misc. for CoreCLR • ARM64 support (Linux only) • Hardware

    intrinsics (UTF8 encoding is optimized using these now)
  11. 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
  12. 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
  13. 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
  14. 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
  15. Misc. • Process.Kill(bool entireProcessTree) • BitOperations for advanced bit twiddling

    • Microsoft.VisualBasic is back • DbCommand • Async transactions
  16. 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
  17. 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/…” <script src="_content/RazorLib1/hello.js"></script>
  18. 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
  19. 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<T> and avoids going through the Stream adapter • HttpRequest.BodyReader • HttpRequest.BodyWriter
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. IConfigurationRoot.GetDebugView • Provides a way to dump all configuration values

    and their sources • Useful for debugging configuration issues • DEMO
  27. 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
  28. 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
  29. 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
  30. 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.
  31. Thank you! • Follow us on twitter: • @davidfowl •

    @damianedwards • Code samples and this deck will be available at • https://github.com/davidfowl/Core3HiddenGems