introduce
The official version of vs2022 was launched yesterday. It is estimated that many people have downloaded and started to create. Net 6. In this section, I will briefly introduce some changes to. Net 6.
text
The most obvious changes brought by. Net6 this time are:
- With top-level statements, we can't see Program.Main().
- Implicit using instructions, which means that the compiler will automatically add a set of using instructions according to the project type.
- The Startup file was removed.
var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.Run();
For this change, you may think it is an optimization brought by C# 10, but my understanding is that. Net6 makes it easier for novices to get started. In the early stage, we used. Net core version 2 / 3 / 5. We also explained to newcomers that the system Startup portal Program.Main() is not needed after the configuration is split into two files. Program.cs and Startup.cs, although separation of concerns is achieved, However, it will be difficult for newcomers to understand this time. When we discuss Startup, we don't need to explain how to call the two agreed methods. Even if they don't explicitly implement the interface, they can be called.
Let's look at the previous syntax. We have a lot of nested lambda, and the code looks very complex.
var hostBuilder = Host.CreateDefaultBuilder(args) .ConfigureServices(services => { services.AddControllers(); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.Configure((ctx, app) => { if (ctx.HostingEnvironment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", () => "Hello World!"); endpoints.MapRazorPages(); }); }); }); hostBuilder.Build().Run();
After upgrading to. Net 6, we can use a simpler API to implement it.
var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); var app = builder.Build(); app.MapGet("/", () => "Hello World!"); app.Run();
contrast
Let's compare the syntax of the two versions
Add service to DI container
var hostBuilder = Host.CreateDefaultBuilder(args); hostBuilder.ConfigureServices(services => { services.AddControllers(); services.AddSingleton<MyThingy>(); }) var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddSingleton<MyThingy>();
Logging
var hostBuilder = Host.CreateDefaultBuilder(args); hostBuilder.ConfigureLogging(builder => { builder.AddFile(); }) var builder = WebApplication.CreateBuilder(args); builder.Logging.AddFile();
Serilog integration
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseSerilog() // <-- Add this line .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); builder.Host.UseSerilog();
See the effect from the completed
public interface IHelloService { string Hello(bool isHappy); } public class HelloService : IHelloService { public string Hello(bool isHappy) { var hello = $"Hello World"; if (isHappy) return $"{hello}, you seem to be happy today"; return hello; } } using MinimalApiDemo; using System.Security.Claims; var builder = WebApplication.CreateBuilder(args); builder.Services.AddScoped<IHelloService, HelloService>(); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.MapGet("/Hello", (bool? isHappy, IHelloService service) => { if (isHappy is null) return Results.BadRequest("Please tell if you are happy or not :-)"); return Results.Ok(service.Hello((bool)isHappy)); }); app.Run();
epilogue
I won't map the effect. Let's experiment by ourselves. We'll see the situation later. If I have time, I'll give you an in-depth analysis of WebApplication and WebApplication builder.
Finally, you are welcome to pay attention to my blog, https://github.com/MrChuJiu/Dppt/tree/master/src Welcome, Star
Contact author: Jia Qun: 867095512 @ mrchujiu