头图

.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 API controller action parameters from the service;
  • Dependency injection for SignalR hub methods;
  • Provide endpoint descriptions and summaries for minimal APIs;
  • Bind arrays and StringValues from headers and query strings in a minimal API;
  • Custom cookie consent value.

For more details on the planned ASP.NET Core work for .NET 7, see the full ASP.NET Core roadmap for .NET 7 on GitHub.

start using

To get started with ASP.NET Core in .NET 7 Preview 2, install the .NET 7 SDK .

If you are using Visual Studio on Windows, we recommend installing the latest Visual Studio 2022 Preview . Visual Studio for Mac support for .NET 7 Preview is not yet available, but will be coming soon.
To install the latest .NET WebAssembly build tools, run the following command from an elevated command prompt: dotnet workload install wasm-tools.

Upgrade an existing project

To upgrade an existing ASP.NET Core app from .NET 7 Preview 1 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 the full list of breaking changes in ASP.NET Core for .NET 7.

Infer API controller action parameters from a service

Parameter bindings for API controller actions now bind parameters via dependency injection when the type is configured as a service. This means that the [FromServices] attribute no longer needs to be explicitly applied to parameters.

 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;
})

Dependency Injection for SignalR Hub Methods

SignalR hub methods now support injecting services via 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;
}

You can disable this feature by setting DisableImplicitFromServicesParameters:

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

To explicitly mark parameters to be bound from a configured service, use the [FromServices] attribute:

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

Provides endpoint descriptions and summaries for the Minimal API

The Minimal API now supports annotating operations with descriptions and summaries for OpenAPI specification generation. You can set these descriptions and digests for route handlers in your Minimal API application using extension methods:

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

Or set the description or summary via 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 Minimal API

With this release, you can now bind values in HTTPS headers and query strings to arrays of primitive types, arrays of strings, 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]}")

You can also bind query strings or header values to arrays 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]}")

Custom cookie consent value

You can now use the new CookiePolicyOptions.ConsentCookieValue property to specify a value for tracking whether a user agrees to a cookie usage policy.

Thanks to @daviddesmet for contributing this improvement!

Request feedback on IIS Volume Shadow Copy

In .NET 6, we added experimental support for shadow copying application assemblies to the ASP.NET Core Module (ANCM) for 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 the application offline file , but this is sometimes inconvenient or impossible. Shadow copies allow application assemblies to be updated by copying assemblies while the application is running.

You can enable shadow copying by customizing the ANCM handler settings in web.config:

 <?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>

We're working on making Shadow Copy in IIS a feature of ASP.NET Core in .NET 7, and we're looking for more feedback on whether the feature meets user requirements. If you deploy ASP.NET Core to IIS, try using Shadow Copy and share your feedback with us on GitHub.

Summarize

We hope you enjoy the preview of ASP.NET Core in .NET 7. Let us know what you think of these new improvements by filing an issue on GitHub.

Thanks for trying out ASP.NET Core!

Click for what's new in the ASP.NET Core documentation


微软技术栈
418 声望994 粉丝

微软技术生态官方平台。予力众生,成就不凡!微软致力于用技术改变世界,助力企业实现数字化转型。