Owin管道与asp.net管道模型

最近一直没搞懂在 mvc5框架中 为什么存在 Global.asax.cs和Startup.cs 文件...

然后搜索下面文章:

http://stackoverflow.com/questions/20168978/do-i-need-a-global-asax-cs-file-at-all-if-im-using-an-owin-startup-cs-class-and

其中提到

1、Application_Start 会比Startup.Configuration 先启动

2、mvc4 版本一致性 布啦布啦....

3、 Global.asax.cs 可以处理一些特殊事件 例如 Application_Start Application_Error Session_Start 等等

4、Startup.Configuration 中可以配置 关于授权之类的(替代原先的Application_AuthenticateRequest,另外mvc5中引入了ASP.NET Identity 2,而这个基于Owin)

5、最最重要的一条如果 引用集没有Microsoft.Owin.Host.SystemWeb 这个dll 便不会加载Startup.Configuration

----------------------分割线----------------------------------

ok 进入另一个话题 owin 的管道有什么 首先贴下微软的PipelineStage

 1 namespace Owin
 2 {
 3   /// <summary>
 4   /// An ordered list of known Asp.Net integrated pipeline stages. More details on the ASP.NET integrated pipeline can be found at http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx
 5   ///
 6   /// </summary>
 7   public enum PipelineStage
 8   {
 9     Authenticate,
10     PostAuthenticate,
11     Authorize,
12     PostAuthorize,
13     ResolveCache,
14     PostResolveCache,
15     MapHandler,
16     PostMapHandler,
17     AcquireState,
18     PostAcquireState,
19     PreHandlerExecute,
20   }
21 }

看注释 去看 httpapplication事件。。。个人感觉Owin管道里的事件是可以替代原先的httpapplication事件的。。。

--------------------------------------------分割线-----------------------------------------------------

既然说到Startup.Configuration可以替代 那怎么用才是关键,直接上代码:

 1 public void Configuration(IAppBuilder app)
 2 {
 3     app.Use((context, next) =>
 4     {
 5         PrintCurrentIntegratedPipelineStage(context, "Middleware 1");
 6         return next.Invoke();
 7     });
 8     app.Use((context, next) =>
 9     {
10         PrintCurrentIntegratedPipelineStage(context, "2nd MW");
11         return next.Invoke();
12     });
13     app.UseStageMarker(PipelineStage.Authenticate);
14     app.Run(context =>
15     {
16         PrintCurrentIntegratedPipelineStage(context, "3rd MW");
17         return context.Response.WriteAsync("Hello world");
18     });
19     app.UseStageMarker(PipelineStage.ResolveCache);
20 }

看代码中红色部分。。。 使用app.UseStageMarker(xxxx状态)之前的代码 便是xxxx状态注册。

注意:

  1、如果不是用UseStageMarker标记 那些中间件的位置 所有的位置为:PreHandlerExecute。

  2、多次使用UseStageMarker 不能 前面顺序比后面大。。。

相关链接:http://www.asp.net/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline

----------------------------分割线-----------------------------------------------------------

个人一些闲言碎语

貌似 asp.net5 里 mvc框架 中没有了Global.asax.cs文件 入口就是Startup.cs

还有 个人感觉  IOwinConent 会替代 HttpAplication ;OwinMiddleware 替代IHttpModule ( 当然这是指 asp.net5里,而mvc5中还是 混搭风格),app.Use(xxxxxx)构成的 管道还是 相对好理解的

不过现在asp.net 还是 rc等出了 rtm再来更新吧。。。。

