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

    View full-size slide

  2. First, the usual suspects
    SingleFileExe!

    View full-size slide

  3. Core CLR
    Let’s start at the bottom

    View full-size slide

  4. 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

    View full-size slide

  5. 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

    View full-size slide

  6. 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

    View full-size slide

  7. 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

    View full-size slide

  8. 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

    View full-size slide

  9. 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

    View full-size slide

  10. 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

    View full-size slide

  11. 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

    View full-size slide

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

    View full-size slide

  13. BCL
    APIs for all

    View full-size slide

  14. 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

    View full-size slide

  15. 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

    View full-size slide

  16. 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

    View full-size slide

  17. 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

    View full-size slide

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

    View full-size slide

  19. ASP.NET Core
    Up to the server now

    View full-size slide

  20. 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

    View full-size slide

  21. 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/…”

    View full-size slide

  22. 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

    View full-size slide

  23. 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

    View full-size slide

  24. 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

    View full-size slide

  25. 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

    View full-size slide

  26. 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

    View full-size slide

  27. 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

    View full-size slide

  28. 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

    View full-size slide

  29. Microsoft.Extensions.*
    That other layer

    View full-size slide

  30. 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

    View full-size slide

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

    View full-size slide

  32. 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

    View full-size slide

  33. 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

    View full-size slide

  34. 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

    View full-size slide

  35. 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.

    View full-size slide

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

    View full-size slide