当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET Core笔记(1) - 了解Startup类

ASP.NET Core笔记(1) - 了解Startup类

2020年03月22日  | 移动技术网IT编程  | 我要评论

女性网站大全,九把刀之天绝地变态版,天知恋

  • startup构造函数
  • configureservices方法
  • configure方法
  • 在configurewebhostdefaults中直接配置服务和请求管道

core一般使用startup类来进行应用的配置。在构建应用主机时指定startup类,通常通过在主机生成器上调用webhostbuilderextensions.usestartup 方法来指定 startup类:

public static ihostbuilder createhostbuilder(string[] args) =>
    host.createdefaultbuilder(args)
        .configurewebhostdefaults(webbuilder =>
        {
            webbuilder.usestartup<startup>();
        });

startup类中可以包含以下方法:

  • startup构造函数
  • configureservices方法,可选
  • configure方法

startup构造函数

在3.1中,使用泛型主机 (ihostbuilder) 时,startup构造函数中只能注入这三种类型的服务:iwebhostenvironment、ihostenvironment、iconfiguration。
尝试注入别的服务时会抛出invalidoperationexception异常。

system.invalidoperationexception: 'unable to resolve service for type '***' while attempting to activate '_1_startup.startup'.'

因为主机启动时,执行顺序为startup构造函数 -> configureservices方法 -> configure 方法。在startup构造函数执行时主机只提供了这三个服务,别的服务需要在configureservices方法中添加。然后到了configure方法执行的时候,就可以使用更多的服务类型了。

configureservices方法

主机会调用configureservices方法,将需要的服务以依赖注入的方式添加到服务容器,使其在configure方法和整个应用中可用。

configureservices方法的参数中无法注入除iservicecollection之外的服务。
具体使用时可以通过iservicecollection的扩展方法为应用配置各种功能。

public void configureservices(iservicecollection services)
{

    services.adddbcontext<applicationdbcontext>(options =>
        options.usesqlserver(
            configuration.getconnectionstring("defaultconnection")));
    services.adddefaultidentity<identityuser>(
        options => options.signin.requireconfirmedaccount = true)
        .addentityframeworkstores<applicationdbcontext>();

    services.addrazorpages();
}

configure方法

configure 方法用于指定应用响应 http 请求的方式。 可通过将中间件组件添加到 iapplicationbuilder 实例来配置请求管道。 configure 方法参数中的iapplicationbuilder不需要在服务容器中注册就可使用,它已由主机创建好并直接传递给了configure方法。
configure方法由一系列的use扩展方法组成:

public void configure(iapplicationbuilder app, iwebhostenvironment env)
{
    if (env.isdevelopment())
    {
        app.usedeveloperexceptionpage();
    }

    app.userouting();

    app.useendpoints(endpoints =>
    {
        endpoints.mapget("/", async context =>
        {
            await context.response.writeasync("hello world!");
        });
    });
}

每个use扩展都在请求管道中添加了中间件。配置到请求管道中的中间件都会调用它之后的下一个中间件或者直接将管道短路。

在configure方法参数中,可以根据自己的需要注入像iwebhostenvironment, iloggerfactory之类的服务,或者是在configureservices方法中添加到di容器中的服务。

在configurewebhostdefaults中直接配置服务和请求管道

core还提供了不使用startup类而能够配置服务和请求管道的方式。也可以在configurewebhostdefaults上调用它提供的configureservices和configure方法。

public static ihostbuilder createhostbuilder(string[] args) =>
        host.createdefaultbuilder(args)
            .configureappconfiguration((hostingcontext, config) =>
            {
            })
            .configurewebhostdefaults(webbuilder =>
            {
                webbuilder.configureservices((c, services) =>
                {
                    services.addcontrollers();
                })
                .configure(app =>
                {
                    var env = app.applicationservices.getrequiredservice<iwebhostenvironment>();

                    if (env.isdevelopment())
                    {
                        app.usedeveloperexceptionpage();
                    }

                    app.userouting();

                    app.useendpoints(endpoints =>
                    {
                        endpoints.mapget("/", async context =>
                        {
                            await context.response.writeasync("hello world!");
                        });
                    });
                });
            })

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网