-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What's new in .NET 6 Preview 7 #6444
Comments
.NET SDK: C# Implicit Namespace Imports for C# ProjectsDoc issue: dotnet/docs#25066 We added support for implicit namespace imports for C# projects to reduce the amount of boilerplate in project templates by reducing the using statements needed in each C# file. For example, an empty web app looked like this: using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.MapGet("/", () => "Hello World!");
app.Run(); but now it's simplified to: var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.MapGet("/", () => "Hello World!");
app.Run(); |
ZipFile Respects Unix File PermissionsWe added support to If a zip archive doesn't contain file permissions (because it was created on Windows, or using a tool which didn't capture the permissions, like an earlier version of .NET) extracted files get the default file permissions, just like any other newly created file. This behavior didn't change. The Unix file permissions work with other zip archive tools as well, including: |
NativeMemoryWe added new native memory allocation APIs exposed via |
Serialization notifications and property ordering for System.Text.JsonThese features make it much easier to customize the behavior of JSON objects. Previously, this functionality required implementing a custom converter. Notifications for (de)serialization are useful for defaulting values and validation. To use them, implement one or more of the interfaces Here's an example that validates during both public class Person : IJsonOnDeserialized, IJsonOnSerializing
{
public string FirstName{ get; set; }
void IJsonOnDeserialized.OnDeserialized() => Validate(); // Call after deserialization
void IJsonOnSerializing.OnSerializing() => Validate(); // Call before serialization
private void Validate()
{
if (FirstName is null)
{
throw new InvalidOperationException("The 'FirstName' property cannot be 'null'.");
}
}
} The order properties are serialized in can now be controlled by applying the Here's an example that specifies JSON should be serialized in the order public class Person
{
public string City { get; set; } // No order defined (has the default ordering value of 0)
[JsonPropertyOrder(1)] // Serialize after other properties that have default ordering
public string FirstName { get; set; }
[JsonPropertyOrder(2)] // Serialize after FirstName
public string LastName { get; set; }
[JsonPropertyOrder(-1)] // Serialize before other properties that have default ordering
public int Id { get; set; }
} |
@tannergooding should there be something about dotnet/runtime#55776 here? although it's a little special. |
Yes. I will be adding it and working on the related blog post when I get back from vacation. |
Oops, didn't realize you were on vacation. 😺 |
Introducing System.Diagnostics PropagatorsDevelopers will be able to control how distributed tracing information is propagated Introduced DistributedContextPropagator propagator = DistributedContextPropagator.Current;
propagator.Inject(activity, carrier, (object theCarrier, string fieldName, string value) =>
{
// Extract the context from the activity then inject it to the carrier.
}); Configure propagation with different propagator// Set the current propagation behavior to not transmit any distributed context information in outbound network messages.
DistributedContextPropagator.Current = DistributedContextPropagator.CreateNoOutputPropagator(); |
Full Case Mapping Support in Globalization Invariant Mode.NET Core has been supporting the Globalization Invariant Mode for a while. This mode used to support only ASCII range characters case mapping in operations like |
When mentioning the improvement to Globalization Invariant Mode above it might be an opportunity to note the breaking change dotnet/docs#24849 |
@danmoseley the doc I linked is the published breaking change doc. |
@tarekgh ah my bad, I had forgotten they were both in the same breaking change doc. |
W^X (write xor execute) support for all platforms and architecturesThe runtime now has a mode in which it doesn't create any memory pages that are writeable and executable at the same time. All executable memory is mapped as read-execute only. This was already supported on macOS ARM64 before preview 7, because it is not allowed to create memory mappings that are writeable and executable at the same time on that platform at all. Preview 7 adds that for all the remaining platform. On these platforms, executable code generation / modification is done via separate read-write memory mappings. This is true for both JITted code and runtime generated helpers. These mappings are created at virtual memory addresses that are different from the executable code address and exist only for a very brief period of time when the writing is performed. For example, JIT now generates code into a scratch buffer that is copied into the executable memory using a single memory copy function call after the whole method is jitted. And the writeable mapping lifetime spans only the time of the memory copy. This new feature is disabled by default and it can be enabled by setting the environment variable |
Simplified call patterns for cryptographic operationsThe .NET encryption and decryption routines were designed around streaming, with no real concession for when the payload is already in memory. The new Encrypt- and Decrypt- methods on Previous versions: private static byte[] Decrypt(byte[] key, byte[] iv, byte[] ciphertext)
{
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
// These are the defaults, but let's set them anyways.
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
using (MemoryStream destination = new MemoryStream())
using (ICryptoTransform transform = aes.CreateDecryptor())
using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
{
cryptoStream.Write(ciphertext, 0, ciphertext.Length);
cryptoStream.FlushFinalBlock();
return destination.ToArray();
}
}
} With the new simplified methods: private static byte[] Decrypt(byte[] key, byte[] iv, byte[] ciphertext)
{
using (Aes aes = Aes.Create())
{
aes.Key = key;
return aes.DecryptCbc(ciphertext, iv);
}
} With the new Encrypt- and Decrypt-methods only the key property is used from the SymmetricAlgorithm instance. The new DecryptCbc method does support choosing the padding algorithm, but PKCS#7 is used with CBC so often that it's a default argument. If you like the clarity, just specify it: private static byte[] Decrypt(byte[] key, byte[] iv, byte[] ciphertext)
{
using (Aes aes = Aes.Create())
{
aes.Key = key;
return aes.DecryptCbc(ciphertext, iv, PaddingMode.PKCS7);
}
} |
System.Text.Json.Nodes.JsonNode support for
|
.NET 6 C# project templates use latest language idiomsWe updated .NET SDK templates to use latest C# language idioms when creating the project targeting .NET 6 framework.
|
Added APIs for exposing nullability information from reflectionRuntime Issue: dotnet/runtime#29723 API shapenamespace System.Reflection
{
public sealed class NullabilityInfoContext
{
public NullabilityInfo Create(ParameterInfo parameterInfo);
public NullabilityInfo Create(PropertyInfo propertyInfo);
public NullabilityInfo Create(EventInfo eventInfo);
public NullabilityInfo Create(FieldInfo parameterInfo);
}
public sealed class NullabilityInfo
{
public Type Type { get; }
// ReadState and WriteState could be different in case the member has nullability attribute affecting them
// For example for a non null property [MaybeNull] attribute makes ReadState nullable
public NullabilityState ReadState { get; }
// For example for a nulllable property [DisallowNull] attribute makes WriteState not null
public NullabilityState WriteState { get; }
// if the member is an array, then the nullability of the array elements null otherwise
public NullabilityInfo? ElementType { get; }
// if the member has generic argument(s) then nullability of each argument, else empty array
public NullabilityInfo[] GenericTypeArguments { get; }
}
public enum NullabilityState
{
Unknown, // Nullability not enabled
NotNull, // Not nullable value or reference type
Nullable // Nulable value or reference type
}
} UsageGetting top-level nullability informationprivate NullabilityInfoContext _nullabilityContext = new NullabilityInfoContext();
private void DeserializePropertyValue(PropertyInfo p, object instance, object? value)
{
if (value == null)
{
var nullabilityInfo = _nullabilityContext.Create(p);
if (nullabilityInfo.WriteState != NullabilityState.Nullable)
throw new MySerializerException($"Property '{p.GetType().Name}.{p.Name}'' cannot be set to null.");
}
p.SetValue(instance, value);
} Getting nested nullability informationclass Data
{
public string?[] ArrayField;
public Tuple<string?, object> TupleField;
}
private void Print()
{
Type type = typeof(Data);
FieldInfo arrayField = type.GetField("ArrayField");
FieldInfo tupleField = type.GetField("TupleField");
NullabilityInfoContext context = new ();
NullabilityInfo arrayInfo = context.Create(arrayField);
Console.WriteLine(arrayInfo.ReadState ); // NotNull
Console.WriteLine(arrayInfo.Element.State); // Nullable
NullabilityInfo tupleInfo = context.Create(tupleField);
Console.WriteLine(tupleInfo.ReadState); // NotNull
Console.WriteLine(tupleInfo.GenericTypeArguments [0].State); // Nullable
Console.WriteLine(tupleInfo.GenericTypeArguments [1].State); // NotNull
} |
Generic MathWe are previewing (as in they will not ship as "stable" with .NET 6) several new interfaces which enable operators to be used with generics: public static TSelf Sum<TSelf>(IEnumerable<TSelf> values)
where TSelf : INumber<TSelf>
{
TSelf result = TSelf.Zero;
foreach (var value in values)
{
result += value;
}
return result;
} This is powered by a new feature which allows |
System.Text.JsonUtf8JsonWriter "write raw" APIs (dotnet/runtime#1784)There are scenarios where customers want to integrate existing, "raw" JSON payloads when writing new JSON payloads with Utf8JsonWriter.
Sample code: JsonWriterOptions writerOptions = new() { WriteIndented = true, };
using MemoryStream ms = new();
using UtfJsonWriter writer = new(ms, writerOptions);
writer.WriteStartObject();
writer.WriteString("dataType", "CalculationResults");
writer.WriteStartArray("data");
foreach (CalculationResult result in results)
{
writer.WriteStartObject();
writer.WriteString("measurement", result.Measurement);
writer.WritePropertyName("value");
// Write raw JSON numeric value
byte[] formattedValue = FormatNumberValue(result.Value);
writer.WriteRawValue(formattedValue, skipValidation: true);
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject(); New synchronous stream overloads on
|
Gosh @SingleAccretion did a lot! |
One of them is listed twice but still hell of a job! |
@EgorBo - Feel free to add some performance improvement graphs for dynamic PGO/inline changes. |
Thanks for catching it. Deleted the line. |
What's new in .NET 6 Preview 7
This issue is for teams to highlight work for the community that will release .NET 6 Preview 7.
To add content, use a new conversation entry. The entry should include the team name and feature title as the first line as shown in the template below.
Preview 1: #5853
Preview 2: #5889
Preview 3: #5890
Preview 4: #6098
Preview 5: #6099
Preview 6: #6325
The text was updated successfully, but these errors were encountered: