您的位置:首页 > 编程语言 > ASP

ASP. 4000 NET Core 2.0系列学习笔记-应用程序启动

2018-02-26 22:42 841 查看
ASP.NET Core应用是一个在Main方法中创建的一个Web服务器的控制台应用程序。默认如下:
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace NETCoreAPI
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();  //Run() 启动IWebHost
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)  //设置默认依赖注入
.UseStartup<Startup>()  //指定启动类:Startup
.Build();  //IwebHostBuilder负责创建IWebHost

}
}
Main方法调用遵循builder模式的WebHostBuilder,用于创建一个Web应用程序宿主。
builder具有定义Web服务器(如:Kestrel)和指定启动类(如:UseStartup<Startup>)的方法。using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebApplicationMVC
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run(); //Run()启动IWebHost
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args) //设置默认依赖注入
.UseKestrel() //设置Web server为Kestrel
.UseContentRoot(Directory.GetCurrentDirectory()) //指定Webhost使用的contentroot(内容根目录),比如Views。默认为当前应用程序根目录。
.UseIISIntegration() //使用IISIntegration 中间件
.UseStartup<Startup>() //指定启动类
.Build(); //IWebHostBuilder负责创建IWebHost
}
}在上面的代码中,Web服务器Kestrel被启用(也可以指定其他Web服务器)。WebHostBuilder提供了一些可选方法,其中包括寄宿在IIS和IIS Express中的UseIISIntegration和用于指定根目录内容的UseContentRoot。Buile()和Run()方法构建了用于宿主应用程序的IWebHost,然后启动他来监听传入的HTTP请求。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息