public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
} app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy(); app.UseMvc();
}
}

最初始的StartUp里的方法就是标红的这几个,一个构造函数一个属性和2个方法。

对于一个ASP.NET Core 程序而言,Startup Class 是必须的。ASP.NET Core在程序启动时会从assemblies中找到名字叫Startup的类,如果存在多个名为Startup的类,则会先找到项目根名称空间下的Startup类。

在Startup必须定义Configure方法,而configureServices方法则是可选的,方法会在程序第一次启动时被调用,类似传统的ASP.NET MVC的路由和应用程序状态均可在Startup中配置,也可以在此安装所需中间件等等。

ConfigureServices方法在Configure方法前调用,它是一个可选的方法,可在configureServices里依赖注入接口或一些全局的框架,比如EntityFramework、MVC等。

Configure方法用于每次http请求的处理,

Startup 类的 执行顺序:构造 -> configureServices->configure

ConfigureServices:

主要实现了依赖注入(DI)的配置,方法参数如下:

IServiceCollection:整个ASP.NET Core 默认带有依赖注入(DI),IServiceCollection是依赖注入的容器,首先创建一个类(Foo)和接口(IFoo),代码清单如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace WebApplication1
{
public interface IFoo
{
string GetFoo();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace WebApplication1
{
public class Foo : IFoo
{
public string GetFoo()
{
return "foo";
}
}
}

在ConfigureServices 中将接口和实现注入至容器  

public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IFoo, Foo>();
}

 

如果想在每次Http请求后都使用IFoo的GetFoo()方法来处理,上面讲到可以在Configure方法中注册函数,在注册过程中由于使用了依赖注入(DI),因此可以直接通过RequestServices.GetRequiredService<IFoo>()泛型方法将IFoo对象在容器中取出。

app.Run((context) =>
{
var str = context.RequestServices.GetRequiredService<IFoo>().GetFoo();
return context.Response.WriteAsync(str);
});

除了自己的接口外,还支持通过扩展方法添加更多的注入方法,比如EntityFramework、mvc框架都实现自己的添加方法。

public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders(); services.AddMvc(); // Add application services.
services.AddTransient<IFoo, Foo>();

services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
var conn = Configuration.GetSection("ConnectionStrings");
string efconn = conn["acc_miniehub"];
string sbconn = conn["acc_sb"];

services.AddDbContext<OMSECData.Acc_OmsContext>
(
options => options.UseSqlServer(efconn)
);
//配置CAP
services.AddCap(x =>
{
x.UseEntityFramework<OMSECData.Acc_OmsContext>();
x.UseAzureServiceBus(sb => {
sb.ConnectionString = sbconn;
sb.TopicPath = "oms.q";
});
//启用操作面板
x.UseDashboard();
});

}

  

Configure方法

主要是http处理管道配置和一些系统配置,参数如下:

  • IApplicationBuilder: 用于构建应用请求管道。通过IApplicationBuilder下的run方法传入管道处理方法。这是最常用方法,对于一个真实环境的应用基本上都需要比如权限验证、跨域、异常处理等。下面代码调用IApplicationBuilder.Run方法注册处理函数。拦截每个http请求,输出Hello World。
public void Configure(IApplicationBuilder app)
{
app.Run((context) => context.Response.WriteAsync("Hello World!"));
}

  

  • IHostingEnvironment: 同构造参数

  • ILoggerFactory: 同构造参数

原文地址

ASP.NET CORE读取appsettings.json的配置

