Tải bản đầy đủ - 0 (trang)
Chapter 13: Deploying an ASP.NET Application

Chapter 13: Deploying an ASP.NET Application

Tải bản đầy đủ - 0trang

Overview

Inthischapter,youwilllearnhowto:

Storeapplication-specificconfigurationsettingsforeasy

deployment.

SetupatargetIISapplication.

Copycontent,WebFormspages,Webservices,andassemblies.

DeployanapplicationusingVisualStudio.NET.

Deployingapplicationshasbeenalong-standingbaneofmanyaclassic

ASPdevelopersexistence,andforgoodreason.Whileitwaseasy

enoughtodeployorreplacestaticcontentandtheASPfilesthemselves,

otherportionsoftheapplications,suchascomponentsandapplicationspecificconfigurationsettings,requiredgreaterefforttodeployandoften

necessitatedashutdownoftheapplicationtoreplace.

DeploymentisanotherareainwhichtheimprovementsinASP.NETtruly

shine.ASP.NETeliminatesmanyofthedeploymentshortcomingsin

classicASP,makingitpossibleforthefirsttimetodeployanASP.NET

applicationsimplybycopyingallofthenecessaryfilestotheIIS

applicationdirectorywheretheapplicationwillbedeployed.Thischapter

willdiscussthestepsfordeployingASP.NETWebapplications,both

manuallyandthroughMicrosoftVisualStudio.NET.



UnderstandingtheStructureofASP.NET

Applications

AlthoughyoulearnedaboutthestructureofASP.NETWebapplications

earlierinthebook,itisworthwhiletoreviewthisinformation.

UnderstandingthestructureofanASP.NETapplicationwillmakethe

deploymentprocesssmoother,andcanhelppreventunexpectederrors

andproblemsinyourdeployedapplications.

EachASP.NETapplicationneedstohaveacorrespondingIISapplication

roottofunctionproperly.DuringdevelopmentusingVisualStudio.NET,

thisprocessishandledforyouautomatically.Eachtimeyoucreateanew

ASP.NETprojectinVisualStudio.NET,partoftheproject-creation

processundertakenbytheIDEiscreatingthefolderandmakingitanIIS

applicationroot.Theapplicationrootprovidestheboundaryforthe

application.Thephysicalfilesystemdirectorythatmapstothe

applicationrootisthelocationwheretherootWeb.configfileforthe

application,aswellastheapplicationsbindirectory(whichcontainsall

managedassembliesrelatedtotheapplication)andtheGlobal.asaxfile

(ifyouchoosetouseone),aredeployed.IftheIISdirectorythatisthe

rootoftheapplicationisnotconfiguredasanapplicationroot,certain

configurationsettingscanfail,andtheassemblieslocatedinthebin

directory(ifpresent)willnotbeloaded.

Important MicrosoftWindowsXPoffersmultipleviewsoftheControl

Panel.Thedefaultviewiscalledcategoryviewandthe

optionalviewiscalledtheclassicview.Forthischapter,

allinstructionswillbefortheclassicview.Toswitchfrom

categoryviewtoclassicview,clickonSwitchToClassic

ViewontheleftsideoftheControlPanel.

TodetermineifadirectoryinIIShasbeenconfiguredasanapplication,

completethefollowingsteps.



DetermineifadirectoryinIIShasbeenconfiguredasan

application

1. OpentheInternetInformationServicesMMCsnap-in(called



InternetServicesManagerinMicrosoftWindows2000)by

clickingStart,ControlPanel,Administrativetools,andthen

selectingInternetInformationServices.(InWindows2000,click

Start,Programs,AdministrativeTools,andthenInternet

ServicesManager.)Notethatyoumustbeloggedinasan

administrator(orusetheRunAsfeaturetorunthesnap-in)to

accessthefullfunctionalityofthistool.

2. Navigatetothedirectorywhoseconfigurationyouwanttocheck

byexpandingtheWebsitecontainingitandexpandingother

nodesasnecessary.Ifthedirectoryisconfiguredasan

applicationroot,itwillshowaniconofablackglobeinagray

box.Ifthedirectoryisnotconfiguredasanapplication,itwill

eithershowafoldericon(forcontentthatresidesdirectlywithin

thepathoftheparentapplicationroot)orafoldericonwitha

smallglobe(forcontentthatresideselsewhereinthefile

system).Thefollowingillustrationshowsexamplesofthese

icons:



3. Ifthedirectoryisnotconfiguredasanapplication,youcan

createanapplicationrootatthislevel.Right-clickthedirectory,