贴下 asp.net5中Startup

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Threading.Tasks;
  5 using Microsoft.AspNet.Builder;
  6 using Microsoft.AspNet.Hosting;
  7 using Microsoft.AspNet.Identity.EntityFramework;
  8 using Microsoft.Data.Entity;
  9 using Microsoft.Extensions.Configuration;
 10 using Microsoft.Extensions.DependencyInjection;
 11 using Microsoft.Extensions.Logging;
 12 using WebApplication2.Models;
 13 using WebApplication2.Services;
 14
 15 namespace WebApplication2
 16 {
 17     public class Startup
 18     {
 19         public Startup(IHostingEnvironment env)
 20         {
 21             // Set up configuration sources.
 22             var builder = new ConfigurationBuilder()
 23                 .AddJsonFile("appsettings.json")
 24                 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
 25
 26             if (env.IsDevelopment())
 27             {
 28                 // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
 29                 builder.AddUserSecrets();
 30             }
 31
 32             builder.AddEnvironmentVariables();
 33             Configuration = builder.Build();
 34         }
 35
 36         public IConfigurationRoot Configuration { get; set; }
 37
 38         // This method gets called by the runtime. Use this method to add services to the container.
 39         public void ConfigureServices(IServiceCollection services)
 40         {
 41             // Add framework services.
 42             services.AddEntityFramework()
 43                 .AddSqlServer()
 44                 .AddDbContext<ApplicationDbContext>(options =>
 45                     options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
 46
 47             services.AddIdentity<ApplicationUser, IdentityRole>()
 48                 .AddEntityFrameworkStores<ApplicationDbContext>()
 49                 .AddDefaultTokenProviders();
 50
 51             services.AddMvc();
 52
 53             // Add application services.
 54             services.AddTransient<IEmailSender, AuthMessageSender>();
 55             services.AddTransient<ISmsSender, AuthMessageSender>();
 56         }
 57
 58         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
 59         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
 60         {
 61             loggerFactory.AddConsole(Configuration.GetSection("Logging"));
 62             loggerFactory.AddDebug();
 63
 64             if (env.IsDevelopment())
 65             {
 66                 app.UseBrowserLink();
 67                 app.UseDeveloperExceptionPage();
 68                 app.UseDatabaseErrorPage();
 69             }
 70             else
 71             {
 72                 app.UseExceptionHandler("/Home/Error");
 73
 74                 // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
 75                 try
 76                 {
 77                     using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
 78                         .CreateScope())
 79                     {
 80                         serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
 81                              .Database.Migrate();
 82                     }
 83                 }
 84                 catch { }
 85             }
 86
 87             app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
 88
 89             app.UseStaticFiles();
 90
 91             app.UseIdentity();
 92
 93             // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715
 94
 95             app.UseMvc(routes =>
 96             {
 97                 routes.MapRoute(
 98                     name: "default",
 99                     template: "{controller=Home}/{action=Index}/{id?}");
100             });
101         }
102
103         // Entry point for the application.
104         public static void Main(string[] args) => WebApplication.Run<Startup>(args);
105     }
106 }
时间: 2024-07-30 04:21:47

Owin管道与asp.net管道模型的相关文章

Asp.net管道模型(管线模型)

前言 为什么我会起这样的一个标题,其实我原本只想了解asp.net的管道模型而已,但在查看资料的时候遇到不明白的地方又横向地查阅了其他相关的资料,而收获比当初预想的大了很多. 有本篇作基础,下面两篇就更好理解了: 理解并自定义HttpHandler 理解并自定义HttpModule 目录 一般不写目录,感觉这次要写的东西有些多就写一个清晰一下吧. 1.Asp.net管道模型: 2.进程的子进程与进程的线程: 3.应用程序域(AppDomain): 4.IIS5.x下一个HTTP请求/响应过程的整

谈谈IIS与ASP.NET管道

作为一个Asp.Net平台开发者,非常有必要了解IIS和Asp.Net是如何结合,执行我们的托管代码,以及Asp.Net管道事件的. 本节目录 IIS 5.X IIS 6 IIS 7+ 集成模式 Asp.Net管道 HttpModule HttpHandle IIS 5.x InetInfo.exe与W3SVC服务 IIS 5.x运行在进程InetInfo.exe中,在该进程中一个最重要的服务就是名为World Wide Web Publishing Service(简称W3SVC)的Windo

Asp.net管道 (第二篇)

