您的位置:首页 > 移动开发

Debug your ASP.NET Application while Hosted on IIS

2015-03-10 07:33 573 查看
http://www.codeproject.com/Articles/37182/Debug-your-ASP-NET-Application-while-Hosted-on-IIS

ThisarticledescribeshowtodebugawebapplicationwhichishostedonIIS.Italsodescribeshowtoselectthecorrectprocesstoattachtowhenmultipleworkerprocessesarerunning


Contents

Overview
ASP.NET
Debuggingvs.IISDebugging
What
istheWorkerProcess?
Application
Pool

What
isApplicationPool?
Default
ApplicationPool
Creating
andAssigninganApplicationPool
Assign
SitetotheApplicationPool

How
tostart?
Which
processtoattachto?
How
toattachtooneofmanyrunningWorkerProcesses

Getting
alistofrunningWorkerProcesses
Attaching
tothecorrectprocess

Summary


Overview

GenerallywedebugourASP.NETwebapplicationfromVisualStudio.VisualStudiohasitsownASP.NETengine,whichiscapableenoughtorunanddebugyourwebsitesinsideVisualStudio.However,ifyoursiteishostedonIISandyouwanttodebugthatsite
directly,howwouldyoudebugit?WhenwehostsitesonIIS,theWorkerProcess(w3wp.exe)isusedtorunthewebapplication.WeneedtoattachtothisparticularprocessfromVisualStudiotodebug
thewebapplication.Thisarticledescribestheoverallideaofdebugginganapplicationusingthismethod.ItalsodescribestheWorkerProcess,Application
PoolandselectingaparticularprocessiftherearemultipleWorkerProcessesrunningonIIS,using
iisapp.vbs
.
Ihopeyouwillenjoythisarticleandprovideyourvaluablesuggestionsandfeedback.


ASP.NETDebuggingvs.IISDebugging

VisualStudiohasitsownintegrateddebuggingengine,whichdebugsourcodewhenweruntheapplicationinVisualStudio.Ifwearedevelopingasiteandneedtodebugthecode,wejustsetbreakpointsanddothedebugging(Note:
InthisarticleIdonotdescribehowtosetthedebugmode).

Whenweruntheapplication,executionbreakswhencertainabreakpointisreached.Itisverysimple,becausewhenanASP.NETapplicationisrunninginVisualStudio,itisunderthecontroloftheASP.NET
EnginewhichisintegratedwithVisualStudio.Ifyouwanttocheckwhichprocessisrunningfordebugging,runthewebapplicationfromVisualStudio:youwillgetapopupnotificationasshownbelow.



Fig.1:TaskbarpopupwhendebuggingisstartedfromVisualStudio
ThisindicatesaprocessisstartingtoruntheASP.NETapplication.Double-clickontheiconandapopupwindowwillappeartoshowthedetails.



Fig.2:DevelopmentServerprocessdetails
BehindtherunningprocessisWebDev.WebServer.exe.WhenWepressF5toruntheapplication,thisprocessstartstoexecutetheit.Ifyouwant
runtheapplicationfromcommandprompt,youhavetoperformthefollowingsteps.

Stepstorunawebapplicationfromthecommandprompt:

OpenTheVisualStudiocommandprompt
RunWebDev.WebServer

Thefollowingscreenwillcomeup.ChecktheExamplesectionthere.



Fig.3:WebDev.WebServerusagenotification
NowbacktoIISdebugging.IIScomesintothepicturewhenwedeployorhostthesite.AfterdeployingthesiteonIIS,ifwewanttodebugthesitethere,wecan'tdoitdirectlyasinVisualStudio.IIShasitsown
Worker
Processwhichtakescareofallexecutionandmaintenanceofdeployedwebapplications.IwilldescribethedetailsoftheWorkerProcessinalatersection.So,ifwehaverunningprocessinIISandweneedtodebugtheapplication,firstofallwe
havetoattachtothecorrectprocessfromVisualStudio.Beforedescribingthat,let'sjusthavealookattheWorkerProcessandApplicationPool.