selectProperties,andthenclicktheCreatebuttoninthe

Directorytab(orVirtualDirectorytab)ofthePropertiesdialog

box.ThePropertiesdialogboxfortheChapter_13directoryis

shownintheillustrationonthenextpage.(Notethatthedialog

forIIS6.0isslightlydifferentintheoptionsitpresents.)



4. ClickOKtoupdatethedirectorywiththenewsetting.

Thedirectoryshouldnowshowtheiconforanapplicationroot,

asshowninthefollowingillustration.Thisstepshouldnotbe

necessaryfordirectoriesthataretherootofaVisualStudio

.NETWebApplicationproject,sinceVisualStudio.NET

configurestherootdirectoryofthisprojecttypeasan

ApplicationinIISautomatically.



Asyoucanseeinthesefigures,IISapplicationrootscanbenestedone

beneaththeother.However,eachapplicationrootdefinesitsown

applicationboundary.Oneadvantageofthisisthatitallowsyouto

partitionyourapplicationasnecessary.Forexample,youcanprovide

customizedconfigurationsettingsorusedifferentversionsofaprivate



assemblywithdifferentpartsofyourapplication.Themostimportant

thingtoremember,however,isthattheapplicationrootiswherethebin

directoryandGlobal.asaxandWeb.configfilesshouldbelocated.



DistinguishingBetweenPhysicalPathandURL

Animportantdistinctiontomakewhendiscussingdeploymentisbetween

thephysicalpathofanapplication(thatis,theapplicationslocationwithin

thefilesystemofitsserver)andtheURLoftheapplication.This

distinctionbecomesimportantwhendiscussingthearrayofdifferenttools

availablefordeployment,someofwhich(suchastheDOSXCOPY

command)usefileornetworkpaths,andsomeofwhich(suchasFTP

andWebDAV)useURLs.

ThereisonlyasinglefilepathforagivenapplicationrootinIIS.However,

morethanonedirectoryinIIScanmaptoasinglephysicaldirectoryin

thefilesystem.ThisallowsyoutosetupURLsforbothpublicandprivate

accesstoagivenapplication,withdifferentIISsettingsforeach.Ifmore

thanoneIISapplicationrootpointstothesamephysicaldirectoryinthe

filesystem,eachIISapplicationrootwillhaveitsownIIS-specific

settings.Itisimportanttonote,however,thatifthephysicaldirectory

containsASP.NET-specificconfigurationandstart-upfiles(Web.config

andGlobal.asax),bothIISapplicationswillsharethesettingsinthese

files.

Oneimportantcaveatregardingthissharingisthatwhenasubfolder

containingaWeb.configfileoraGlobal.asaxfileisdefinedasan

applicationrootinoneoftheIISapplications,itsnotconfiguredasan

applicationrootinthesecondIISapplication.Inthefirstcase,shownin

theillustrationonthenextpage,requeststotheChapter_13subfolderof

theaspnetsbsfolderwillusetheGlobal.asaxfilecontainedinthe

Chapter_13folderforstart-upcode.



Inthesecondcase,showninthefollowingillustration,theGlobal.asax

filecontainedwithinthissubfolderwillbeignored,sincethefolderisnot

configuredasanapplicationinIIS.



YoushouldalsonotethatcertainsettingsintheWeb.configfileare

applicableonlyattheapplicationrootlevel.Forexample,ifthe

Web.configfilecontainedintheChapter_13foldershowninthe

precedingillustrationcontainsansection,a

configurationerrorwillresultbecausethissectioncanbedefinedonlyat

themachine(machine.config)orapplicationrootlevel.

TheURLforaspecificapplicationrootinIISisaproductofthedomain

name(ifany)associatedwiththeIPaddressthatisassignedtotheIIS

Website,plusthefolderhierarchybetweentheIISWebsiteandthe



applicationroot.Thus,inthepreviousillustration,wheretheaspnetsbs

applicationrootresidesunderthedefaultWebsite,theURLtoreachthe

Chapter_13directorywouldbeeither

http://localhost/aspnetsbs/Chapter_13/or

http://servername/aspnetsbs/Chapter_13/.

ThisworksifyourequesttheURLfromtheservercontainingthe

application,becauserequestsforlocalhostorservername(where

servernamerepresentsthenameoftheWebservercontainingthe

application)areautomaticallydirectedtothedefaultWebsite.Ifyou

wantedtomakethecontentoftheChapter_13applicationrootavailable

ontheInternet,youdneedtocreateanewIISWebsite,assignapublicly

