Microsoft releases .NET 7 Preview 7, the last preview of the new version of the framework before the release of the first release candidate, This preview brings new improvements to the framework


Microsoft released .NET 7 Preview 7 on Tuesday. The company announced that this is the final preview of .NET 7 and that the next release will be the first release candidate (RC). This .NET 7 preview includes improvements to System.LINQ, Unix file permissions, low-level structures, p/Invoke source generation, code generation, and websockets. Preview 7 was tested with Visual Studio 17.4 Preview 1, which was also released Tuesday alongside the final Visual Studio v17.3 release. The release of the stable version of .NET 7 is scheduled for November 10, 2022.

When development of .NET 7 began, Microsoft explained to the community that this new version will finally unify all the disparate components of .NET development tools, allowing developers to build all types of applications – desktop, mobile, web and others – on the same base class library (BCL), the same execution engine and the same compilers. That was actually the goal of .NET 5 – the successor to the .NET Core offerings – when it debuted in November 2020. But development issues exacerbated by the pandemic prevented that from being achieved. objective.

Indeed, not all the planned elements have been integrated into .NET 5 and have been postponed until the arrival of .NET 6 in November 2021 as an LTS (Long Term Support) version. But even then, Microsoft’s overall unification effort was incomplete, as some components, such as .NET Multi-platform App UI (.NET MAUI), fell short of schedule. .NET MAUI has since reached general availability, and full unification is now expected in November. During the celebration of 20 years of .NET last February, Microsoft reiterated its intention to unify all framework components starting with .NET 7.

.NET 7 builds on the foundation established by .NET 6, which includes a unified set of core, runtime, and SDK libraries, a simplified development experience, and increased developer productivity. Key areas of focus for .NET 7 include better support for cloud-native scenarios, tools to make it easier to upgrade existing projects, and simplifying the developer experience by making it easier to work with containers,” wrote Jeremy Likness from the .NET team about this release. Basically, .NET 7 aims to make cloud-native application development and containerization easier.

As part of Preview 7, Likness said on Tuesday that the development team focused on improving System.LINQ, Unix file permissions, low-level structures, p /Invoke, code generation and websockets. All of these specific items are general advice provided on the Themes of .NET site. Below are the highlights of .NET 7 Preview 7:

Simplify ordering with System.LINQ

System.Linq now understands new methods, Order and OrderDescendingwhich are used to order a IEnumerable depending on T. IQueryable also supports these methods now.

Before the change

Previously, you had to call OrderBy/OrderByDescending by referring to the eigenvalue.

1
2
3

var data = new[] { 2, 1, 3 };
var sorted = data.OrderBy(static e => e);
var sortedDesc = data.OrderByDescending(static e => e);

After the change

Now you can write:

1
2
3

var data = new[] { 2, 1, 3 };
var sorted = data.Order();
var sortedDesc = data.OrderByDescending();

Support for Unix file modes

Previously, .NET had no built-in support for getting and setting Unix file permissions, which control which users can read, write, and execute files and directories. It is not always easy to manually invoke the syscalls, because some are exposed differently according to the distributions. For example, on Ubuntu you can have pin voke __xstaton RedHat status, And so on. According to the team, this is why having a first-class .NET API is important. In Preview 7, the team introduced a new type enum :

1
2
3
4
5
6
7
8

public enum UnixFileMode
{
    None,
    OtherExecute, OtherWrite, OtherRead,
    GroupExecute, GroupWrite, GroupRead,
    UserExecute, UserWrite, UserRead,
     ...
}

It also added the APIs File.GetUnixFileMode and File.SetUnixFileMode which allow to get and set the file mode to a path or a handle (file descriptors). As well as a new property on FileInfo and DirectoryInfo appointed UnixFileMode. There is also a new overload of Directory.CreateDirectory and a new property on FileStreamOptions to allow you to create a directory or file with a particular mode all at once.

According to the team, when you use them, umask is still applied, as if you had created the directory or file in your shell. Here is an example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

