Monday 4 March 2024

What is New in ASP.NET 9?

Leave a Comment

.NET 9, the successor to.NET 8, focuses on cloud-native apps and performance enhancements. It will get standard-term support (STS) for 18 months.

 
Serialization

In System.Text.Json, .NET 9 has new options for serializing JSON and a new singleton, making it easier to serialize using web defaults.

  1. Indentation options
    var options = new JsonSerializerOptions
    {
        WriteIndented = true,
        IndentCharacter = '\t',
        IndentSize = 2,
    };
    
    string json = JsonSerializer.Serialize(
        new { Value = 1 },
        options
        );
    Console.WriteLine(json);
    //{
    //                "Value": 1
    //}
JsonSerializeOptions includes new properties that let you customize the indentation character and indentation size of written JSON as given above.
 
Default web options
string webJson = JsonSerializer.Serialize(
    new { SomeValue = 42 },
    JsonSerializerOptions.Web // Defaults to camelCase naming policy.
    );
Console.WriteLine(webJson);
// {"someValue":42}
If you want to serialize with the default options that ASP.NET Core uses for web apps, use the new JsonSerializeOptions.Web singleton.

LINQ

Recently added to the toolkit are the CountBy and AggregateBy methods. These functions enable state aggregation by key, eliminating the necessity for intermediate groupings through GroupBy.

CountBy allows for swift computation of the frequency of each key. In the following example, it identifies the word that appears most frequently within a given text string.

string sourceText = """
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Sed non risus. Suspendisse lectus tortor, dignissim sit amet,
    adipiscing nec, ultricies sed, dolor. Cras elementum ultrices amet diam.
""";

// Find the most frequent word in the text.
KeyValuePair<string, int> mostFrequentWord = sourceText
    .Split(new char[] { ' ', '.', ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(word => word.ToLowerInvariant())
    .CountBy(word => word)
    .MaxBy(pair => pair.Value);

Console.WriteLine(mostFrequentWord.Key);

AggregateBy provides the capability to execute broader, more versatile workflows. Illustrated in the following example is the calculation of scores associated with a specified key.

(string id, int score)[] data =
    [
        ("0", 42),
        ("1", 5),
        ("2", 4),
        ("1", 10),
        ("0", 25),
    ];

var aggregatedData =
    data.AggregateBy(
        keySelector: entry => entry.id,
        seed: 0,
        (totalScore, curr) => totalScore + curr.score
        );

foreach (var item in aggregatedData)
{
    Console.WriteLine(item);
}
//(0, 67)
//(1, 15)
//(2, 4)
Cryptography

In the realm of cryptography, .NET 9 introduces a new one-shot hash method within the CryptographicOperations type. .NET offers various static "one-shot" implementations of hash functions and related functions, such as SHA256.HashData and HMACSHA256.HashData. Utilizing one-shot APIs is preferred due to their potential to optimize performance and minimize or eliminate allocations.

When a developer aims to create an API supporting hashing with the caller defining the hash algorithm, it typically involves accepting a HashAlgorithmName argument. However, employing this pattern with one-shot APIs necessitates switching over every potential HashAlgorithmName and subsequently utilizing the appropriate method. To address this issue, .NET 9 introduces the CryptographicOperations.HashData API. This API enables the generation of a hash or HMAC over an input as a one-shot operation, with the algorithm determined by a HashAlgorithmName.

static void HashAndProcessData(HashAlgorithmName hashAlgorithmName, byte[] data)
{
    byte[] hash = CryptographicOperations.HashData(hashAlgorithmName, data);
    ProcessHash(hash);
}

Conclusion

.NET 9 includes major enhancements designed specifically for cloud-native apps and performance optimization. With an emphasis on serialization, LINQ enhancements, and cryptographic breakthroughs, developers may use new features and APIs to streamline development processes and improve application security. Notable features include improved JSON serialization choices, powerful LINQ methods such as CountBy and AggregateBy, and simple CryptographicOperations.HashData API enables efficient hashing operations. As.NET 9 evolves, it promises to provide developers with powerful tools and capabilities to create modern, high-performance applications for a wide range of use cases.

 

Best ASP.NET Core 8.0.1 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 8.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.

0 comments:

Post a Comment