DOTNET Core MVC (一)

 

 

 In the form of the console, .net core mvc operation codes,

Host.CreateDefaultBuilder (args) 
                .ConfigureWebHostDefaults (WebBuilder => 
                { 
                    webBuilder.UseStartup <the Startup> (); // specified to start to use the host network type. 
                });

Return to the default IHostBuilder, program initialization.

 

According to the official documentation: The way to do so many things:

The ContentRootPath to the GetCurrentDirectory () Results

Loading host IConfiguration from the environment variable "DOTNET_" prefix

From the command line parameters provided by the host load IConfiguration

From "appsettings" and "appsettings" load the application IConfiguration. [EnvironmentName] json "

Use the entry assembly is loaded from a user application IConfiguration confidential when EnvironmentName "development"

Application environment variables loaded from IConfiguration

From the command line parameters provided load application IConfiguration

The ILoggerFactory configured to log in to the console, debugging and event source output

When EnvironmentName for the "development", into the container to enable verification of the scope of dependency 

At this point json configuration has been loaded.

 

1, ConfigureServices method: runtime calls. Use this method to add services to the container. Dependency injection is used to configure to create objects based on dependencies at runtime.

1) optional.

2) In the  Configure prior method of configuring application services invoked by the host.

services.AddControllersWithViews();

The Controller register default runtime.

Add services to the service container, and it is used in applications Configure methods. Service to parse through dependency injection or ApplicationServices.

Note: IServiceCollection for the .net core comes with a container.

IServiceCollection container = new ServiceCollection();
services.AddScoped<IMyDependency, MyDependency>();
services.AddTransient<IOperationTransient, Operation>();
services.AddScoped<IOperationScoped, Operation>();
services.AddSingleton<IOperationSingleton, Operation>();
services.AddSingleton<IOperationSingletonInstance>(new Operation(Guid.Empty));

AddTransient: created each time a request from the service container. This survival for lightweight, stateless service. Scope lifetime service (AddScoped) to each client request (connect) a way to create.

AddSingleton: single embodiment, after the first request, the subsequent request to use the same instance. If the application requires a single instance of behavior, it is recommended to allow a lifetime service container management services. Do not implement a single instance of design patterns and provides a user code to manage object lifetime in class.

AddScoped: Scope lifetime service to each client request (connect) a way to create.

2, Configure method for specifying application response HTTP requests. By adding a middleware component configured to request pipeline IApplicationBuilder instance. Configure method can be used IApplicationBuilder, but not registered in the service container. Hosted create IApplicationBuilder and pass it directly to the Configure.

Configure for configuring middleware (Middleware) to construct a request processing pipeline.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  if (env.IsDevelopment())
  {
    app.UseDeveloperExceptionPage();
  }
  else
  {
    app.UseExceptionHandler("/Error");
    app.UseHsts();
  }

  app.UseHttpsRedirection();
  app.UseStaticFiles();

  app.UseRouting();

  app.UseAuthorization();

  app.UseEndpoints(endpoints =>
  {
    endpoints.MapRazorPages();
  });
}

IApplicationBuilder: definition of embedded middleware delegate added to the application's request pipeline. (IApplicationBuilder, Func <HttpContext, Func <Task>, Task>))

 Original, please indicate the source.

It is late at night, first wrote here, the follow-up to continue to learn ~

 

Guess you like

Origin www.cnblogs.com/xtt321/p/12324541.html