WhatistheWorkerProcess?

TheWorkerProcess(w3wp.exe)runsASP.NETapplicationswithinIIS.AllASP.NETfunctionalityrunsunderthescopeoftheWorkerProcess.Whenarequestcomestotheserverfromaclient,theWorker
Processisresponsibleforgeneratingtherequestandresponse.ItsalsomaintainstheInProcsessiondata.IfwerecycletheWorkerProcess,wewillloseitsstate.Formoreinformation,readthisarticle:A
Low-LevelLookattheASP.NETArchitecture

ApplicationPool

Thisisoneofthemostimportantthingsthatyoushouldcreateforyourownapplicationinaproductionenvironment.ApplicationPoolsareusedtoseparatesetsofIISWorkerProcessesthatsharethesameconfiguration.ApplicationPoolsenableustoisolate
ourwebapplicationforbettersecurity,reliability,andavailability.TheWorkerProcessservesastheprocessboundarythatseparateseachApplicationPool,sothatwhenoneWorkerProcessorapplicationhasanissueorrecycles,otherapplicationsorWorker
Processesarenotaffected.



Fig.4:RelationshipbetweenApplicationPoolandworkerprocessinIIS

DefaultApplicationPool

ThenameofthedefaultapplicationofIIS6.0isDefaultAppPool.AfterhostingthesiteonIIS,ifwecheckthepropertiesofthevirtualdirectory,wecantoviewthatinformationasfollows.

StartMenu→Runcommand→
inetmgr

ExpandDefaultWebSitesorOtherWebSites,whereyouhavecreatedthevirtualdirectory
RightClickonthevirtualdirectory
ClickonProperties

Thefollowingvirtualdirectorypropertiesscreenwillcomeup,showingtheApplicationPoolnamewhichisassignedtotheselectedsite.



Fig.5:VirtualdirectorypropertiesshowingApplicationPoolname
IfyouwanttocheckthelistofallApplicationPoolsinIIS,youhavetoexpandtheApplicationPoolnodeonIISServer.



Fig.6:ListofApplicationPools
Now,eachandeveryApplicationPoolshouldhavetheminimumofoneWorkerProcesswhichtakescareoftheoperationofthesitewhichisassociatedwiththeApplicationPool.Right-clickontheApplicationPool→gotothePerformancetab,
checknearthebottomofthetab,thereisawebgardensection,andbydefault,thenumberofWorkerProcessesis1.AnApplicationPoolcontainingmorethanoneWorkerProcesscalledaWeb
Garden.



Fig.7:ApplicationPoolpropertiesshowingWebgarden

CreatingandAssigninganApplicationPool

OpentheIISConsole,right-clickontheApplicationPoolsfolder,selectNew



Fig.8-1

GivetheApplicationPoolIDandclickOK.



Fig.8-2

Now,right-clickonthevirtualdirectoryandassignthenewlycreatedApplicationPooltothatvirtualdirectory.



Fig.8-3

Now,thiswebsitewillrunindependently,withinStateServerAppPool,soanyproblemrelatedtootherapplicationswillnotaffectthisapplication.Thisisthemainadvantageofcreatingaseparate
ApplicationPool.


Howtostart?

WhatIhavesaiduptonowgiveyouagoodideaofWorkerProcessesandApplicationPools.Youshouldhaveaclearunderstandingonthesebeforegoingontothenextpart.NowIwillshowyouhowtodebugasitewhichishostedonanIISServer.

Forthedemonstration,IhavecreatedawebsitecalledSampleWebSiteandhosteditontomylocalIIS.Belowisdefaultpageoutput.



Fig.9:Samplewebsite


Whichprocesstoattachto?

Now,asIhavealreadyexplained,theprocessnameisw3wp.exe,sowecancheckitfromourTaskManagerwhetherornottheWorkerProcessisrunning.



