iis运行原理 Asp.Net详解IIS内部运行原理
本章節主要講IIS和 管道內部如何處理客戶端Http請求,會較多的以代碼的形式講述,讓大家對HttpApplication、HttpHandler、HttpApplicationFactory、Page這幾個在處理請求過程中扮演重要角色的對象有更深入的了解。
下面我們通過單步調式跟蹤System.Web.Dll源碼來分析各個過程中關鍵處理函數
(說明:部分代碼只保留了關鍵代碼)
1、首先進入管道運行時中的托管入口函數
IIS集成模式:
在IPipelineRuntime.cs類中請求通知函數:ProcessRequestNotification,并調用ProcessRequestNotificationHelper函數
internal static int ProcessRequestNotification(
IntPtr rootedObjectsPointer,
IntPtr nativeRequestContext,
IntPtr moduleData,
int flags)
{
try {
//調用ProcessRequestNotificationHelper
return ProcessRequestNotificationHelper(rootedObjectsPointer, nativeRequestContext, moduleData, flags);
}
catch(Exception e) {
ApplicationManager.RecordFatalException(e);
throw;
}
}
2、在ProcessRequestNotificationHelper函數中調用運行時HttpRuntime中的ProcessRequestNotification函數
internal static int ProcessRequestNotificationHelper(
IntPtr rootedObjectsPointer,
IntPtr nativeRequestContext,
IntPtr moduleData,
int flags)
{
IIS7WorkerRequest wr = null;
HttpContext context = null;
RequestNotificationStatus status = RequestNotificationStatus.Continue;
RootedObjects root;
bool workerRequestWasJustCreated = false;
try {
status = HttpRuntime.ProcessRequestNotification(wr, context);//調用ProcessRequestNotification函數
}
finally {
}
return (int)status;
}
3、在ProcessRequestNotification中調用ProcessRequestNotificationPrivate
4、處理請求通知ProcessRequestNotificationPrivate函數中調用了HttpApplicatioinFactory的GetApplicationInstance()函數來獲取HttpApplication實例對象,并調用BeginProcessRequestNotification處理請求
private RequestNotificationStatus ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) {
RequestNotificationStatus status = RequestNotificationStatus.Pending;
try {
IHttpHandler handler = null;
if (context.NeedToInitializeApp()) {
try {
EnsureFirstRequestInit(context);
}
catch {
}
context.Response.InitResponseWriter();
handler = HttpApplicationFactory.GetApplicationInstance(context);//獲取HttpApplication實例
if (handler == null)
throw new HttpException(SR.GetString(SR.Unable_create_app_object));
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, handler.GetType().FullName, "Start");
HttpApplication app = handler as HttpApplication;
if (app != null) {
// associate the context with an application instance
app.AssignContext(context);
}
}
wr.SynchronizeVariables(context);
if (context.ApplicationInstance != null) {
// 處理請求
IAsyncResult ar = context.ApplicationInstance.BeginProcessRequestNotification(context, _requestNotificationCompletionCallback);
if {
status = RequestNotificationStatus.Continue;
}
}
else if (handler != null) {
// HttpDebugHandler is processed here
handler.ProcessRequest(context);
status = RequestNotificationStatus.FinishRequest;
}
else {
status = RequestNotificationStatus.Continue;
}
}
catch (Exception e) {
}
return status;
}
IIS經典模式:
在ISAPIRuntime.cs類中請求 ProcessRequest函數并調用HttpRuntime.ProcessReques方法
1、HttpRuntime處理請求入口函數
public static void ProcessRequest(HttpWorkerRequest wr) {
if (wr == null)
throw new ArgumentNullException("wr");
if (HttpRuntime.UseIntegratedPipeline) {
throw new PlatformNotSupportedException(SR.GetString(SR.Method_Not_Supported_By_Iis_Integrated_Mode, "HttpRuntime.ProcessRequest"));
}
ProcessRequestNoDemand(wr);//調用ProcessRequestNoDemand函數
}
2、ProcessRequestNoDemand函數,調用ProcessRequestNow函數 internal static void ProcessRequestNoDemand(HttpWorkerRequest wr) {
RequestQueue rq = _theRuntime._requestQueue;
wr.UpdateInitialCounters();
if (rq != null) // could be null before first request
wr = rq.GetRequestToExecute(wr);
if (wr != null) {
CalculateWaitTimeAndUpdatePerfCounter(wr);
wr.ResetStartTime();
ProcessRequestNow(wr);調用ProcessRequestNow
}
}
3、ProcessRequestNow函數,調用ProcessRequestInternal internal static void ProcessRequestNow(HttpWorkerRequest wr)
{
_theRuntime.ProcessRequestInternal(wr);//調用ProcessRequestInternal
}
4、ProcessRequestInternal函數,調用HttpApplicatioinFactory的GetApplicationInstance()函數來獲取HttpApplication實例對象
private void ProcessRequestInternal(HttpWorkerRequest wr) {
// Count active requests
Interlocked.Increment(ref _activeRequestCount);
// Get application instance
IHttpHandler app = HttpApplicationFactory.GetApplicationInstance(context);//獲取HttpApplication對象
if (app == null)
throw new HttpException(SR.GetString(SR.Unable_create_app_object));
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, app.GetType().FullName, "Start");
if (app is IHttpAsyncHandler) {
// asynchronous handler
IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler)app;
context.AsyncAppHandler = asyncHandler;
//來到了真正處理請求步驟
asyncHandler.BeginProcessRequest(context, _handlerCompletionCallback, context);
}
else {
// synchronous handler
app.ProcessRequest(context);
FinishRequest(context.WorkerRequest, context, null);
}
}
catch (Exception e) {
context.Response.InitResponseWriter();
FinishRequest(wr, context, e);
}
}
說明:以上4個步驟集成模式和經典模式分不同的函數執行,從下面開始兩種模式調用相同的函數,只是在函數中針對不同的模式進行不同的處理
5、進入HttpApplicationFactory,調用GetNormalApplicationInstance函數
internal static IHttpHandler GetApplicationInstance(HttpContext context) {
if (_customApplication != null)
return _customApplication;
if (context.Request.IsDebuggingRequest)
return new HttpDebugHandler();
_theApplicationFactory.EnsureInited();
_theApplicationFactory.EnsureAppStartCalled(context);
return _theApplicationFactory.GetNormalApplicationInstance(context);//返回HttpApplication實例
}
6、進入GetNormalApplicationInstance函數,調用InitInternal初始化HttpApplication內部對象
private HttpApplication GetNormalApplicationInstance(HttpContext context) {
HttpApplication app = null;
lock (_freeList) {
if (_numFreeAppInstances > 0) {
app = (HttpApplication)_freeList.Pop();
_numFreeAppInstances--;
if (_numFreeAppInstances < _minFreeAppInstances) {
_minFreeAppInstances = _numFreeAppInstances;
}
}
}
if (app == null) {
// If ran out of instances, create a new one
app = (HttpApplication)HttpRuntime.CreateNonPublicInstance(_theApplicationType);
using (new ApplicationImpersonationContext()) {
app.InitInternal(context, _state, _eventHandlerMethods);//初始化Application內部對象
}
}
if (AppSettings.UseTaskFriendlySynchronizationContext) {
// When this HttpApplication instance is no longer in use, recycle it.
app.ApplicationInstanceConsumersCounter = new CountdownTask(1); // representing required call to HttpApplication.ReleaseAppInstance
app.ApplicationInstanceConsumersCounter.Task.ContinueWith((_, o) => RecycleApplicationInstance((HttpApplication)o), app, TaskContinuationOptions.ExecuteSynchronously);
}
return app;
}
7、進入HttpApplication,調用InitInternal函數來初始化Application內部對象。初始化HttpModule和建立處理執行步驟
internal void InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) {
Debug.Assert(context != null, "context != null");
// Remember state
_state = state;
PerfCounters.IncrementCounter(AppPerfCounter.PIPELINES);
try {
try {
// Remember context for config lookups
_initContext = context;
_initContext.ApplicationInstance = this;
// Set config path to be application path for the application initialization
context.ConfigurationPath = context.Request.ApplicationPathObject;
// keep HttpContext.Current working while running user code
using (new DisposableHttpContextWrapper(context)) {
// Build module list from config 根據IIS集成模式和經典模式初始化HttpMudule
if (HttpRuntime.UseIntegratedPipeline) { //集成模式
Debug.Assert(_moduleConfigInfo != null, "_moduleConfigInfo != null");
Debug.Assert(_moduleConfigInfo.Count >= 0, "_moduleConfigInfo.Count >= 0");
try {
context.HideRequestResponse = true;
_hideRequestResponse = true;
InitIntegratedModules();//集成模式初始化HttpModule
}
finally {
context.HideRequestResponse = false;
_hideRequestResponse = false;
}
}
else { //經典模式
InitModules();//經典模式初始化HttpModule
// this is used exclusively for integrated mode
Debug.Assert(null == _moduleContainers, "null == _moduleContainers");
}
// Hookup event handlers via reflection
if (handlers != null)
HookupEventHandlersForApplicationAndModules(handlers);
// Initialization of the derived class
_context = context;
if (HttpRuntime.UseIntegratedPipeline && _context != null) {
_context.HideRequestResponse = true;
}
_hideRequestResponse = true;
try {
Init();
}
catch (Exception e) {
RecordError(e);
}
}
if (HttpRuntime.UseIntegratedPipeline && _context != null) {
_context.HideRequestResponse = false;
}
_hideRequestResponse = false;
_context = null;
_resumeStepsWaitCallback= new WaitCallback(this.ResumeStepsWaitCallback);
// Construct the execution steps array
<span style="color:#FF0000;"><strong>if (HttpRuntime.UseIntegratedPipeline) {</strong></span> //集成模式 調用PipelineStepManager
<span style="color:#FF0000;"><strong>_stepManager = new PipelineStepManager(this);</strong></span>
}
else {
<span style="color:#FF0000;"><strong> _stepManager = new ApplicationStepManager(this);</strong></span> //經典模式 調用 ApplicationStepManager
}
<span style="color:#FF0000;"><strong>_stepManager.BuildSteps(_resumeStepsWaitCallback);</strong></span> //建立執行處理步驟
}
finally {
_initInternalCompleted = true;
// Reset config path
context.ConfigurationPath = null;
// don't hold on to the context
_initContext.ApplicationInstance = null;
_initContext = null;
}
}
catch { // Protect against exception filters
throw;
}
}
8、初始化HttpModule,包含系統默認的HttpModule和自定義的HttpModule
1)、系統默認的HttpModule:
C:\Windows\\Framework64\v4.0.30319\Config\web.config
?
?
2)、自定義的HttpModule在網站的Web.config中配置
經典模式:
<system.web>
<httpModules>
<add name="mymodule" type="WebApplication2.myModule"/>
</httpModules>
</system.web>
集成模式: <system.webServer>
<modules>
<add name="mymodule" type="WebApplication2.myModule"/>
</modules>
</system.webServer>
3)、經典模式初始化: private void InitModules() {
//讀取配置文件中HttpModules節點
HttpModulesSection pconfig = RuntimeConfig.GetAppConfig().HttpModules;
//獲取HttpModule集合
HttpModuleCollection moduleCollection = pconfig.CreateModules();
HttpModuleCollection dynamicModules = CreateDynamicModules();
moduleCollection.AppendCollection(dynamicModules);
_moduleCollection = moduleCollection; // don't assign until all ops have succeeded
InitModulesCommon();//調用InitModulesCommon
}
4)、初始化各個HttpModule對象.
private void InitModulesCommon() {
int n = _moduleCollection.Count;
for (int i = 0; i < n; i++) {
// remember the module being inited for event subscriptions
// we'll later use this for routing
_currentModuleCollectionKey = _moduleCollection.GetKey(i);
_moduleCollection[i].Init(this);//初始化每個HttpModule
}
_currentModuleCollectionKey = null;
InitAppLevelCulture();
}
5)、初始化HttpModule,在Init函數中注冊HttpApplication對象的事件,這里舉了兩個例子
OutputCacheModule.cs? web.config中默認第一個HttpModule
void IHttpModule.Init(HttpApplication app) {
OutputCacheSection cacheConfig = RuntimeConfig.GetAppConfig().OutputCache;
if (cacheConfig.EnableOutputCache) {
app.ResolveRequestCache += new EventHandler(this.OnEnter);
app.UpdateRequestCache += new EventHandler(this.OnLeave);
}
}
SessionStateModule.cs web.config中默認第二個HttpModule,實現Session的系統HttpModule public void Init(HttpApplication app) {
bool initModuleCalled = false;
SessionStateSection config = RuntimeConfig.GetAppConfig().SessionState;
if (!s_oneTimeInit) {
s_lock.AcquireWriterLock();
try {
if (!s_oneTimeInit) {
InitModuleFromConfig(app, config);
initModuleCalled = true;
if (!CheckTrustLevel(config))
s_trustLevelInsufficient = true;
s_timeout = (int)config.Timeout.TotalMinutes;
s_useHostingIdentity = config.UseHostingIdentity;
// See if we can try InProc optimization. See inline doc of s_allowInProcOptimization
// for details.
if (config.Mode == SessionStateMode.InProc &&
_usingAspnetSessionIdManager) {
s_allowInProcOptimization = true;
}
if (config.Mode != SessionStateMode.Custom &&
config.Mode != SessionStateMode.Off &&
!config.RegenerateExpiredSessionId) {
s_allowDelayedStateStoreItemCreation = true;
}
s_configExecutionTimeout = RuntimeConfig.GetConfig().HttpRuntime.ExecutionTimeout;
s_configRegenerateExpiredSessionId = config.RegenerateExpiredSessionId;
s_configCookieless = config.Cookieless;
s_configMode = config.Mode;
// The last thing to set in this if-block.
s_oneTimeInit = true;
Debug.Trace("SessionStateModuleInit",
"Configuration: _mode=" + config.Mode +
";Timeout=" + config.Timeout +
";CookieMode=" + config.Cookieless +
";SqlConnectionString=" + config.SqlConnectionString +
";StateConnectionString=" + config.StateConnectionString +
";s_allowInProcOptimization=" + s_allowInProcOptimization +
";s_allowDelayedStateStoreItemCreation=" + s_allowDelayedStateStoreItemCreation);
}
}
finally {
s_lock.ReleaseWriterLock();
}
}
if (!initModuleCalled) {
InitModuleFromConfig(app, config);
}
if (s_trustLevelInsufficient) {
throw new HttpException(SR.GetString(SR.Session_state_need_higher_trust));
}
}
集成模式初始化HttpModule代碼我就不貼出來了,大家可以在源碼中查看
9、建立處理步驟BuildSteps,將所有要執行的步驟載入到一個IExecutionStep[]集合中,待后面ResumeSteps調用
經典模式:調用ApplicationStepManager類中的BuildSteps函數
internal class ApplicationStepManager : StepManager {
private IExecutionStep[] _execSteps;
private WaitCallback _resumeStepsWaitCallback;
private int _currentStepIndex;
private int _numStepCalls;
private int _numSyncStepCalls;
private int _endRequestStepIndex;
internal ApplicationStepManager(HttpApplication app): base(app) {
}
internal override void BuildSteps(WaitCallback stepCallback )
{
ArrayList steps = new ArrayList();
HttpApplication app = _application;
bool urlMappingsEnabled = false;
UrlMappingsSection urlMappings = RuntimeConfig.GetConfig().UrlMappings;
urlMappingsEnabled = urlMappings.IsEnabled && ( urlMappings.UrlMappings.Count > 0 );
steps.Add(new ValidateRequestExecutionStep(app));
steps.Add(new ValidatePathExecutionStep(app));
if (urlMappingsEnabled)
steps.Add(new UrlMappingsExecutionStep(app)); // url mappings
app.CreateEventExecutionSteps(HttpApplication.EventBeginRequest, steps);
app.CreateEventExecutionSteps(HttpApplication.EventAuthenticateRequest, steps);
app.CreateEventExecutionSteps(HttpApplication.EventDefaultAuthentication, steps);
app.CreateEventExecutionSteps(HttpApplication.EventPostAuthenticateRequest, steps);
app.CreateEventExecutionSteps(HttpApplication.EventAuthorizeRequest, steps);
app.CreateEventExecutionSteps(HttpApplication.EventPostAuthorizeRequest, steps);
app.CreateEventExecutionSteps(HttpApplication.EventResolveRequestCache, steps);
app.CreateEventExecutionSteps(HttpApplication.EventPostResolveRequestCache, steps);
steps.Add(new MapHandlerExecutionStep(app));//分配一個Handler
app.CreateEventExecutionSteps(HttpApplication.EventPostMapRequestHandler, steps);
app.CreateEventExecutionSteps(HttpApplication.EventAcquireRequestState, steps);
app.CreateEventExecutionSteps(HttpApplication.EventPostAcquireRequestState, steps);
app.CreateEventExecutionSteps(HttpApplication.EventPreRequestHandlerExecute, steps);
steps.Add(app.CreateImplicitAsyncPreloadExecutionStep()); // implict async preload step
steps.Add(new CallHandlerExecutionStep(app)); // 執行HttpHandler
app.CreateEventExecutionSteps(HttpApplication.EventPostRequestHandlerExecute, steps);
app.CreateEventExecutionSteps(HttpApplication.EventReleaseRequestState, steps);
app.CreateEventExecutionSteps(HttpApplication.EventPostReleaseRequestState, steps);
steps.Add(new CallFilterExecutionStep(app)); // filtering
app.CreateEventExecutionSteps(HttpApplication.EventUpdateRequestCache, steps);
app.CreateEventExecutionSteps(HttpApplication.EventPostUpdateRequestCache, steps);
_endRequestStepIndex = steps.Count;
app.CreateEventExecutionSteps(HttpApplication.EventEndRequest, steps);
steps.Add(new NoopExecutionStep()); // the last is always there
_execSteps = new IExecutionStep[steps.Count];
steps.CopyTo(_execSteps);
// callback for async completion when reposting to threadpool thread
_resumeStepsWaitCallback = stepCallback;
}
集成模式:調用PipelineStepManager類中的 BuildSteps函數
internal override void BuildSteps(WaitCallback stepCallback) {
Debug.Trace("PipelineRuntime", "BuildSteps");
//ArrayList steps = new ArrayList();
HttpApplication app = _application;
// add special steps that don't currently
// correspond to a configured handler
IExecutionStep materializeStep = new MaterializeHandlerExecutionStep(app);
// implicit map step
app.AddEventMapping(
HttpApplication.IMPLICIT_HANDLER,
RequestNotification.MapRequestHandler,
false, materializeStep);
// implicit async preload step
app.AddEventMapping(
HttpApplication.IMPLICIT_HANDLER,
RequestNotification.ExecuteRequestHandler,
false, app.CreateImplicitAsyncPreloadExecutionStep());
// implicit handler routing step
IExecutionStep handlerStep = new CallHandlerExecutionStep(app);
app.AddEventMapping(
HttpApplication.IMPLICIT_HANDLER,
RequestNotification.ExecuteRequestHandler,
false, handlerStep);
// implicit handler WebSockets step
IExecutionStep webSocketsStep = new TransitionToWebSocketsExecutionStep(app);
app.AddEventMapping(
HttpApplication.IMPLICIT_HANDLER,
RequestNotification.EndRequest,
true /* isPostNotification */, webSocketsStep);
// add implicit request filtering step
IExecutionStep filterStep = new CallFilterExecutionStep(app);
// normally, this executes during UpdateRequestCache as a high priority module
app.AddEventMapping(
HttpApplication.IMPLICIT_FILTER_MODULE,
RequestNotification.UpdateRequestCache,
false, filterStep);
// for error conditions, this executes during LogRequest as a high priority module
app.AddEventMapping(
HttpApplication.IMPLICIT_FILTER_MODULE,
RequestNotification.LogRequest,
false, filterStep);
_resumeStepsWaitCallback = stepCallback;
}
10、真正處理請求階段, 調用ResumeSteps函數來執行處理步驟
經典模式:
返回到ProcessRequestInternal函數中執行BeginProcessRequest函數
IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) {
HttpAsyncResult result;
// Setup the asynchronous stuff and application variables
_context = context;
_context.ApplicationInstance = this;
_stepManager.InitRequest();
// Make sure the context stays rooted (including all async operations)
_context.Root();
// Create the async result
result = new HttpAsyncResult(cb, extraData);
// Remember the async result for use in async completions
AsyncResult = result;
if (_context.TraceIsEnabled)
HttpRuntime.Profile.StartRequest(_context);
// Start the application
ResumeSteps(null);//執行處理步驟
// Return the async result
return result;
}
ResumeSteps函數,調用ExecuteStepl函數來執行處理步驟 internal override void ResumeSteps(Exception error) {
bool appCompleted = false;
bool stepCompletedSynchronously = true;
HttpApplication app = _application;
CountdownTask appInstanceConsumersCounter = app.ApplicationInstanceConsumersCounter;
HttpContext context = app.Context;
ThreadContext threadContext = null;
AspNetSynchronizationContextBase syncContext = context.SyncContext;
Debug.Trace("Async", "HttpApplication.ResumeSteps");
try {
if (appInstanceConsumersCounter != null) {
appInstanceConsumersCounter.MarkOperationPending(); // ResumeSteps call started
}
using (syncContext.AcquireThreadLock()) {
//執行步驟
error = app.ExecuteStep(_execSteps[_currentStepIndex], ref stepCompletedSynchronously);
}
}
集成模式:
返回到ProcessRequestNotificationPrivate函數,執行BeginProcessRequestNotification
BeginProcessRequestNotification函數,調用ResumeSteps執行處理步驟
internal IAsyncResult BeginProcessRequestNotification(HttpContext context, AsyncCallback cb)
{
if (this._context == null)
{
this.AssignContext(context);
}
context.CurrentModuleEventIndex = -1;
HttpAsyncResult result = new HttpAsyncResult(cb, context);
context.NotificationContext.AsyncResult = result;
this.ResumeSteps(null);//開始執行處理步驟
return result;
}
?ResumeSteps
// PipelineStepManager::ResumeSteps
// called from IIS7 (on IIS thread) via BeginProcessRequestNotification
// or from an async completion (on CLR thread) via HttpApplication::ResumeStepsFromThreadPoolThread
// This attribute prevents undesirable 'just-my-code' debugging behavior (VSWhidbey 404406/VSWhidbey 609188)
[System.Diagnostics.DebuggerStepperBoundaryAttribute]
internal override void ResumeSteps(Exception error) {
HttpContext context = _application.Context;
IIS7WorkerRequest wr = context.WorkerRequest as IIS7WorkerRequest;
AspNetSynchronizationContextBase syncContext = context.SyncContext;
RequestNotificationStatus status = RequestNotificationStatus.Continue;
ThreadContext threadContext = null;
bool needToDisassociateThreadContext = false;
bool isSynchronousCompletion = false;
bool needToComplete = false;
bool stepCompletedSynchronously = false;
int currentModuleLastEventIndex = _application.CurrentModuleContainer.GetEventCount(context.CurrentNotification, context.IsPostNotification) - 1;
CountdownTask appInstanceConsumersCounter = _application.ApplicationInstanceConsumersCounter;
using (context.RootedObjects.WithinTraceBlock()) {
error = _application.ExecuteStep(step, ref stepCompletedSynchronously);//執行處理步驟
}
}
11、ExecuteStep執行BuildSteps中的各個步驟 internal Exception ExecuteStep(IExecutionStep step, ref bool completedSynchronously) {
Exception error = null;
try {
try {
if (step.IsCancellable) {
_context.BeginCancellablePeriod(); // request can be cancelled from this point
try {
step.Execute();
}
finally {
_context.EndCancellablePeriod(); // request can be cancelled until this point
}
_context.WaitForExceptionIfCancelled(); // wait outside of finally
}
else {
step.Execute();</strong></span>
}
if {
completedSynchronously = false;
return null;
}
}
}
這里貼出了兩個很重要的步驟:獲取HttpHandler(MaterializeHandlerExecutionStep)和執行HttpHandler(CallHandlerExecutionStep)
經典模式:
MapHandlerExecutionStep
// execution step -- map HTTP handler (used to be a separate module)
internal class <span style="color:#FF0000;"><strong>MapHandlerExecutionStep </strong></span>: IExecutionStep {
private HttpApplication _application;
internal MapHandlerExecutionStep(HttpApplication app) {
_application = app;
}
void IExecutionStep.Execute() {
HttpContext context = _application.Context;
HttpRequest request = context.Request;
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_ENTER, context.WorkerRequest);
//獲取HttpHandler
context.Handler = _application.MapHttpHandler(
context,
request.RequestType,
request.FilePathObject,
request.PhysicalPathInternal,
false /*useAppConfig*/);</strong></span>
Debug.Assert(context.ConfigurationPath == context.Request.FilePathObject, "context.ConfigurationPath (" +
context.ConfigurationPath + ") != context.Request.FilePath (" + context.Request.FilePath + ")");
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_LEAVE, context.WorkerRequest);
}
bool pletedSynchronously {
get { return true;}
}
bool IExecutionStep.IsCancellable {
get { return false; }
}
}
MapHttpHandler獲取處理請求的HttpHandler
internal IHttpHandler MapHttpHandler(HttpContext context, string requestType, VirtualPath path, string pathTranslated, bool useAppConfig)
{
IHttpHandler handler = (context.ServerExecuteDepth == 0) ? context.RemapHandlerInstance : null;
using (new ApplicationImpersonationContext())
{
if (handler != null)
{
return handler;
}
HttpHandlerAction mapping = this.GetHandlerMapping(context, requestType, path, useAppConfig);
if (mapping == null)
{
PerfCounters.IncrementCounter(AppPerfCounter.REQUESTS_NOT_FOUND);
PerfCounters.IncrementCounter(AppPerfCounter.REQUESTS_FAILED);
throw new HttpException(SR.GetString("Http_handler_not_found_for_request_type", new object[] { requestType }));
}
IHttpHandlerFactory factory = this.GetFactory(mapping);//獲取HttpHandlerFactory對象,如果網站Web.config中自定義了HttpHandlerFactory對象,則會覆蓋系統默認的
try
{
IHttpHandlerFactory2 factory2 = factory as IHttpHandlerFactory2;
if (factory2 != null)
{
handler = factory2.GetHandler(context, requestType, path, pathTranslated);//獲取HttpHandler對象,如果為自定義的HttpHandlerFactory,則返回自定義HttpHandlerFactory中設置的HttpHandler
}
else
{
handler = factory.GetHandler(context, requestType, path.VirtualPathString, pathTranslated);
}
}
catch (FileNotFoundException exception)
{
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
{
throw new HttpException(0x194, null, exception);
}
throw new HttpException(0x194, null);
}
catch (DirectoryNotFoundException exception2)
{
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
{
throw new HttpException(0x194, null, exception2);
}
throw new HttpException(0x194, null);
}
catch (PathTooLongException exception3)
{
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
{
throw new HttpException(0x19e, null, exception3);
}
throw new HttpException(0x19e, null);
}
if (this._handlerRecycleList == null)
{
this._handlerRecycleList = new ArrayList();
}
this._handlerRecycleList.Add(new HandlerWithFactory(handler, factory));
}
return handler;
}
集成模式:
MaterializeHandlerExecutionStep步驟
internal class MaterializeHandlerExecutionStep: IExecutionStep {
private HttpApplication _application;
internal MaterializeHandlerExecutionStep(HttpApplication app) {
_application = app;
}
void IExecutionStep.Execute() {
HttpContext context = _application.Context;
HttpRequest request = context.Request;
IHttpHandler handler = null;
string configType = null;
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_ENTER, context.WorkerRequest);
IIS7WorkerRequest wr = context.WorkerRequest as IIS7WorkerRequest;
// Get handler
if (context.RemapHandlerInstance != null){
//RemapHandler overrides all
wr.SetScriptMapForRemapHandler();
context.Handler = context.RemapHandlerInstance;
}
else if (request.RewrittenUrl != null) {
// RewritePath, we need to re-map the handler
bool handlerExists;
configType = wr.ReMapHandlerAndGetHandlerTypeString(context, request.Path, out handlerExists);
if (!handlerExists) {
// WOS 1973590: When RewritePath is used with missing handler in Integrated Mode,an empty response 200 is returned instead of 404
throw new HttpException(404, SR.GetString(SR.Http_handler_not_found_for_request_type, request.RequestType));
}
}
else {
configType = wr.GetManagedHandlerType();
}
if (!String.IsNullOrEmpty(configType)) {
IHttpHandlerFactory factory = _application.GetFactory(configType);//獲取HttpHandlerFactory對象
string pathTranslated = request.PhysicalPathInternal;
try {
handler = factory.GetHandler(context, request.RequestType, request.FilePath, pathTranslated);//獲取HttpHandler對象
}
catch (FileNotFoundException e) {
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
throw new HttpException(404, null, e);
else
throw new HttpException(404, null);
}
catch (DirectoryNotFoundException e) {
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
throw new HttpException(404, null, e);
else
throw new HttpException(404, null);
}
catch (PathTooLongException e) {
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
throw new HttpException(414, null, e);
else
throw new HttpException(414, null);
}
context.Handler = handler;
// Remember for recycling
if (_application._handlerRecycleList == null)
_application._handlerRecycleList = new ArrayList();
_application._handlerRecycleList.Add(new HandlerWithFactory(handler, factory));
}
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_LEAVE, context.WorkerRequest);
}
bool pletedSynchronously {
get { return true;}
}
bool IExecutionStep.IsCancellable {
get { return false; }
}
}
12、執行callhandlerexecutestep步驟,調用handler.ProcessRequest(context),如果HttpHandler類型為Page,則開始Page頁生命周期
internal class CallHandlerExecutionStep: IExecutionStep {
private HttpApplication _application;
private AsyncCallback _completionCallback;
private IHttpAsyncHandler _handler; // per call
private AsyncStepCompletionInfo _asyncStepCompletionInfo; // per call
private bool _sync; // per call
internal CallHandlerExecutionStep(HttpApplication app) {
_application = app;
_completionCallback = new AsyncCallback(this.OnAsyncHandlerCompletion);
}
voidIExecutionStep.Execute()
{
HttpContext context = _application.Context;
IHttpHandler handler = context.Handler;
if (EtwTrace.IsTraceEnabled(rmation, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_ENTER, context.WorkerRequest);
if (handler != null && HttpRuntime.UseIntegratedPipeline) {
IIS7WorkerRequest wr = context.WorkerRequest as IIS7WorkerRequest;
if (wr != null && wr.IsHandlerExecutionDenied()) {
_sync = true;
HttpException error = new HttpException(403, SR.GetString(SR.Handler_access_denied));
error.SetFormatter(new PageForbiddenErrorFormatter(context.Request.Path, SR.GetString(SR.Handler_access_denied)));
throw error;
}
}
if (handler == null) {
_sync = true;
}
else if (handler is IHttpAsyncHandler) {
// asynchronous handler
IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler)handler;
_sync = false;
_handler = asyncHandler;
var beginProcessRequestDelegate = AppVerifier.WrapBeginMethod<HttpContext>(_application, asyncHandler.BeginProcessRequest);
_asyncStepCompletionInfo.Reset();
context.SyncContext.AllowVoidAsyncOperations();
IAsyncResult ar;
try {
ar = beginProcessRequestDelegate(context, _completionCallback, null);
}
catch {
// The asynchronous step has completed, so we should disallow further
// async operations until the next step.
context.SyncContext.ProhibitVoidAsyncOperations();
throw;
}
bool operationCompleted;
bool mustCallEndHandler;
_asyncStepCompletionInfo.RegisterBeginUnwound(ar, out operationCompleted, out mustCallEndHandler);
if (operationCompleted) {
_sync = true;
_handler = null; // not to remember
// The asynchronous step has completed, so we should disallow further
// async operations until the next step.
context.SyncContext.ProhibitVoidAsyncOperations();
try {
if (mustCallEndHandler) {
asyncHandler.EndProcessRequest(ar);
}
_asyncStepCompletionInfo.ReportError();
}
finally {
SuppressPostEndRequestIfNecessary(context);
// In Integrated mode, generate the necessary response headers
// after the handler runs
context.Response.GenerateResponseHeadersForHandler();
}
if (EtwTrace.IsTraceEnabled(rmation, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
}
}
else {
//同步 handler
_sync = true;
context.SyncContext.SetSyncCaller();
try {
handler.ProcessRequest(context);//開始Page頁面生命周期
}
finally {
context.SyncContext.ResetSyncCaller();
if (EtwTrace.IsTraceEnabled(rmation, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
SuppressPostEndRequestIfNecessary(context);
context.Response.GenerateResponseHeadersForHandler();
}
}
}
bool pletedSynchronously {
get { return _sync;}
}
bool IExecutionStep.IsCancellable {
// launching of async handler should not be cancellable
get { return (_application.Context.Handler is IHttpAsyncHandler) ? false : true; }
}
}
13、進入Page類的 ProcessRequest方法開始處理請求
private void ProcessRequest() {
// culture needs to be saved/restored only on synchronous pages (if at all)
// save culture
Thread currentThread = Thread.CurrentThread;
CultureInfo prevCulture = currentThread.CurrentCulture;
CultureInfo prevUICulture = currentThread.CurrentUICulture;
try {
ProcessRequest(true /*includeStagesBeforeAsyncPoint*/, true /*includeStagesAfterAsyncPoint*/);//調用ProcessRequest函數
}
finally {
// restore culture
RestoreCultures(currentThread, prevCulture, prevUICulture);
}
}
14、進入ProcessRequest,調用ProcessRequestMain函數
private void ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) {
// Initialize the object and build the tree of controls.
// This must happen *after* the intrinsics have been set.
// On async pages only call Initialize once (ProcessRequest is called twice)
if (includeStagesBeforeAsyncPoint) {
FrameworkInitialize();
this.ControlState = ControlState.FrameworkInitialized;
}
bool needToCallEndTrace = Context.WorkerRequest is IIS7WorkerRequest;
try {
try {
if (IsTransacted) {
ProcessRequestTransacted();
}
else {
// No transactions
ProcessRequestMain(includeStagesBeforeAsyncPoint, includeStagesAfterAsyncPoint);//調用ProcessRequestMain函數
}
if (includeStagesAfterAsyncPoint) {
needToCallEndTrace = false;
ProcessRequestEndTrace();
}
}
catch (ThreadAbortException) {
try {
if (needToCallEndTrace)
ProcessRequestEndTrace();
} catch {}
}
finally {
if (includeStagesAfterAsyncPoint) {
ProcessRequestCleanup();
}
}
}
catch { throw; } // Prevent Exception Filter Security Issue (ASURT 122835)
}
15、進入ProcessRequestMain,開始Page頁面主體處理過程
private void ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) {
try {
HttpContext con = Context;
string exportedWebPartID = null;
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin PreInit");
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_ENTER, _context.WorkerRequest);
PerformPreInit(); //Page頁面生命周期的OnPerLoad階段
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End PreInit");
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin Init");
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_ENTER, _context.WorkerRequest);
InitRecursive(null);
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End Init");
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin InitComplete");
OnInitComplete(EventArgs.Empty);//Page頁面生命周期的OnInitComplete階段
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End InitComplete");
if (IsPostBack) {
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin LoadState");
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_ENTER, _context.WorkerRequest);
LoadAllState();//Page頁面生命周期的LoadViewState階段(加載視圖狀態)
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) {
Trace.Write("aspx.page", "End LoadState");
Trace.Write("aspx.page", "Begin ProcessPostData");
}
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_ENTER, _context.WorkerRequest);
ProcessPostData(_requestValueCollection, true /* fBeforeLoad */);//Page頁面生命周期ProcessPostData階段(處理回發數據)
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End ProcessPostData");
}
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin PreLoad");
OnPreLoad(EventArgs.Empty);//Page頁面生命周期OnPreLoad階段(預加載)
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End PreLoad");
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin Load");
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_ENTER, _context.WorkerRequest);
LoadRecursive();//Page頁面生命周期Load階段(加載)
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End Load");
if (IsPostBack) {
// Try process the post data again (ASURT 29045)
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin ProcessPostData Second Try");
<span style="color:#FF0000;"><strong> ProcessPostData(_leftoverPostData, false /* !fBeforeLoad */);//Page頁面生命周期 ProcessPostData階段</strong></span>
if (con.TraceIsEnabled) {
Trace.Write("aspx.page", "End ProcessPostData Second Try");
Trace.Write("aspx.page", "Begin Raise ChangedEvents");
}
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_ENTER, _context.WorkerRequest);
RaiseChangedEvents();//Page頁面生命周期 RaiseChangedEvents階段(處理回發更改事件)
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) {
Trace.Write("aspx.page", "End Raise ChangedEvents");
Trace.Write("aspx.page", "Begin Raise PostBackEvent");
}
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_ENTER, _context.WorkerRequest);
RaisePostBackEvent(_requestValueCollection);//Page頁面生命周期 RaisePostBackEvent階段(處理回發事件)
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End Raise PostBackEvent");
}
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin LoadComplete");
OnLoadComplete(EventArgs.Empty);//Page頁面生命周期 加載完成
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End LoadComplete");
if (IsPostBack && IsCallback) {
PrepareCallback(callbackControlId);//Page頁面生命周期 PrepareRender(預呈現)
}
else if (!IsCrossPagePostBack) {
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin PreRender");
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_ENTER, _context.WorkerRequest);
PreRenderRecursiveInternal();
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End PreRender");
}
}
/// Async Point here
if (_legacyAsyncInfo == null || _legacyAsyncInfo.CallerIsBlocking) {
// for non-async pages with registered async tasks - run the tasks here
// also when running async page via server.execute - run the tasks here
ExecuteRegisteredAsyncTasks();
}
// Make sure RawUrl gets validated.
ValidateRawUrlIfRequired();
if (includeStagesAfterAsyncPoint) {
if (IsCallback) {
RenderCallback();
return;
}
if (IsCrossPagePostBack) {
return;
}
if (con.TraceIsEnabled) Trace.Write("aspx.page", "Begin PreRenderComplete");
PerformPreRenderComplete();
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End PreRenderComplete");
if (con.TraceIsEnabled) {
BuildPageProfileTree(EnableViewState);
Trace.Write("aspx.page", "Begin SaveState");
}
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER, _context.WorkerRequest);
SaveAllState();//Page頁面生命周期 SaveAllState階段(保存視圖狀態)
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) {
Trace.Write("aspx.page", "End SaveState");
Trace.Write("aspx.page", "Begin SaveStateComplete");
}
OnSaveStateComplete(EventArgs.Empty);
if (con.TraceIsEnabled) {
Trace.Write("aspx.page", "End SaveStateComplete");
Trace.Write("aspx.page", "Begin Render");
}
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER, _context.WorkerRequest);
// Special-case Web Part Export so it executes in the same security context as the page itself (VSWhidbey 426574)
if (exportedWebPartID != null) {
ExportWebPart(exportedWebPartID);
}
else {
RenderControl(CreateHtmlTextWriter(Response.Output));//Page頁面生命周期 Render階段(呈現)
}
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_LEAVE, _context.WorkerRequest);
if (con.TraceIsEnabled) Trace.Write("aspx.page", "End Render");
CheckRemainingAsyncTasks(false);
}
}
至此,整個IIS處理完整請求就結束了,列舉了各個關鍵處理階段的處理函數。當我們了解.net framework內部處理機制后能夠使我們理解運行機制和本質,對開發人員來說這很重要,不能只停留在會用會寫的階段,而且要知道其內部原理。這樣不僅可以幫助我們編寫更高效的代碼,同時可以學習并借鑒微軟的面向對象的編程思想,受益頗多!
總結
以上是生活随笔為你收集整理的iis运行原理 Asp.Net详解IIS内部运行原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奥迪起诉蔚来车名侵权引热议!小鹏总裁发声
- 下一篇: 乐视视频APP图标更新:加了7个字 贾跃