关于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 ...
随机推荐
- [luogu4054 JSOI2009] 计数问题(2D BIT)
传送门 Solution 2D BIT模板 Code //By Menteur_Hxy #include <cmath> #include <cstdio> #include ...
- jdk编译安装及tomcat编译安装
这里我安装的jdk版本为1.8版本,tomcat版本为8.5(请上官网下载) 运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) jdk部署: 1.前往软件所在路径 [root@web ...
- hdu2004 成绩转换【C++】
成绩转换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Android一键换肤功能:一种简单的实现
Android一键换肤功能:一种简单的实现 现在的APP开发,通常会提供APP的换肤功能,网上流传的换肤代码和实现手段过于复杂,这里有一个开源实现,我找了一大堆,发现这个项目相对较为简洁:htt ...
- Spring MVC Beginner's Guide--应该看第二次
第一遍,就差WEBFLOW知识点没过了.. 真的值得好好再看第二次呢.. 样例工程算是比较多的啦. 学到真的不少..
- N天学习一个linux命令之xargs
用途 标准输入流读取参数(空格或者换行符分隔),传递给需要执行的命令 用法 xargs [options] [command [initial-arguments]] 常用选项 --arg-file= ...
- [转]十五天精通WCF——第九天 高级玩法之自定义Behavior
终于我又看完了二期爱情保卫战,太酸爽了,推荐链接:http://www.iqiyi.com/a_19rrgublqh.html?vfm=2008_aldbd,不多说,谁看谁入迷,下面言归正传, 看看这 ...
- python 执行环境
一些函数 执行其它非python程序 1 一些函数 callable callable()是一个布尔函数,确定一个对象是否可以通过函数操作符(())来调用.如果函数可调用便返回True,否则便是Fal ...
- log显示error时的堆栈信息理解和分析
error显示的log堆栈信息,是从最深层(最内层)的堆栈信息开始由内向外打印的. error显示的log堆栈信息,是从最深层(最内层)的堆栈信息开始由内向外打印的. error显示的log堆栈信息, ...
- canvas学习相关的一点东西
<!DOCTYPE html> <html> <head> <style> canvas { border: 1px dashed black; } & ...