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
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
GenerallywedebugourASP.NETwebapplicationfromVisualStudio.VisualStudiohasitsownASP.NETengine,whichiscapableenoughtorunanddebugyourwebsitesinsideVisualStudio.However,ifyoursiteishostedonIISandyouwanttodebugthatsite
directly,howwouldyoudebugit?WhenwehostsitesonIIS,theWorkerProcess(w3wp.exe)isusedtorunthewebapplication.WeneedtoattachtothisparticularprocessfromVisualStudiotodebug
thewebapplication.Thisarticledescribestheoverallideaofdebugginganapplicationusingthismethod.ItalsodescribestheWorkerProcess,Application
PoolandselectingaparticularprocessiftherearemultipleWorkerProcessesrunningonIIS,using
Ihopeyouwillenjoythisarticleandprovideyourvaluablesuggestionsandfeedback.
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.
RunWebDev.WebServer
Thefollowingscreenwillcomeup.ChecktheExamplesectionthere.
Fig.3:WebDev.WebServerusagenotification
NowbacktoIISdebugging.IIScomesintothepicturewhenwedeployorhostthesite.AfterdeployingthesiteonIIS,ifwewanttodebugthesitethere,wecan'tdoitdirectlyasinVisualStudio.IIShasitsownWorker
Processwhichtakescareofallexecutionandmaintenanceofdeployedwebapplications.IwilldescribethedetailsoftheWorkerProcessinalatersection.So,ifwehaverunningprocessinIISandweneedtodebugtheapplication,firstofallwe
havetoattachtothecorrectprocessfromVisualStudio.Beforedescribingthat,let'sjusthavealookattheWorkerProcessandApplicationPool.
TheWorkerProcess(w3wp.exe)runsASP.NETapplicationswithinIIS.AllASP.NETfunctionalityrunsunderthescopeoftheWorkerProcess.Whenarequestcomestotheserverfromaclient,theWorker
Processisresponsibleforgeneratingtherequestandresponse.ItsalsomaintainstheInProcsessiondata.IfwerecycletheWorkerProcess,wewillloseitsstate.Formoreinformation,readthisarticle:A
Low-LevelLookattheASP.NETArchitecture
ourwebapplicationforbettersecurity,reliability,andavailability.TheWorkerProcessservesastheprocessboundarythatseparateseachApplicationPool,sothatwhenoneWorkerProcessorapplicationhasanissueorrecycles,otherapplicationsorWorker
Processesarenotaffected.
Fig.4:RelationshipbetweenApplicationPoolandworkerprocessinIIS
StartMenu→Runcommand→
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
Fig.8-1
GivetheApplicationPoolIDandclickOK.
Fig.8-2
Now,right-clickonthevirtualdirectoryandassignthenewlycreatedApplicationPooltothatvirtualdirectory.
Fig.8-3
Now,thiswebsitewillrunindependently,withinStateServerAppPool,soanyproblemrelatedtootherapplicationswillnotaffectthisapplication.Thisisthemainadvantageofcreatingaseparate
ApplicationPool.
WhatIhavesaiduptonowgiveyouagoodideaofWorkerProcessesandApplicationPools.Youshouldhaveaclearunderstandingonthesebeforegoingontothenextpart.NowIwillshowyouhowtodebugasitewhichishostedonanIISServer.
Forthedemonstration,IhavecreatedawebsitecalledSampleWebSiteandhosteditontomylocalIIS.Belowisdefaultpageoutput.
Fig.9:Samplewebsite
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.
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
ifyouattachtoawrongprocess?Checktheimagebelow:
Fig.15:Breakpointwhentheprocessisnotattachedcorrectly
Start→Runcommand→cmd
Changedirectoryto\Windows\System32
Runthecommand:
andwaitfortheoutput.Wow!YougetalistofrunningWorkerProcess,ProcessIDandApplication
PoolName!
Fig.16:ListofrunningWorkerProcesseswithPIDandApplicationPoolname
soAttachtothatprocess.
Fig.17:Attachthecorrectprocessfordebugging
Now,enjoydebugging!
Fig.18:Breakpointisready
SometimesweneedtodebugourapplicationwhichishostedonIIS.Forthat,weneedtoattachtherunningWorkerProcesstotheVisualStudio.IfwehavemultipleWorkerProcessesrunningontheIISserver,wecanidentifytheappropriateWorkerProcess
byusingthecommand
IhopethisarticlewillhelpbeginnerswhoarestillstrugglingwithdebuggingapplicationsthatarehostedonIIS.Pleasegiveyourfeedbackandsuggestionstoimprovethearticle.
Thankyou.
ThisarticledescribeshowtodebugawebapplicationwhichishostedonIIS.Italsodescribeshowtoselectthecorrectprocesstoattachtowhenmultipleworkerprocessesarerunning
Contents
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
Overview
GenerallywedebugourASP.NETwebapplicationfromVisualStudio.VisualStudiohasitsownASP.NETengine,whichiscapableenoughtorunanddebugyourwebsitesinsideVisualStudio.However,ifyoursiteishostedonIISandyouwanttodebugthatsitedirectly,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:
OpenTheVisualStudiocommandpromptRunWebDev.WebServer
Thefollowingscreenwillcomeup.ChecktheExamplesectionthere.
Fig.3:WebDev.WebServerusagenotification
NowbacktoIISdebugging.IIScomesintothepicturewhenwedeployorhostthesite.AfterdeployingthesiteonIIS,ifwewanttodebugthesitethere,wecan'tdoitdirectlyasinVisualStudio.IIShasitsownWorker
Processwhichtakescareofallexecutionandmaintenanceofdeployedwebapplications.IwilldescribethedetailsoftheWorkerProcessinalatersection.So,ifwehaverunningprocessinIISandweneedtodebugtheapplication,firstofallwe
havetoattachtothecorrectprocessfromVisualStudio.Beforedescribingthat,let'sjusthavealookattheWorkerProcessandApplicationPool.
WhatistheWorkerProcess?
TheWorkerProcess(w3wp.exe)runsASP.NETapplicationswithinIIS.AllASP.NETfunctionalityrunsunderthescopeoftheWorkerProcess.Whenarequestcomestotheserverfromaclient,theWorkerProcessisresponsibleforgeneratingtherequestandresponse.ItsalsomaintainstheInProcsessiondata.IfwerecycletheWorkerProcess,wewillloseitsstate.Formoreinformation,readthisarticle:A
Low-LevelLookattheASP.NETArchitecture
ApplicationPool
Thisisoneofthemostimportantthingsthatyoushouldcreateforyourownapplicationinaproductionenvironment.ApplicationPoolsareusedtoseparatesetsofIISWorkerProcessesthatsharethesameconfiguration.ApplicationPoolsenableustoisolateourwebapplicationforbettersecurity,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,selectNewFig.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.NowyouknowthattheprocessIDforDefaultApplicationPoolis1772,
soAttachtothatprocess.
Fig.17:Attachthecorrectprocessfordebugging
Now,enjoydebugging!
Fig.18:Breakpointisready
Summary
SometimesweneedtodebugourapplicationwhichishostedonIIS.Forthat,weneedtoattachtherunningWorkerProcesstotheVisualStudio.IfwehavemultipleWorkerProcessesrunningontheIISserver,wecanidentifytheappropriateWorkerProcessbyusingthecommand
cscriptiisapp.vbs.
IhopethisarticlewillhelpbeginnerswhoarestillstrugglingwithdebuggingapplicationsthatarehostedonIIS.Pleasegiveyourfeedbackandsuggestionstoimprovethearticle.
Thankyou.
相关文章推荐
- Debug your ASP.NET Application while Hosted on IIS
- Remote IIS Debugging : Debug your ASP.NET Application which is hosted on "Remote IIS Server"
- Remote IIS Debugging : Debug your ASP.NET Application which is hosted on "Remote IIS Server"
- Test your ASP.NET MVC or WebForms Application on IIS 7 in 30 seconds
- Remoting from IIS Hosted component to ASP.NET Client application
- enable all verbs to application hosting on IIS 7.5 and using Asp.net 4.0
- Your First ASP.NET 5 Application on a Mac
- [ASP.NET] - 提示错误:The server has encountered an error while loading an application during the processing your request
- How To Debug an ASP.NET Web Application
- IIS and ASP.NET: The Application Pool(2)
- debug your asp.net custom control in design time
- Managing Your ASP.NET Application
- Creating portable ASP.NET applications that work on IIS 6.0, IIS 7.0 Classic, and IIS 7.0 Integrated modes
- Build Your ASP.NET Pages on a Richer Bedrock
- Configuring IIS to Run 32-bit ASP.NET Applications on 64-bit Windows (IIS 6.0)
- Build Your ASP.NET Pages on a Richer Bedrock
- A frame in a frameset may become blank on an ASP page or in an ASP.NET application(frameset 可能变成空白)
- 【asp.net】Using ASP.NET MVC on IIS 5
- Managing Your ASP.NET Application[3]->Using the Management API
- IIS and ASP.NET: The Application Pool