Professional Development – 2023 – Week 47

Image Credit: https://www.flickr.com/photos/54585499@N04/

.NET

What Is .NET Aspire? The Insane Future of .NET! (via Nick Chapsas)

  • This is a turnkey framework for building distributed applications in the cloud. The challenge with distributed applications is getting all the pieces talking to each other both locally and when deployed.
  • In the example, there’s a Blazor project (HTML) that depends on the API project.
  • The template has two projects — AppHost and ServiceDefaults. (As of Nov 2023, Rider doesn’t yet have support for the Aspire project type. The extra tooling in Visual Studio just saves you some typing — under the hood it’s just NuGet packages and project references.)
  • The simplification comes by running only the Aspire host rather than the front-end and the back-end. There’s also a built-in dashboard that helps you see all the moving pieces (environment variables, logs, console, containers, structured logs, traces).
  • You can enable Redis caching (which runs in a container) with a single line and some NuGet package additions.

How to Deploy .NET 8’s New .NET Aspire Stack (via Nick Chapsas)

Aspire is agnostic of deployment; it creates a manifest.json that describes what’s happening in your applications. From there, you can use tools such as AZD (Azure CLI) to package everything up for deployment in a cloud service.

The C# 12 Feature You Shouldn’t Use, Yet (via Nick Chapsas)

  • If your constructor is only setting backing fields, both Rider and Visual Studio will suggest using the primary constructor syntax.
  • One disadvantage is that it backing fields the compiler creates are not readonly. Another issue is that you can’t add logic to throw exceptions for null injections. This approach also breaks the naming convention of backing fields starting with an underscore.

The New Best Way To Search for Values in .NET 8 (via Nick Chapsas)

  • The use case is determining if a given string only has characters in a given array of characters.
  • Naive: Loop over each character and check if the array .Contains() it.
  • Better: Convert the string to a Span and check via .ContainsAnyExcept().
  • New in .NET 8: static readonly SearchValues<char> searchVals = SearchValues.C reate(stringOfChars). Then you use the same .AsSpan().ContainsAnyExcept() approach.