Some news about .NET 10: BackgroundServices and new String Comparer

12/16/2024
2 minute read

Recently, there were two new features merged in .NET 10 I think are small little quality of life improvements: Avoid Blocking on startup with BackgroundServices and a new string comparer.

Avoid Blocking on startup with BackgroundServices

The first one (link here: #36063) is a small change in the way BackgroundServices are started in .NET 10. If you have a BackgroundService in your application, you might have noticed that it blocks the application startup until the service is started. This is because the StartAsync method is called synchronously in the IHostedService implementation. You could do tricks like such to come around this:

public class MyBackgroundService : BackgroundService
{
    private readonly IHostApplicationLifetime _appLifetime;

    public MyBackgroundService(IHostApplicationLifetime appLifetime)
    {
        _appLifetime = appLifetime;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await Task.Yield();
        // Do your work here
    }
}

Or wrap the whole thing in a Task.Run. But that is not really nice and easy to discover. In .NET 10, the BackgroundService class will now have a new virtual property called StartAsynchronously which is set to true by default. This means that the StartAsync method will be called asynchronously, and the application will not block on startup. You have to explicitly set it to false if you want the old behavior.

EDIT: BackgroundService will not get an additional property (aka StartAsynchronously) but the behavior will shift towards the async nature. Thanks @kapsiR for pointing that out.

New String Comparer

The second one (link here: #13979 describes the following issue, imagine you want to sort the following two strings: Windows 10 and Windows 7. Treating them purely as strings, Windows 10 would come before Windows 7 because the character 1 comes before 7. But there are contexts where you need to treat some part of the string as a number and sort it accordingly. This is where the new StringComparer comes in. It will allow you to sort strings as numbers, so Windows 10 will come after Windows 7.

var list = new List<string> { "Windows 10", "Windows 7" };
list.Sort(StringComparer.NumericOrdering); // The API is different than in the original post!

Another example, hinted in the ticket, is sorting IP addresses.

.NET 8 and Blazor United / Server-side rendering

New .NET and new Blazor features. In this blog post, I want to highlight the new features that are hitting us with .NET 8 in the Blazor world. So let's see what's new.

LINQ might get a "left join" operator in .net 10

LINQ has a Join operator, that basically translates to something like a SQL INNER JOIN. There is no built in operator for a LEFT JOIN in LINQ, but you can achieve the same result by using a combination of GroupJoin, SelectMany and DefaultIfEmpty. But there is a chance that this might change in the future, as there is a proposal to add a LeftJoin operator in .net 10.

A story about boxing / unboxing and string interpolation in C#

A story about boxing / unboxing and string interpolation in C#. What has string interpolation to do with boxing / unboxing and what's the impact?

An error has occurred. This application may no longer respond until reloaded. Reload x