This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Before ASP.NET Core 10, depending on the scenario: | |
| // Some attributes were ignored | |
| // Record struct parameters weren’t validated consistently | |
| // Constructor binding didn’t always trigger annotation checks. | |
| public readonly record struct UserInput( | |
| [Required] string Name, | |
| [Range(18, 99)] int Age); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var builder = WebApplication.CreateBuilder(args); | |
| .... | |
| builder.WebHost.UseUrls( | |
| "https://api.localhost:7001", | |
| "https://admin.localhost:7002", | |
| "https://frontend.localhost:7003"); | |
| var app = builder.Build(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "Kestrel": { | |
| "Endpoints": { | |
| "Api": { | |
| "Url": "https://api.localhost:7001" | |
| }, | |
| "Admin": { | |
| "Url": "https://admin.localhost:7002" | |
| }, | |
| "Frontend": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "profiles": { | |
| "ApiApp": { | |
| "commandName": "Project", | |
| "dotnetRunMessages": true, | |
| "applicationUrl": "https://api.localhost:7001", | |
| "environmentVariables": { | |
| "ASPNETCORE_ENVIRONMENT": "Development" | |
| } | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Before ASP.NET Core 10 | |
| // Order matters | |
| app.UseAuthentication(); | |
| app.UseRouting(); | |
| app.UseAuthorization(); // This might run before endpoint metadata is fully known | |
| app.MapControllers(); | |
| // With ASP.NET Core 10 | |
| // Order does not matter | |
| app.UseRouting(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| builder.Services | |
| .AddOpenTelemetry() | |
| .ConfigureResource(r => r.AddService( | |
| serviceName: builder.Environment.ApplicationName, | |
| serviceVersion: "1.0.0")) | |
| .WithMetrics(metrics => | |
| { | |
| // HTTP pipeline instrumentation (http.server.* etc.) | |
| metrics.AddAspNetCoreInstrumentation(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable, NgZone } from '@angular/core'; | |
| import { Observable } from 'rxjs'; | |
| import { Country } from './country.model'; | |
| @Injectable({ providedIn: 'root' }) | |
| export class CountryStreamService { | |
| private readonly url = 'https://localhost:5001/countries/stream'; | |
| constructor(private zone: NgZone) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var app = builder.Build(); | |
| app.UseCors(); | |
| // SSE endpoint in ASP.NET Core 10 | |
| app.MapGet("/countries/stream", (CountryService service, CancellationToken ct) => | |
| { | |
| var stream = service.StreamCountries(ct); | |
| // Each Country will be sent as an SSE event named "country" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class AudioAnalyzer | |
| { | |
| public static double AverageLevel(ReadOnlySpan<byte> data) | |
| => data.IsEmpty ? 0 : data.ToArray().Average(b => (double)b); | |
| } | |
| var bytes = track.RawData; // byte[] | |
| // Before C# 14 | |
| AudioAnalyzer.AverageLevel(bytes.AsSpan()); // implicit conversion |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Not allowed before C# 14, does not compile | |
| track?.Info = new Metadata(); | |
| track?.Info?.Duration = TimeSpan.Zero; | |
| playlist?["title"] = "My Mix"; | |
| // Must do this | |
| if (track != null && track.Info != null) | |
| { | |
| track.Info.Duration = TimeSpan.FromMinutes(5); | |
| } |
NewerOlder