关于Core里的 StartUp里的方法的理解。
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里的方法的理解。的更多相关文章
- Entity Framework 手动使用migration里面的up 和down方法。
add-migration -IgnoreChanges 201606100717405_201606100645298_InitialCreate 执行这一句后 ,清空使用map生成的代码,个人不太 ...
- Java Native Interface 五 JNI里的多线程与JNI方法的注册
本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 JNI里的多线程 在本地方法里写有关多线程的 ...
- 批量删除wps文档里的回车符的方法!WPS使用技巧分享!
有时候整理文档的时候,如果是从网上复制的文字,可能会因为复制而产生很多的回车符.怎样能批量去掉这些个回车符呢,下面马上告诉你批量删除wps文档里的回车符的方法!WPS使用技巧分享! 想要批量删除批量删 ...
- 检查项目里是否有IDFA的方法
检查项目里是否有IDFA的方法: 步骤:1.打开终端cd到要检查的文件的根目录. 2.执行下列语句:grep -r advertisingIdentifier . (别少了最后那个点号). 发现有ma ...
- iOS 在类别里添加成员变量的方法:objc_setAssociatedObject
今天在github上查看MJPopupViewController这个项目,发现里面用到了objc_setAssociatedObject,用来为类别添加成员变量. 我百度之后,发现有人是这样说明的: ...
- [转]在Linux里设置环境变量的方法
在Linux里设置环境变量的方法(export PATH) 一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“/opt/a ...
- 【公众号系列】在SAP里查看条件记录的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[公众号系列]在SAP里查看条件记录的方法 ...
- ASP.NET Core Startup类 Configure()方法 | ASP.NET Core 中间件详细说明
ASP.NET Core 程序启动过程如下 目录 Startup 类 Configure() 方法 中间件 使用中间件 Configure 方法 的参数 IApplicationBuilder Ext ...
- TortoiseSVN里锁lock 的使用方法
刚才试验了一下,终于搞明白了TortoiseSVN里锁lock 的使用方法. 简单的说,如果压根没有锁lock,那么每个人都拥有一个本地copy,每个人都能自由地对本地copy编辑edit并提交com ...
随机推荐
- python 函数编写指南
#函数编写指南:1.给函数指定描述性名称,且只在其中是用小写字母和下划线 2.每个函数都应包含简要的阐述其功能的注释,该注释应紧跟在函数定义后面,且采用文档字符串格式 3.给形参指定默认值时,等号两边 ...
- python - 函数的定义和使用
目录 函数的定义和使用 一. 为什么要用函数? 二. 函数的参数 三. 函数的变量 global和nolocal 四. 递归函数 五. lamabda匿名函数 函数的定义和使用 1 def test( ...
- Django REST framework - 认证
目录 认证 DRF 5种验证方式 如何确定身份验证? 设置身份验证方案 案例: 基于自定义Token认证 第一步: 定义一个用户表和一个保存用户Token的表 第二步: 定义一个登陆视图 第三步定义一 ...
- 第二节:Series基本属性及方法(下)
- 洛谷P3375【模板】KMP字符串匹配
题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next. (如果你不知道这是什么意思也不要问,去百度 ...
- 清北学堂模拟赛d2t4 最大值(max)
题目描述LYK有一本书,上面有很多有趣的OI问题.今天LYK看到了这么一道题目:这里有一个长度为n的正整数数列ai(下标为1~n).并且有一个参数k.你需要找两个正整数x,y,使得x+k<=y, ...
- 深入分析Linux自旋锁
原创 2016-08-12 tekkamanninja CU技术社区 作者| tekkamanninja本文版权由tekkamanninja所有,如需转载,请联系本公众号获取授权!在复习休眠的过程 ...
- android 随手记之文件+參数上传请求
第一步:须要两个jar的支持,稍后以下给会出下载地址. 第二步:建立一个project 以下贴出最基本的代码 package com.example.testpaizhao; import java. ...
- 十分钟学会canvas
一句话描述:canvas是HTML5加入的用来绘制2D图像与文字的元素. 基础 简单步骤: var c = document.getElementById("mycanvas"); ...
- iOS - 社会化分享-微信分享,朋友圈分享
我仅仅做了文字和图片分享功能 1. TARGETS - Info - URL Types identifier -> weixin URL Schemes -> 应用id 2.在AppD ...