In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Net 6 introduction and how compared with the previous version of the writing, in view of this question, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Introduction
The official version of vs2022 has been released, and it is estimated that many people have downloaded and started to create. Net 6 is beginning to taste. In this section, I will briefly introduce some changes to .net 6.
Text
The most obvious changes brought about by .Net6 are:
With the top-level statement, we can't see Program.Main ().
Implicit using directives, implicit using directives mean that the compiler automatically adds a set of using instructions based on the project type.
The Startup file was removed.
Var builder = WebApplication.CreateBuilder (args); var app = builder.Build (); app.Run ()
For such a change, you may think that this is the optimization brought by C# 10, but my understanding is that .net 6 in order to make it easier for beginners to start, we used the .net Core version on 2-3-5, and we had to explain to the newcomers that the system startup entry Program.Main () was no longer needed. Although the configuration was split into two files, Program.cs and Startup.cs, although the focus was separated, it would be difficult for newcomers to understand. When we talk about Startup, we don't have to explain how to call the two convention methods, even if they don't explicitly implement the interface.
Let's take a 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 the simpler API to implement it.
Var builder = WebApplication.CreateBuilder (args); builder.Services.AddControllers (); var app = builder.Build (); app.MapGet ("/", () = > "Hello World!"); app.Run (); comparison
Let's compare the grammar of the two versions.
Add service var hostBuilder = Host.CreateDefaultBuilder (args) to the DI container; hostBuilder.ConfigureServices (services = > {services.AddControllers (); services.AddSingleton ();}) var builder = WebApplication.CreateBuilder (args); builder.Services.AddControllers (); builder.Services.AddSingleton (); 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 () / / {webBuilder.UseStartup ();}); builder.Host.UseSerilog (); 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 () / / Add services to the container.builder.Services.AddControllers (); / / Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbucklebuilder.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 () This is the answer to the question about the introduction of .net 6 and the comparison with the previous version. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 204
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.