| By Brad Abrams | Article Rating: |
|
| April 15, 2009 02:00 AM EDT | Reads: |
43,959 |
Part 2: Save Personalization Settings
In this part I will show how to leverage the ASP.NET profile system to store and retrieve data tied to a particular user. The beautiful thing about this is there is no explicit database configuration required.
First, let's go back to the ASP.NET server project and add the ProfileService.svc. The easiest way to do this might be to copy the AuthenticationService.svc change the file name and then change the contents as show below.
<%@ ServiceHost Language="C#" Service=
"System.Web.ApplicationServices.
ProfileService" %>
Now, we need to enable the service via WCF configuration in web.config. This looks just like the config for the prfofile service. In the system.serviceModel\services section add a new node.
<!-- this enables the WCF
ProfileService endpoint --> <service name=
"System.Web.ApplicationServices.
ProfileService" behaviorConfiguration=
"ProfileServiceTypeBehaviors"> <endpoint contract=
"System.Web.ApplicationServices.
ProfileService" binding="basicHttpBinding"
bindingConfiguration="userHttp" bindingNamespace=
"http://asp.net/
ApplicationServices/v200"/> </service>
and in the system.serviceModel\behaviors\serviceBehaviors add a new node
<behavior name=
"ProfileServiceTypeBehaviors"> <serviceMetadata
httpGetEnabled="true"/> </behavior>
Now we need to configure the profile system. Again, in web.config add a section listing all the profile properties. In the System.web section, add the following node.
<profile> <properties> <add name="Color" type=
"string" defaultValue="Red" /> </properties> </profile>
Now we need to enable profile service to be accessed from the webservice. To do this, add the following node to the system.web.extensions\scripting\webservices section.
<profileService enabled=
"true" readAccessProperties=
"Color" writeAccessProperties=
"Color"/>
Now, let's go to the Silverlight client and add a reference to this service. This is the same as we did authentication service. Add Service Reference, Discover and select the profileService.
Now, we need to add a little UI to the page.xaml in order to give us a way to view and edit the personalized setting.
<StackPanel Orientation=
"Horizontal" HorizontalAlignment=
"Center"> <TextBlock>Favorite Color:
</TextBlock> <TextBox x:Name=
"colorNameBox"
Width="100" Height=
"25"></TextBox> <Button x:Name=
"submitButton"
Width="50" Height="25" Content="submit" Click=
"submitButton_Click">
</Button> </StackPanel>
Now, let's extend loginComplete to retrieve all the profile properties for the user that just logged in. Again, we need to do that asynchronously so we don't block the browser.
void client_LoginCompleted
(object sender,
LoginCompletedEventArgs e) { if (e.Error != null) statusText.Text =
e.Error.ToString(); else { statusText.Text = e.UserState +
" logged In result:" + e.Result; ProfileServiceClient client =
new ProfileServiceClient(); client.GetAllProperties
ForCurrentUserAsync(false); client.GetAllProperties
ForCurrentUserCompleted +=
new EventHandler
<GetAllPropertiesFor
CurrentUserCompletedEventArgs>
(client_GetAllProperties
ForCurrentUserCompleted); } }
When we get the property values back, we just set the LayoutRoot to have that background.
void client_GetAllProperties
ForCurrentUserCompleted
(object sender,
GetAllPropertiesForCurrent
UserCompletedEventArgs e) { if (e.Error == null) { colorNameBox.Text =
e.Result["Color"]; ChangeBackgroundColor
(e.Result["Color"]); } }
private void ChangeBackgroundColor
(string colorName) { SolidColorBrush brush =
new SolidColorBrush(); switch (colorName.ToLower()) { case "black": brush.Color = Colors.Black; break; case "blue": brush.Color = Colors.Blue; break; case "brown": brush.Color = Colors.Brown; break; case "green": brush.Color = Colors.Green; break; case "orange": brush.Color = Colors.Orange; break; case "purple": brush.Color = Colors.Purple; break; case "yellow": brush.Color = Colors.Yellow; break; case "red": brush.Color = Colors.Red; break; case "white": default: brush.Color = Colors.White; break; } LayoutRoot.Background = brush; }
Finally, when the submit button is pressed we need to set the value on the server.. for completeness, I show waiting until the result comes back from the server before setting the background locally.
private void submitButton_Click
(object sender, RoutedEventArgs e) { ProfileServiceClient client =
new ProfileServiceClient(); Dictionary<string, object> properites =
new Dictionary<string,object>(); properites.Add("Color",colorNameBox.Text); client.SetPropertiesForCurrentUserAsync
(properites, false, properites); client.SetProperties
ForCurrentUserCompleted +=
new EventHandler
<SetPropertiesForCurrent
UserCompletedEventArgs>
(client_SetPropertiesFor
CurrentUserCompleted); } void client_SetPropertiesFor
CurrentUserCompleted
(object sender,
SetPropertiesForCurrentUser
CompletedEventArgs e) { Dictionary<string, object>
properites = e.UserState as
Dictionary<string, object>; ChangeBackgroundColor
((string)properites["Color"]); }
The end result looks good! Notice the employee and the manager can each have different values for color.
Next Page - Part 3. Enable Custom UI Based on a User's Role
Published April 15, 2009 Reads 43,959
Copyright © 2009 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Brad Abrams
Brad Abrams is currently the Group Program Manager for the UI Framework and Services team at Microsoft which is responsible for delivering the developer platform that spans both client and web based applications, as well as the common services that are available to all applications. Specific technologies owned by this team include ASP.NET, Atlas and Windows Forms. He was a founding member of both the Common Language Runtime, and .NET Framework teams.
Brad has been designing parts of the .NET Framework since 1998 when he started his framework design career building the BCL (Base Class Library) that ships as a core part of the .NET Framework. He was also the lead editor on the Common Language Specification (CLS), the .NET Framework Design Guidelines, the libraries in the ECMA\ISO CLI Standard, and has been deeply involved with the WinFX and Windows Vista efforts from their beginning.
He co-authored Programming in the .NET Environment, and was editor on .NET Framework Standard Library Annotated Reference Vol 1 and Vol 2 and the Framework Design Guidelines.
![]() |
radixweb 08/21/08 01:44:07 AM EDT | |||
Hey Brad, Great Post..... Thanks... |
||||
![]() |
Nico 06/04/08 06:15:34 PM EDT | |||
This is great! Been looking forward to seeing more AJAX with Silverlight and this is right up my alley. Now that I know how it works... do you think there's a version out there with advanced escaping or is this security sound across the board? |
||||
- Microsoft’s First Step Toward Cloud Computing
- Adobe Flex Developer Earns $100K in New York City
- Jill T. Singer of CIA to Present at Cloud Computing Expo on November 2
- Visual Studio 2010 Is Cloud Friendly
- SplendidCRM for Microsoft Windows Azure
- Microsoft Falls Off Cliff, Keeps on Ticking
- Microsoft to Data-Mine Facebook & Twitter
- Amazon RDS vs. SQL Azure
- Azure Gets its First Commercial ERP App
- Qt DevDays 2009 - Munich
- Installing Geneva Beta 2 on Windows 7
- Binary Serialization and Azure Web Applications
- Yahoo! to Present at 4th International Cloud Computing Expo
- Microsoft’s First Step Toward Cloud Computing
- Social Media on Ulitzer - Strategy Nets New AUM for RIA
- EC Wrong, Wrong, Wrong – and Sloppy to Boot: Intel
- Adobe Flex Developer Earns $100K in New York City
- This Bing Thing Is Working
- Jill T. Singer of CIA to Present at Cloud Computing Expo on November 2
- Visual Studio 2010 Is Cloud Friendly
- SplendidCRM for Microsoft Windows Azure
- Azure on Ulitzer - Microsoft’s Cloud Builder Floats to Cisco: Report
- Governmental Cloud Interoperability on The Microsoft Cloud
- Microsoft Falls Off Cliff, Keeps on Ticking
- Where Are RIA Technologies Headed in 2008?
- The Top 250 Players in the Cloud Computing Ecosystem
- Accessing the ASP.NET Authentication, Profile and Role Service in Silverlight
- Silverlight 2 - Adobe Flex Killer Is on Its Way!
- Building Great AJAX Applications Using ASP.NET
- Spice Up User Experience with Silverlight
- Is the Silverlight Adoption Rate Artificially Inflated?
- Kaazing Announces Support for Silverlight
- Will Google's Android Sink or Swim?
- VS 2008 Builds AJAX-based Web Apps
- Rich Content Rotator for ASP.NET
- Getting Started with Silverlight: Zero to Hero




