availableIPaddressonthemachinetotheWebsite,andthencreate(or

haveaDNSprovidercreate)aDNSentrythatmapsyourchosendomain

name(forexample,http://www.aspnetsbs.com/)tothatIPaddress.Then

youwouldcreateanewapplicationrootunderthenewWebsitethat

mapstotheChapter_13directoryinthefilesystem.Youcouldaccess

thisapplicationfromtheInternetusingtheURL

http://www.aspnetsbs.com/Chapter_13/.

Notethatifyouhavedirectorybrowsingdisabled(thedefault),youwill

needtosetadefaultdocumentintheDocumentstaboftheProperties

dialogboxforthedirectory,incasetheuserdoesnotenteradocument

name.Thenameofthedefaultdocumentshouldmatchthenameofa

pagethatexistsinthedirectory.Thiswillallowaccesstocontentusinga

URLthatdoesnotcontainadocumentname,suchasthepreceding

URLs.Otherwise,thenameofthedesiredfilemustbeappended,oran

errorwilloccur.



StoringApplication-SpecificConfigurationSettings

Oneofthechallengesofdeployingmostnon-trivialWebapplicationsin

classicASPisfiguringoutwheretostoreapplication-specific

configurationinformation.ClassicASPallowedforanumberof

approaches,includingusingcustomkeysinthesystemregistry,storing

configurationinformationinaback-enddatabase,readingconfiguration

settingsfromacustomconfigurationfile,andbuildingsettingsinto

components.Eachapproachhasitsownsetofdrawbacksthatmakesit



lessthanideal.

Forexample,storingconfigurationinformationinthesystemregistry

makesthatinformationrelativelysecure(particularlyifproperregistry

securityproceduresarefollowed).

However,addingthatinformationtotheregistryormodifyingitfroma

remotemachinecanbedifficult,ifnotimpossible,becauseofpermission

settingsdesignedtoprotecttheregistryfromremotetampering.

Additionally,accessingsettingswithintheregistrycanbeexpensivefrom

aperformancestandpoint.

Neitherstoringconfigurationinformationinaback-enddatabasenor

storingthatinformationincustomconfigurationfilesprovideseasily

repeatableproceduresformultipleapplications.Thedatabasesolution

requiresthatchangestotheWebapplicationandtheconfiguration

databasebesynchronizedsothatusersalwaysgetthecorrect

information.Thecustomconfigurationfilesolutionrequiresbuildinglogic

intopagesorcomponentstoparseandcachetheapplicationsettings,

whileperiodicallycheckingtoseeifsettingshavebeenmodified,and

loadingthenewsettingsiftheyhavebeen.

Tosimplifythissituation,ASP.NETprovidesaspecialsectionofthe

Web.configandmachine.configfilescalledappSettings,whichallowsyou

tostoreapplication-specificconfigurationsettingsasasetofkey/value

pairs.






Whenyourapplicationstartsup,ASP.NETcachesthevaluesofthe

appSettingssectioninastringcollectioncalled

ConfigurationSettings.AppSettings.Youcanaccessthesesettingsby

passingthedesiredkeytothiscollection,asshownhere:

Label1.Text=ConfigurationSettings.AppSettings["myConfigKey"];

Inadditiontoautomaticallyloadingandcachingthesevaluesforyouat

applicationstart-up,ASP.NETmonitorstheWeb.configfile(s)forchanges

anddynamicallyreloadsthecontentsoftheappSettingsconfiguration

sectionifchangesaredetected.



Thepracticefilesforthischapterincludeanexampleofusingthe

appSettingsconfigurationsectiontostoreanddisplayasimpletextvalue.

SeetheWeb.configfileforanexampleofstoringthevalue;seethe

AppSettings.aspxfileforanexampleofretrievinganddisplayingthe

value.

Important WhileappSettingsprovidesastoragelocationfor

application-specificconfigurationsettingsthatisalmost

idealinitseaseofuseandperformance,notethatsince

thevaluesarestoredasplaintextinafileintheWeb

space,somesecurityriskisassociatedwiththis

approach.

IfavulnerabilityintheWebserverallowedaccesstothe

Web.configfile,anysensitiveinformationstoredin

appSettingscouldbecompromised.Forthisreason,

neverstoresensitiveinformation,suchasusernames,

passwords,orcreditcardoraccountnumbers,inthe

appSettingssectionofWeb.config.Foritemsofmedium

sensitivity,onesolutionistoaddtheitemstothe

appSettingsofthemachine.configfile,whichisnot

locatedwithintheWebspaceandthusharderto

compromise.Keepinmind,however,thattheappSettings

storedinMachine.configareavailabletoeveryapplication

ontheserver,unlesstheyarelockeddownusingthe

tag.(SeeChapter5formoreinformationon

lockingdownconfigurationsettings.)

Formoreinformationonstoringsensitiveinformationin

Webapplications,seetheStoringSecretssectionofthe

followingsecurityarticleontheMSDNWebsite:

http://msdn.microsoft.com/library/enus/dnnetsec/html/SecNetch08.asp.



DeployingaWebApplicationManually

IfyouredevelopingASP.NETapplicationsagainsttheMicrosoft.NET

FrameworkwithoutthebenefitofVisualStudio.NET,most(ifnotall)of

yourdeploymentworkwillbemanual.ThegreatnewsisthatASP.NET

applicationsaresimpletodeploy.EvenifyouareusingVisualStudio

.NET,youmightneedtoorwanttodeployyourVisualStudio.NET

ASP.NETapplicationmanually.



SettingUptheTargetDeploymentDirectory

TosuccessfullydeployanASP.NETapplication,thefirstthingyouneedis

atargetdirectoryontheserverthatwillhosttheapplication.Youmustset

upthisdirectoryinIISasanapplicationrootifyouwanttousethe

automaticloadingofassembliesinthebinsubdirectoryorusea

Global.asaxfileifyouwanttohavearootWeb.configfileforthe

application.(Youcantsetcertainconfigurationsectionsbelowthe

applicationlevel,andtheywillcauseerrorsifusedinaWeb.configfile

thatresidesinasubdirectory.)



CopyingFilestotheTargetDirectory

OnceyouvesetupthetargetdirectoryinIIS(mappedtoaphysicalfolder

onthehostmachine),yourereadytocopyallthenecessaryfilestothe

targetdirectory.Youcandothisbyusinganynumberofcommonly

availabletools,fromtheXCOPYcommandtoWebDAV.Youcaneven

drag-and-dropfilestoandfromanetworkshareusingMicorosoft

WindowsExplorer.

Ifyoureusingcode-behindinyourWebFormspagesorusercontrols,

youllneedtodecidewhethertodeploythecode-behindclassfiles

containingyoursourcecodetothetargetserver.IfyoureusingtheSrc

attributeofthe@Pageor@ControldirectivetohaveASP.NET

dynamicallycompileyourcode-behindclasses,youhavetodeploythe

classfiles,ortheapplicationwillnotfunction.Ifyouwanttoavoid

deployingthecode-behindclassfiles,youcanusetheInheritsattributeof

the@Pageor@ControldirectiveinsteadoftheSrcattribute.Usingthe



Inheritsattributerequiresyoutomanuallycompileyourcode-behind

classesandputtheresultingassembliesintheapplicationsbin

subdirectory.ProjectscreatedwithVisualStudio.NETwillbecreated

usingtheInheritsattributeofthe@Pageor@Controldirective.Asa

result,youmustbuildtheprojectbeforedeployingit,whetheryoudeploy

theprojectmanuallyorbyusingVisualStudiosdeploymenttools.



DeployingContent

Deployingstaticcontent(suchasHTMLpagesandimages),WebForms

pages,usercontrols,andcode-behindclassesisassimpleascopying

themfromthedevelopmentapplicationdirectorytothedeploymenttarget

directory.Forexample,youwouldusethefollowingcommandtodeploy

contentfromalocaldirectoryC:\inetpub\wwwroot\myDevApptoaremote

directoryforwhichamappeddrive,X:,hasbeencreated:

xcopyc:\inetpub\wwwroot\ASPNetApp1x:\/E/K/O

TheparameterspassedtoXCOPYareasfollows:

/Especifiesthatallsubdirectoriesaretobecopied,whether

emptyornot.

/Kspecifiesthatfileanddirectoryattributesonthedestination

shouldbesettomatchthesource.

/OspecifiesthatownershipandACLinformationshouldbe

copiedtothedestination.Thisisusefulwhenusingdomain

accountstosetACLs.

Youcanfindoutaboutallofthecommand-lineparametersavailablewith

XCOPYbyexecutingXCOPY/?.



DeployingAssemblies

Oneoftheareasofgreatimprovementindeploymentis.NETmanaged

assembliesoverCOMcomponents.WithclassicASPapplicationsthat

usedcustomCOMcomponents,notonlydidyouneedtocopythe

componentfiletothemachineonwhichtheapplicationwasbeing



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 13: Deploying an ASP.NET Application

Tải bản đầy đủ ngay(0 tr)

×
x