.NET 7 Preview 2 is now available and includes many major improvements to ASP.NET Core.

Here’s a summary of what’s new in this preview:

  • Infer the API controller operation parameters from the service
  • Dependency injection for SignalR hub methods
  • Provides an endpoint description and summary for the Minimal API
  • Bind arrays and StringValues from headers and query strings in the smallest API
  • Customize cookie consent values

For more details on working with ASP.NET Core planned for.net 7, see GitHub. The complete ASP.NET Core roadmap for NET 7.

Begin to use

To start using ASP.NET Core in.NET 7 Preview 2, install it. NET 7 SDK.

If you are using Visual Studio on Windows, we recommend installing the latest Visual Studio 2022 preview. Visual Studio for Mac. NET 7 preview support is not yet available, but is coming soon.

To install the latest.NET WebAssembly build tools, run the following command from the promoted command prompt:

dotnet workload install wasm-tools

Upgrading existing projects

To convert existing ASP.NET Core applications from. NET 7 Preview 1 Upgrade to. NET 7 Preview 2:

  • Update all Microsoft.AspNetCore.* package references to 7.0.0-Preview.2.*.
  • Update all Microsoft.extensions.* package references to 7.0.0-Preview.2.*.

See also a complete list of major changes in ASP.NET Core for.NET 7.

Infer the API controller operation parameters from the service

When the type is configured as a service, parameter bindings for API controller operations now bind parameters through dependency injection. This means that you no longer need to apply the [FromServices] attribute explicitly to the parameter.

Services.AddScoped<SomeCustomType>();

[Route("[controller]")]
[ApiController]
public class MyController : ControllerBase
{
    // Both actions will bound the SomeCustomType from the DI container
    public ActionResult GetWithAttribute([FromServices]SomeCustomType service) => Ok();
    public ActionResult Get(SomeCustomType service)=> Ok(); } you can disable this feature by setting DisableImplicitFromServicesParameters:  Services.Configure<ApiBehaviorOptions>(options => { options.DisableImplicitFromServicesParameters =true;
})
Copy the code

You can set the DisableImplicitFromServicesParameters to disable this feature:

Services.Configure<ApiBehaviorOptions>(options =>
{
     options.DisableImplicitFromServicesParameters = true;
})
Copy the code

Dependency injection for SignalR hub methods

The SignalR hub method now supports injection of services through dependency injection (DI).

Services.AddScoped<SomeCustomType>();

public class MyHub : Hub
{
    // SomeCustomType comes from DI by default now
    public Task Method(string text, SomeCustomType type) => Task.CompletedTask;
}
Copy the code

You can set the DisableImplicitFromServicesParameters to disable this feature:

services.AddSignalR(options =>
{
    options.DisableImplicitFromServicesParameters = true;
});
Copy the code

To explicitly mark the parameters to bind from the configured service, use the [FromServices] property:

public class MyHub : Hub
{
    public Task Method(string arguments, [FromServices] SomeCustomType type);
}
Copy the code

Provides an endpoint description and summary for the Minimal API

Minimal API now supports annotating operations using descriptions and summaries generated for the OpenAPI specification. You can set these descriptions and summaries for the route handler in the Minimal API application using the extension method:

app.MapGet("/hello", () = >...). .WithDescription("Sends a request to the backend HelloService to process a greeting request.");
Copy the code

Or by setting a property on the route handler delegate:

app.MapGet("/hello", [EndpointSummary("Sends a Hello request to the backend")]() => ...)

Bind arrays and StringValues from headers and query strings in the Minimal API

In this release, you can now bind values in HTTPS headers and query strings to primitive arrays, string arrays, or StringValues:

// Bind query string values to a primitive type array
// GET  /tags?q=1&q=2&q=3
app.MapGet("/tags", (int[] q) => $"tag1: {q[0]} , tag2: {q[1]}, tag3: {q[2]}")

// Bind to a string array
// GET /tags? names=john&names=jack&names=jane
app.MapGet("/tags", (string[] names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")

// Bind to StringValues
// GET /tags? names=john&names=jack&names=jane
app.MapGet("/tags", (StringValues names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")
Copy the code

You can also bind query strings or header values to an array of complex types, as long as the type has a TryParse implementation, as shown in the following example.

// Bind query string values to a primitive type array
// GET  /tags?q=1&q=2&q=3
app.MapGet("/tags", (int[] q) => $"tag1: {q[0]} , tag2: {q[1]}, tag3: {q[2]}")

// Bind to a string array
// GET /tags? names=john&names=jack&names=jane
app.MapGet("/tags", (string[] names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")

// Bind to StringValues
// GET /tags? names=john&names=jack&names=jane
app.MapGet("/tags", (StringValues names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")
Copy the code

Customize cookie consent values

. You can now use the new CookiePolicyOptions ConsentCookieValue attribute specifies to track the user whether or not the value of cookies use strategy.

Thanks to @Daviddesmet for contributing to this improvement!

Request feedback about IIS volume shadow replication

In.NET 6, we added experimental support for shadow copy application assemblies for THE ASP.NET Core module (ANCM) of IIS. When an ASP.NET Core application runs on Windows, the binaries are locked, so they cannot be modified or replaced. You can stop an application by deploying an application offline file, but sometimes this is inconvenient or impossible. Volume shadow replication allows application assemblies to be updated by copying them while the application is running.

You can enable volume shadow replication by customizing ANCM handler Settings in web.config:

<? The XML version = "1.0" encoding = "utf-8"? > <configuration> <system.webServer> <handlers> <remove name="aspNetCore"/> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/> </handlers> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".logsstdout"> <handlerSettings> <handlerSetting name="experimentalEnableShadowCopy" value="true" /> <handlerSetting name="shadowCopyDirectory" value=".. /ShadowCopyDirectory/" /> </handlerSettings> </aspNetCore> </system.webServer> </configuration>Copy the code

We are working on making volume shadow replication in IIS become. NET 7 is a feature of ASP.NET Core, and we are looking for more feedback on whether this feature meets user requirements. If you are deploying ASP.NET Core to IIS, try using volume shadow replication and share your feedback with us on GitHub.

To give feedback

We hope you like it. ASP.NET Core preview for NET 7. Let us know what you think of these new improvements by submitting a question on GitHub.

Thanks for trying ASP.NET Core!