Fig.10:TaskManagershowingtherunningIISprocess
Nowwearegoingtoattachtotheprocess.InVisualStudio,gotoDebug→AttachtoProcess



Fig.11:OpeningtheAttachtoProcesswindow
AfterclickingAttachtoProcess,thefollowingscreenwillcomeup



Fig.12:AttachtoProcesswindow,showingasingleWorkerProcessrunning
NowweareabletoseethattheWorkerProcessisrunning,andweneedtoattachthatprocess.SelecttheprocessandclickontheAttachbutton.Afterthat,lookatthetwoimagesbelow:



Fig.13-1:Processattachedsuccessfully



Fig.13-2:Processnotattached
Didyounoticethebreakpointsymbol?IftheWorkerProcessattachedsuccessfully,withinthecode,thebreakpointsymbolshouldbeasolidcircle.Otherwiseitwillhaveawarningiconasshown.ForasingleWorkerProcess,thisscenarioisnotcommon.
However,whenwehavemultipleWorkerProcessesrunningonIIS,thenwecanhavesomeconfusion.Iwilldiscussthesameinalatersection.

NowifweclicktheDebugbuttonaftersuccessfullyattachingtotheprocess,executionwillstopatthebreakpoint.

Next,let'shavealookatwhattodoifwehavemultipleWorkerProcessesrunning.


HowtoattachtooneofmanyrunningWorkerProcesses

Now,whenthisscenariowillcomeup?WhenwehavemultiplesiteshostedonIIS,andthosesiteshavetheirownApplicationPool.Now,multipleApplicationPoolsmeansmultipleWorkerProcessesarerunning.

HereIhavethreeApplicationPoolsinmyIIS.Theyare:

DefaultApplicationPool
GenericApplicationPool
StateServerApplicationPool

Now,mySampleWebSiteisassociatedwiththeDefaultAppPool.Now,IwanttoattachtheprocesstodebugmySampleWebSite.
Followthesamestepsasbefore.OpentheProcessAttachwindow:



Fig.14:ListofmultipleWorkerProcess
Justhavealook,therearethreeWorkerProcessescurrentlyrunning,andyouhavetoattachoneofthem.But,youdonotknowwhichWorkerProcessisthedefaultApplicationPool's.Whatyoudois,youselectanyoneofthematrandom,let'ssaytheone
withprocessID=4308
,
andsupposeitisnottheWorkerProcessforthedefaultApplicationPool.Sowhatwillhappen
ifyouattachtoawrongprocess?Checktheimagebelow:



Fig.15:Breakpointwhentheprocessisnotattachedcorrectly

GettingalistofrunningWorkerProcesses

Nowwhatisthesolutionforthepreviouscase?Hereisaquicktip:

Start→Runcommand→cmd
Changedirectoryto\Windows\System32
Runthecommand:
cscriptiisapp.vbs


andwaitfortheoutput.Wow!YougetalistofrunningWorkerProcess,ProcessIDandApplication
PoolName!



Fig.16:ListofrunningWorkerProcesseswithPIDandApplicationPoolname

Attachingtothecorrectprocess

FromhereyoucaneasilyidentifythecorrectApplicationPoolnameanditsprocessID.Now,returntoVisualStudio→AttachProcess.NowyouknowthattheprocessIDforDefaultApplicationPoolis
1772
,
soAttachtothatprocess.



Fig.17:Attachthecorrectprocessfordebugging
Now,enjoydebugging!



Fig.18:Breakpointisready


Summary

SometimesweneedtodebugourapplicationwhichishostedonIIS.Forthat,weneedtoattachtherunningWorkerProcesstotheVisualStudio.IfwehavemultipleWorkerProcessesrunningontheIISserver,wecanidentifytheappropriateWorkerProcess
byusingthecommand
cscriptiisapp.vbs
.

IhopethisarticlewillhelpbeginnerswhoarestillstrugglingwithdebuggingapplicationsthatarehostedonIIS.Pleasegiveyourfeedbackandsuggestionstoimprovethearticle.

Thankyou.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: