cs state management
TRANSCRIPT
-
7/24/2019 Cs State Management
1/38
StateState
ManagementManagement
-
7/24/2019 Cs State Management
2/38
AgendaAgenda
View stateView state
Application cacheApplication cache
Session stateSession stateProflesProfles
CookiesCookies
-
7/24/2019 Cs State Management
3/38
View StateView State
Mechanism or persisting relativelyMechanism or persisting relativelysmall pieces o data across postbackssmall pieces o data across postbacks
Used by pages and controls to persistUsed by pages and controls to persist
statestateAlso available to you or persisting stateAlso available to you or persisting state
elies on hidden input feldelies on hidden input feld
!""V#$%S&A&$'!""V#$%S&A&$'Accessed through ViewState propertyAccessed through ViewState property
&er(proo) optionally encryptable&er(proo) optionally encryptable
-
7/24/2019 Cs State Management
4/38
eading and %riting Vieweading and %riting ViewStateState// Write the price of an item to view stateViewState["Price"] = price;
// Read the price back followin a postbackdecimal price = !decimal ViewState["Price"];
-
7/24/2019 Cs State Management
5/38
View State and *ata &ypesView State and *ata &ypes
%hat data types can you store in view%hat data types can you store in viewstate+state+
Primitive types !strings, integers, etc-'Primitive types !strings, integers, etc-'
&ypes accompanied by type converters&ypes accompanied by type converters
Seriali.able types !types compatible withSeriali.able types !types compatible with/inary0ormatter'/inary0ormatter'
System-%eb-U#-1os0ormatterSystem-%eb-U#-1os0ormatterperorms seriali.ation andperorms seriali.ation anddeseriali.ationdeseriali.ation
2ptimi.ed or compact storage o strings,2ptimi.ed or compact storage o strings,integers, booleans, arrays, and hashintegers, booleans, arrays, and hash
-
7/24/2019 Cs State Management
6/38
Application CacheApplication Cache
#ntelligent in(memory data store#ntelligent in(memory data store
#tem prioriti.ation and automatic eviction#tem prioriti.ation and automatic eviction
&ime(based e3piration and cache&ime(based e3piration and cachedependenciesdependencies
Cache removal callbacksCache removal callbacks
Application scope !available to allApplication scope !available to all
users'users'Accessed through Cache propertyAccessed through Cache property
Page-Cache ( ASP4Page-Cache ( ASP4
5ttpConte3t-Cache ( 6lobal-asa35ttpConte3t-Cache ( 6lobal-asa3
-
7/24/2019 Cs State Management
7/38
Using the ApplicationUsing the ApplicationCacheCache// Write a #ashtable containin stock prices to the cache// Write a #ashtable containin stock prices to the cache#ashtable stocks = new #ashtable !;stocks$%dd !"%&'(") *+$++m;stocks$%dd !",(-.") +$++m;stocks$%dd !"&S0-") 1+$++m;.ache$,nsert !"Stocks") stocks; $
$ $// 0etch the price of &icrosoft stock#ashtable stocks = !#ashtable .ache["Stocks"];if !stocks 2= n3ll // ,mportant2 decimal msft = !decimal stocks["&S0-"]; $
$ $// Remove the #ashtable from the cache.ache$Remove !"Stocks";
-
7/24/2019 Cs State Management
8/38
Cache-#nsertCache-#nsert
p3blic void ,nsert ! strin ke4) // 5e4 that identifies item ob6ect val3e) // -he item itself .ache7ependenc4 dependencies) // .ache dependencies !if an4 7ate-ime absol3te89piration) // When sho3ld item e9pire !absol3te: -imeSpan slidin89piration) // When sho3ld item e9pire !slidin: .ache,temPriorit4 priorit4) // ,tems priorit4 relative to other items
.ache,temRemoved.allback onRemove.allback // Removal callback deleate;
-
7/24/2019 Cs State Management
9/38
&emporal $3piration&emporal $3piration
.ache$,nsert !"Stocks") stocks) n3ll) 7ate-ime$(ow$%dd&in3tes !
-
7/24/2019 Cs State Management
10/38
Cache *ependenciesCache *ependencies
.ache$,nsert !"Stocks") stocks) new .ache7ependenc4 !Server$&apPath !"Stocks$9ml";
.ache$,nsert !"Stocks") stocks) new Sl.ache7ependenc4 !"Stocks") "Prices";
Expire if and when Stocks.xmlchanges
Expire if and when the"Stocks" database's
"Prices" table changes
-
7/24/2019 Cs State Management
11/38
Application CacheApplication Cache
-
7/24/2019 Cs State Management
12/38
Session StateSession State
ead7write per(user data storeead7write per(user data store
Accessed through Session propertyAccessed through Session property
Page-Session ( ASP4Page-Session ( ASP45ttpApplication-Session ( 6lobal-asa35ttpApplication-Session ( 6lobal-asa3
Provider(based or 8e3ible dataProvider(based or 8e3ible datastoragestorage
#n(process !deault'#n(process !deault'
State server processState server process
S91 ServerS91 Server
Cookied or cookielessCookied or cookieless
-
7/24/2019 Cs State Management
13/38
Using Session StateUsing Session State
// Write a Shoppin.art ob6ect to session stateShoppin.art cart = new Shoppin.art !;Session[".art"] = cart; $ $ $// Read this 3sers Shoppin.art from session state
Shoppin.art cart = !Shoppin.art Session[".art"]; $ $ $// Remove this 3sers Shoppin.art from session stateSession$Remove !".art";
-
7/24/2019 Cs State Management
14/38
#n(Process Session State#n(Process Session State
>2?? Web$confi ??@>confi3ration@ >s4stem$web@ >sessionState mode=",nProc"/@ $$$ >/s4stem$web@>/confi3ration@
Web Server
ASP-:$& Session StateSession state storedinside
ASP.E!'s workerprocess
-
7/24/2019 Cs State Management
15/38
State Server Session StateState Server Session State
>2?? Web$confi ??@>confi3ration@ >s4stem$web@ >sessionState mode="StateServer" state.onnectionStrin="tcpip=A$*
-
7/24/2019 Cs State Management
16/38
S91 Server Session StateS91 Server Session State
>2?? Web$confi ??@>confi3ration@ >s4stem$web@ >sessionState mode="SEFServer" sl.onnectionStrin="server=orion;interated sec3rit4=tr3e"/@ $$$ >/s4stem$web@
>/confi3ration@
Web Server
ASP-:$&
Database
Server
ASPState*atabase
&reated withnstallS(lState.s(l ornstallPersistS(l
$State.s(l
-
7/24/2019 Cs State Management
17/38
Session $ventsSession $vents
Session"Start event signals newSession"Start event signals newsessionsession
Session"$nd event signals end oSession"$nd event signals end osessionsession
Process with handlers in 6lobal-asa3Process with handlers in 6lobal-asa3void SessionGStart !H // .reate a shoppin cart and store it in session state // each time a new session is started Session[".art"] = new Shoppin.art !;
I
void SessionG8nd !H // 7o an4 clean3p here when session endsI
-
7/24/2019 Cs State Management
18/38
Session &ime(2utsSession &ime(2uts
Sessions end when predeterminedSessions end when predeterminedtime period elapses without anytime period elapses without anyre;uests rom session? minutes
&ime(out can be changed in&ime(out can be changed in%eb-confg%eb-confg
>2?? Web$confi ??@
>confi3ration@ >s4stem$web@ >sessionState timeo3t="J+" /@ $$$ >/s4stem$web@>/confi3ration@
-
7/24/2019 Cs State Management
19/38
Profle ServiceProfle Service
Stores per(user dataStores per(user data persistentlypersistently
Strongly typed access !unlike sessionStrongly typed access !unlike sessionstate'state'
2n(demand lookup !unlike session state'2n(demand lookup !unlike session state'
1ong(lived !unlike session state'1ong(lived !unlike session state'
Supports authenticated and anonymousSupports authenticated and anonymous
usersusersAccessed through dynamicallyAccessed through dynamicallycompiled 5ttpProfle/ase derivativescompiled 5ttpProfle/ase derivatives!5ttpProfle'!5ttpProfle'
Provider(based or 8e3ible dataProvider(based or 8e3ible data
-
7/24/2019 Cs State Management
20/38
Profle SchemaProfle Schema
Profles
Profle Data
Stores
S91 Server2ther
*ata Stores
5ttpProfle/ase
5ttpProfle !Autogenerated5ttpProfle/ase(*erivative'
AccessProfleProvider 2ther Providers
Profle Providers
S;lProfleProvider
Access
5ttpProfle !Autogenerated5ttpProfle/ase(*erivative'
-
7/24/2019 Cs State Management
21/38
*efning a Profle*efning a Profle
>confi3ration@ >s4stem$web@ >profile@ >properties@ >add name="Screen(ame" /@ >add name="Posts" t4pe="S4stem$,nt1" defa3ltVal3e="+" /@ >add name="FastPost" t4pe="S4stem$7ate-ime" /@
>/properties@ >/profile@ >/s4stem$web@>/confi3ration@
-
7/24/2019 Cs State Management
22/38
Using a ProfleUsing a Profle
// ,ncrement the c3rrent 3sers post co3ntProfile$Posts = Profile$Posts K *;
// Lpdate the c3rrent 3sers last post dateProfile$FastPost = 7ate-ime$(ow;
-
7/24/2019 Cs State Management
23/38
5ow Profles %ork5ow Profles %ork
p3blic partial class paeGasp9 D S4stem$Web$L,$PaeH $$$ protected %SP$#ttpProfile Profile H et H ret3rn !!%SP$#ttpProfile!this$.onte9t$Profile; I I $$$I
Autogenerated classrepresenting the page
Autogenerated classderivedfrom )ttpPro*le+ase
Pro*le propert,included inautogenerated pageclass
-
7/24/2019 Cs State Management
24/38
Profle 6roupsProfle 6roups
Properties can be groupedProperties can be grouped
@group element defnes groups@group element defnes groups
>profile@ >properties@ >add $$$ /@ $$$ >ro3p name="$$$"@ >add $$$ /@ $$$
>/ro3p@ >/properties@>/profile@
-
7/24/2019 Cs State Management
25/38
*efning a Profle 6roup*efning a Profle 6roup
>confi3ration@ >s4stem$web@ >profile@ >properties@ >add name="Screen(ame" /@ >ro3p name="0or3ms"@ >add name="Posts" t4pe="S4stem$,nt1" defa3ltVal3e="+" /@
>add name="FastPost" t4pe="S4stem$7ate-ime" /@ >/ro3p@ >/properties@ >/profile@ >/s4stem$web@>/confi3ration@
-
7/24/2019 Cs State Management
26/38
Accessing a Profle 6roupAccessing a Profle 6roup
// ,ncrement the c3rrent 3sers post co3ntProfile$0or3ms$Posts = Profile$0or3ms$Posts K *;
// Lpdate the c3rrent 3sers last post dateProfile$0or3ms$FastPost = 7ate-ime$(ow;
-
7/24/2019 Cs State Management
27/38
Custom *ata &ypesCustom *ata &ypes
Profles support base typesProfles support base types
String, #ntB>, #ntD, *ate&ime, *ecimal,String, #ntB>, #ntD, *ate&ime, *ecimal,etc-etc-
Profles also support custom typesProfles also support custom typesUse type attribute to speciy typeUse type attribute to speciy type
Use seriali.eAs attribute to speciyUse seriali.eAs attribute to speciy
seriali.ation modeE /inary, 4ml !deault', orseriali.ation modeE /inary, 4ml !deault', orStringString
seriali.eAs=F/inaryF types must beseriali.eAs=F/inaryF types must beseriali.ableseriali.able
seriali.eAs=FStrin F t es need t eseriali.eAs=FStrin F t es need t e
-
7/24/2019 Cs State Management
28/38
Using a Custom *ata &ypeUsing a Custom *ata &ype
>confi3ration@ >s4stem$web@ >profile@ >properties@ >add name=".art" t4pe="Shoppin.art" serialiMe%s="Ninar4" /@ >/properties@ >/profile@
>/s4stem$web@>/confi3ration@
-
7/24/2019 Cs State Management
29/38
Anonymous User ProflesAnonymous User Profles
/y deault, profles arenE Speciy which profle propertiesE Speciy which profle properties
are available to anonymous usersare available to anonymous users*ata keyed by user anonymous #*s*ata keyed by user anonymous #*s
Profles or AnonymousProfles or Anonymous
-
7/24/2019 Cs State Management
30/38
Profles or AnonymousProfles or AnonymousUsersUsers>confi3ration@ >s4stem$web@ >anon4mo3s,dentification enabled="tr3e" /@ >profile@ >properties@ >add name="Screen(ame" allow%non4mo3s="tr3e"/@
>add name="Posts" t4pe="S4stem$,nt1" defa3ltVal3e="+ /@
>add name="FastPost" t4pe="S4stem$7ate-ime" /@ >/properties@ >/profile@ >/s4stem$web@>/confi3ration@
-
7/24/2019 Cs State Management
31/38
Profle ProvidersProfle Providers
Profle service is provider(basedProfle service is provider(based
/eta G ships with two providers/eta G ships with two providers
AccessProfleProvider !Access'HAccessProfleProvider !Access'H
S;lProfleProvider !S91 Server'S;lProfleProvider !S91 Server'
Use custom providers to add supportUse custom providers to add supportor other data storesor other data stores
H Will be replaced by SQL Express provider in beta 2
Using the S91 ServerUsing the S91 Server
-
7/24/2019 Cs State Management
32/38
Using the S91 ServerUsing the S91 ServerProviderProvider>confi3ration@
>s4stem$web@
>profile defa3ltProvider="%sp(etSlProvider" /@
>/s4stem$web@
>/confi3ration@
-
7/24/2019 Cs State Management
33/38
ProflesProfles
-
7/24/2019 Cs State Management
34/38
CookiesCookies
Mechanism or persisting te3tual dataMechanism or persisting te3tual data
*escribed in 0C >G?I*escribed in 0C >G?I
0or relatively small pieces o data0or relatively small pieces o data
5ttpCookie class encapsulates5ttpCookie class encapsulatescookiescookies
5ttpe;uest-Cookies collection5ttpe;uest-Cookies collection
enables cookies to be read romenables cookies to be read romre;uestsre;uests
5ttpesponse-Cookies collection5ttpesponse-Cookies collection
enables cookies to be written toenables cookies to be written to
-
7/24/2019 Cs State Management
35/38
5ttpCookie Properties5ttpCookie Properties
Name Description
Name Cookie name (e.g., "UserName=Jeffpro")
Value Cookie value (e.g., "UserName=Jeffpro")
Values Collection of cookie values (multivalue cookies only)
Haseys !rue if cookie contains multiple values
omain omain to transmit cookie to
#$pires Cookie%s e$piration &ate an& time
'ecure !rue if cookie soul& only e transmitte& over H!!*'
*at *at to transmit cookie to
-
7/24/2019 Cs State Management
36/38
Creating a CookieCreating a Cookie
#ttp.ookie cookie = new #ttp.ookie !"Lser(ame") "Oeffpro";Response$.ookies$%dd !cookie;
&ookiename
&ookie
value
-
7/24/2019 Cs State Management
37/38
eading a Cookieeading a Cookie
#ttp.ookie cookie = Re3est$.ookies["Lser(ame"];if !cookie 2= n3ll H strin 3sername = cookie$Val3e; // "Oeffpro" $$$I
-
7/24/2019 Cs State Management
38/38
J >??B(>??D Microsot Corporation- All rights reserved-&his presentation is or inormational purposes only- Microsot makes no warranties, e3press or implied, in this summary-