关于Core里的 StartUp里的方法的理解。的更多相关文章

  1. Entity Framework 手动使用migration里面的up 和down方法。

    add-migration -IgnoreChanges 201606100717405_201606100645298_InitialCreate 执行这一句后 ,清空使用map生成的代码,个人不太 ...

  2. Java Native Interface 五 JNI里的多线程与JNI方法的注册

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 JNI里的多线程 在本地方法里写有关多线程的 ...

  3. 批量删除wps文档里的回车符的方法!WPS使用技巧分享!

    有时候整理文档的时候,如果是从网上复制的文字,可能会因为复制而产生很多的回车符.怎样能批量去掉这些个回车符呢,下面马上告诉你批量删除wps文档里的回车符的方法!WPS使用技巧分享! 想要批量删除批量删 ...

  4. 检查项目里是否有IDFA的方法

    检查项目里是否有IDFA的方法: 步骤:1.打开终端cd到要检查的文件的根目录. 2.执行下列语句:grep -r advertisingIdentifier . (别少了最后那个点号). 发现有ma ...

  5. iOS 在类别里添加成员变量的方法:objc_setAssociatedObject

    今天在github上查看MJPopupViewController这个项目,发现里面用到了objc_setAssociatedObject,用来为类别添加成员变量. 我百度之后,发现有人是这样说明的: ...

  6. [转]在Linux里设置环境变量的方法

    在Linux里设置环境变量的方法(export PATH) 一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“/opt/a ...

  7. 【公众号系列】在SAP里查看条件记录的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[公众号系列]在SAP里查看条件记录的方法   ...

  8. ASP.NET Core Startup类 Configure()方法 | ASP.NET Core 中间件详细说明

    ASP.NET Core 程序启动过程如下 目录 Startup 类 Configure() 方法 中间件 使用中间件 Configure 方法 的参数 IApplicationBuilder Ext ...

  9. TortoiseSVN里锁lock 的使用方法

    刚才试验了一下,终于搞明白了TortoiseSVN里锁lock 的使用方法. 简单的说,如果压根没有锁lock,那么每个人都拥有一个本地copy,每个人都能自由地对本地copy编辑edit并提交com ...

随机推荐

  1. MySQL(端口3306)

    MySQL(二进制)安装: 下载地址:http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49-linux2.6-x86_64.tar.gz ...

  2. net Core 中定时任务的设置

    接下来的任务 采用定时任务的需求场景: 每天的数据整理,比如库存,每天的零散数据的统计,定时提醒,定时提醒到期未完成的任务-.... 1.采用的第三方类库: quartz 2文档地址:http://w ...

  3. 【ACM】 hdu_1090_A+BII_201307261100

    A+B for Input-Output Practice (II)Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276 ...

  4. 洛谷 P1479 宿舍里的故事之五子棋

    P1479 宿舍里的故事之五子棋 题目描述 宿舍里好多好多有趣的事! 7890653今天看到不知何时流行的五子棋,在宿舍里拿个本子,画一些格子,一个棋盘就做好了! 当7890653把目光放到棋上,突发 ...

  5. LDAP目录服务折腾之后的总结

    前言 公司管理员工信息以及组织架构的后台系统要和Active Directory目录服务系统打通,后台系统使用PHP开发, 折腾了二十多天,终于上线了,期间碰到过各种疑难问题,不过总算在GOOGLE大 ...

  6. 修改 db_unique_name

    在创建DB的时候 db_unique_name设置错了.本来我是想让 db_name=itid db_unique_name=itid1 不过想改回来很简单的. SQL> alter syste ...

  7. jsp param动作标签

    param 标签以"名字-值"对的形式为其它标签提供附加消息.这个标签与jsp:include.jsp:forward.jsp:plugin标签一起使用. param 动作标签 & ...

  8. Unity Update 具体解释

    0x01:简单介绍 Unity的脚本继承了Monobehaviour类,在脚本中定义函数: void FixedUpdate(){} void Update(){} void LateUpdate() ...

  9. oop_day02_类、重载_20150810

    oop_day02_类.重载_20150810 1.怎样创建类?怎样创建对象? 2.引用类型之间画等号: 家门钥匙 1)指向同一个对象(数据有一份) 2)对当中一个引用的改动.会影响另外一个引用 基本 ...

  10. MySQL:常见错误01

    ylbtech-MySQL:常见错误01 1.返回顶部 1. [SQL]select * from product_product_tag aLEFT JOIN system_tag b on b.i ...