从请求进入ASP.NET工作者进程,直至它到达最终的处理程序之前要经过一系列的步骤和过程,这个步骤和过程称为ASP.NET处理管道. Asp.net的处理管道流程如下: 语言描述如下: Asp.net处理管道的第一步是创建HttpWorkerRequest对象,它包含于当前请求有关的所有信息. HttpWorkerRequest把请求传递给HttpRuntime类的静态ProcessRequest方法.HttpRuntime首先要做的事是创建HttpContext对象,并用HttpWorkerR

ASP.NET 管道事件与HttpModule, HttpHandler简单理解 -摘自网络

第一部分:转载自Artech  IIS与ASP.NET管道 ASP.NET管道 以IIS 6.0为例,在工作进程w3wp.exe中,利用Aspnet_ispai.dll加载.NET运行时(如果.NET运行时尚未加载).IIS 6引入了应用程序池的概念,一个工作进程对应着一个应用程序池.一个应用程序池可以承载一个或者多个Web应用,每个Web应用映射到一个IIS虚拟目录.与IIS 5.x一样,每一个Web应用运行在各自的应用程序域中. 如果HTTP.SYS接收到的HTTP请求是对该Web应用的第一

学习ASP.NET MVC框架揭秘笔记-IIS/ASP.NET管道(一)

IIS/ASP.NET管道 ASP.NET MVC就是建立在ASP.NET平台基础上基于MVC模式的Web应用框架,深入理解ASP.NET MVC的前提是对ASP.NET管道式设计有深刻的认识.由于ASP.NET Web应用大都寄宿于IIS上,接下来会介绍3个主要的IIS版本对各自Web请求的处理方式. 1.3.1 IIS 5.x与ASP.NET IIS 5.x运行在进程InetInfo.exe中,该进程寄宿着一个名为World WideWeb Publishing Service(简称W3SV

IIS与asp.net管道

我们在基于asp.net开发web程序,基本上都是发布部署到安装了IIS的windows服务器上,然后只要用户能够访问就算任务完成了,但是很少静下心来想想这背后到底发生了什么,那么这个系列就来总结下asp.net的基础原理. asp.net是什么 我们做web开发的可以说时时刻刻都在跟asp.net打交道,但很少总结asp.net是什么,可以用一句话总结: asp.net是一个开发web程序的平台. HTTP协议 由于web程序是基于HTTP协议的,所以在继续深入了解asp.net之前有必要学习

学习ASP.NET MVC框架揭秘笔记-IIS/ASP.NET管道(二)

IIS7.0与ASP.NET IIS7.0在请求的监听和分发机制上又进行了革新性的改进,主要体现在引入Window进程激活服务(Windows Process Activation Service,WAS)分流了原来(IIS6.0)W3SVC承载的部分功能.IIS6.0中W3SVC主要承载着如下三大功能. 1.HTTP请求接收:接收HTTP.SYS监听到的HTTP请求. 2.配置管理:从元数据库(metabase)中加载配置信息对相关组件进行配置. 3.进程管理:创建.回收.监控工作进程. II

WCF技术剖析之二:再谈IIS与ASP.NET管道

原文:WCF技术剖析之二:再谈IIS与ASP.NET管道 在2007年9月份,我曾经写了三篇详细介绍IIS架构和ASP.NET运行时管道的文章,深入介绍了IIS 5.x与IIS 6.0HTTP请求的监听与分发机制,以及ASP.NET运行时管道对HTTP请求的处理流程: [原创]ASP.NET Process Model之一:IIS 和 ASP.NET ISAPI [原创]ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline - Part I

[Asp.Net]谈谈IIS与Asp.Net管道

作为一个Asp.Net平台开发者,非常有必要了解IIS和Asp.Net是如何结合,执行我们的托管代码,以及Asp.Net管道事件的. 本节目录 IIS 5.X IIS 6 IIS 7+ 集成模式 Asp.Net管道 HttpModule HttpHandle IIS 5.x InetInfo.exe与W3SVC服务 IIS 5.x运行在进程InetInfo.exe中,在该进程中一个最重要的服务就是名为World Wide Web Publishing Service(简称W3SVC)的Windo