30.4. Settings

In the .NET Framework v2.0, the concept of settings with a User scope was introduced to allow per-user information to be stored between application sessions. For example, window positioning or theme information might have been stored as a user setting. Unfortunately, there was no way to centrally manage this information. Meanwhile, ASP.NET Application Services had the notion of profile information, which was essentially per-user information, tracked on a server, that could be used by web applications. Naturally, with the introduction of the client application services, it made sense to combine these ideas to allow settings to be saved via the Web. These settings have a scope of User (Web).

As with the membership and role services, you need to enable the profile service for access by the client application services. You do this by adding the <profileService> element to the <system.web.extensions> element in the web.config file.

<system.web.extensions>
         <scripting>
                <webServices>
                        <profileService enabled="true"

readAccessProperties="Nickname"

writeAccessProperties="Nickname" />
                        <authenticationService enabled="true" requireSSL="false"/>

Following the previous examples, we will build a custom profile provider that will use an in-memory dictionary to store user nicknames. Note that this isn't a good way to track profile information, as it would be lost every time the web server recycled and would not scale out to multiple web servers. Nevertheless, you need to add a new class, CustomProfile, to the ApplicationServices project and set it to inherit from ProfileProvider.

Imports System.Configuration

Public Class CustomProfile
    Inherits ProfileProvider

    Private nicknames As New Dictionary(Of String, String)

    Public Overrides Function GetPropertyValues(ByVal context As SettingsContext, _
                                   ByVal collection AsSettingsPropertyCollection) _
                                                 As SettingsPropertyValueCollection
        Dim vals As New SettingsPropertyValueCollection
        For Each setting As SettingsProperty In collection
            Dim value As New SettingsPropertyValue(var)
            If nicknames.ContainsKey(setting.Name) Then
                value.PropertyValue = nicknames.Item(setting.Name)
            End If
            vals.Add(value)
        Next
        Return vals
    End Function

    Public Overrides Sub SetPropertyValues(ByVal context As SettingsContext, _
                               ByVal collection As SettingsPropertyValueCollection)
        For Each setting As SettingsPropertyValue In collection
            nicknames.Item(setting.Name) = setting.PropertyValue.ToString
        Next
    End Sub

    ...
End Class

The difference with the profile service is that when you specify the provider to use in the <system.web> element in the web.config file, you also need to declare what properties can be saved via the profile service (see the following snippet). In order for these properties to be accessible via the client application services, they must have a corresponding entry in the readAccessProperties and writeAccessProperties attributes of the <profileService> element, shown earlier.

<profile enabled="true" defaultProvider="CustomProfile">
    <providers>
        <add name="CustomProfile" type="ApplicationServices.CustomProfile"/>
    </providers>
    <properties>
        <add name="Nickname" type="string"
             readOnly="false" defaultValue="{nickname}"
             serializeAs="String" allowAnonymous="false" />
    </properties>
</profile>

As an aside, the easiest way to build a full profile service is to use the utility aspnet_regsql.exe (typically found at C:WindowsMicrosoft.NETFrameworkv2.0.50727aspnet_regsql.exe) to populate an existing SQL Server database with the appropriate table structure. You can then use the built-in SqlProfileProvider (SqlMembershipProvider and SqlRoleProvider for membership and role providers, respectively) to store and retrieve profile information. To use this provider, change the profile element you added earlier to the following:

<profile enabled="true" defaultProvider="CustomProfile">
    <providers>
        <add name="SqlProvider"
             type="System.Web.Profile.SqlProfileProvider"
             connectionStringName="SqlServices"
             applicationName="SampleApplication"
             description="SqlProfileProvider for SampleApplication" />

Note that the connectionStringName attribute needs to correspond to the name of a SQL Server connection string located in the connectionStrings section of the web.config file.

Returning to the custom profile provider you have created, to use this in the client application you just need to specify the web settings service location on the Services tab of the project properties designer. This location should be the same as for both the role and authentication services, http://localhost:12345/ApplicationServices.

This is where the Visual Studio 2008 support for application settings is particularly useful. If you now go to the Settings tab of the project properties designer and hit the "Load Web Settings" button, you will initially be prompted for credential information, as you need to be a validated user in order to access the profile service. Figure 30-3 shows this dialog with the appropriate credentials supplied.

Figure 30.3. Figure 30-3

After a valid set of credentials is entered, the profile service will be interrogated and a new row added to the settings design surface, as shown in Figure 30-4. Here you can see that the scope of this setting is indeed User (Web) and that the default value, specified in the web.config file, has been retrieved.

Figure 30.4. Figure 30-4

If you take a look at the app.config file for the client application, you will notice that a new sectionGroup has been added to the configSections element. This simply declares the class that will be used to process the custom section that has been added to support the new user settings.

<configSections>
    <sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="ClientServices.My.MySettings" type="System.Configuration
.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser"
requirePermission="false" />
    </sectionGroup>
</configSections>

Toward the end of the app.config file you will see the custom section that has been created. As you would expect, the name of the setting is Nickname and the value corresponds to the default value specified in the web.config file in the ApplicationServices project.

<userSettings>
    <ClientServices.My.MySettings>
        <setting name="Nickname" serializeAs="String">
            <value>{nickname}</value>
        </setting>
    </ClientServices.My.MySettings>
</userSettings>

To make use of this in code you can use the same syntax as for any other setting. Here we simply retrieve the current value, request a new value, and then save this new value:

Private Sub Form1_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles MyBase.Load
    '... Commented out for brevity ...
    MessageBox.Show(My.Settings.Nickname)
    My.Settings.Nickname = InputBox("Please specify a nickname:", "Nickname")
    My.Settings.Save()
End Sub

If you run this application again, the nickname you supplied the first time will be returned.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.245.233