// Create a new directory with specific permissions
Directory.CreateDirectory("myDirectory", UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
 
// Create a new file with specific permissions
FileStreamOptions options = new()
{
    Access = FileAccess.Write,
    Mode = FileMode.Create,
    UnixCreateMode =  UnixFileMode.UserRead | UnixFileMode.UserWrite,
};
using FileStream myFile = new FileStream("myFile", options);
 
// Get the mode of an existing file
UnixFileMode mode = File.GetUnixFileMode("myFile");
 
// Set the mode of an existing file
File.SetUnixFileMode("myFile", UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);

Improvements to low-level structures: support for the ref field

.NET 7 runtimes now fully support fields ref in types ByRefLike (i.e. the structures ref). With this feature, types that previously required specialized management in runtimes (for example, Span and ReadOnlySpan) can now be fully implemented in C#.

Source generator LibraryImport P/Invoke

Source generator LibraryImport is now available in a supported way for all users. The result of more than 18 months of work, this source generator is designed to directly replace the majority of uses of DllImport, both in the runtime product and in the user code. The team reports that .NET libraries have all adopted LibraryImport and come with code from marshalling source generated from .NET 7 Preview 1.

The source generator comes bundled with the .NET 7 target framework moniker (TFM) and is readily available for consumption. To take advantage of source-generated matching, replace the use of DllImport by LibraryImport. There are analyzers and correctors that can help you with this process. Here is an example :

Before LibraryImport support

1
2
3
4
5

public static class Native
{
    [DllImport(nameof(Native), CharSet = CharSet.Unicode)]
    public extern static string ToLower(string str);
}

With LibraryImport support

1
2
3
4
5

public static class Native
{
    [LibraryImport(nameof(Native), StringMarshalling = StringMarshalling.Utf16)]
    public static partial string ToLower(string str);
}

The team indicated that there is a parser and a code corrector to automatically convert your attributes DllImport in LibraryImport. For preview 7, this is an opt-in. add dotnet_diagnostic.SYSLIB1054.severity=suggestion your file EditorConfig to activate the conversion analyzer as a diagnostic.

Improved behavior of ClientWebSocket

Previously, ClientWebSocket did not provide any details on the upgrade response. However, the response headers and status code information can be important in failure and success scenarios. If it fails, the status code can help distinguish between recoverable and unrecoverable errors (the server doesn't support websockets at all, or it's just a small transient error).

Headings can also contain additional information on how to handle the situation. Headers are also useful even if a websocket connects successfully. they can contain a token linked to a session, information relating to the version of the sub-protocol, etc.

Support

.NET 7 is a Short Term Support (STS) release, meaning it will get free support and fixes for 18 months from its release date. The team says the quality of all versions is the same. The only difference is the duration of the assistance. Finally, the team explains that .NET 7 Preview 7 has been tested with Visual Studio 17.4 Preview 1.

She recommends using the preview channel builds if you want to try .NET 7 with Visual Studio family products. If you're on macOS, she recommends using the latest version of Visual Studio 2022 for Mac. Microsoft plans to launch .NET 7 at an event to be held November 8-10, 2022.

Source: .NET 7 Preview 7

And you?

What do you think of the new features and improvements introduced by .NET 7 Preview 7?

See as well

Microsoft announces .NET 7 Preview 6, the version of the framework for creating applications, improves type converters, customizes JSON contracts

Microsoft announces .NET 7 Preview 4, the version of the framework for creating applications, it fixes bugs and includes updates

Microsoft announces .NET 7 Preview 3, version of framework for building apps, improves startup time with Write-Xor-Execute enabled by 10-15%

Microsoft Releases First Preview of .NET 7 with Continued JIT Compiler Optimizations, New APIs, and Support for More Hot Reload Scenarios

Microsoft celebrates 20 years of .NET, its development framework, .NET repos reportedly in top 30 fastest-velocity open source projects on GitHub since 2017

Leave a Comment