30.6. Offline Support

In the previous steps, if you had a breakpoint in the role provider code on the server, you may have noticed that it hit the breakpoint only the first time you ran the application. The reason for this is that it is caching the role information offline. If you click the "Advanced . . ." button on the Services tab of the project properties designer, you will see that there are a number of properties that can be adjusted to control this offline behavior, as shown in Figure 30-6.

Figure 30.6. Figure 30-6

It's the role service cache timeout that determines how frequently the server is queried for role information. As this timeout determines the maximum period it will take for role changes to be propagated to a connected client, it is important that you set this property according to how frequently you expect role information to change. Clearly, if the application is running offline, the changes will be retrieved the next time the application goes online (assuming the cache timeout has been exceeded while the application is offline).

Clicking the "Save password hash" checkbox means that the application doesn't have to be online in order for the user to log in. The stored password hash is used only when the application is running in offline mode, in contrast to the role information, for which the cache is queried unless the timeout has been exceeded.

Whether the application is online or offline is a property maintained by the client application services, as it is completely independent of actual network or server availability. Depending on your application, it might be appropriate to link the two as shown in the following example, where offline status is set during application startup or when the network status changes. From the project properties designer, click the "View Application Events" button on the Application tab. This will display a code file in which the following code can be inserted:

Namespace My
    Partial Friend Class MyApplication
        Private Sub MyApplication_Startup(ByVal sender As Object, _
           ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) _
                                                                 Handles Me.Startup
            UpdateConnectivity()
        End Sub

        Private Sub MyApplication_NetworkAvailabilityChanged( _
              ByVal sender As Object, _
              ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) _
                                              Handles Me.NetworkAvailabilityChanged
            UpdateConnectivity()
        End Sub

        Private Sub UpdateConnectivity()
        System.Web.ClientServices.ConnectivityStatus.IsOffline = Not My.Computer
.Network.IsAvailable
        End Sub
    End Class
End Namespace

You should note that this is a very rudimentary way of detecting whether an application is online, and that most applications require more complex logic to determine if it is, in fact, connected or not. The other thing to consider is that when the application comes back online, you may wish to confirm that the user information is still up to date using the RevalidateUser method on the ClientFormsIdentity object (only relevant to Forms authentication):

CType(System.Threading.Thread.CurrentPrincipal.Identity,  _
                                              ClientFormsIdentity).RevalidateUser()

The last property in the Advanced dialog determines where the cached credential and role information is stored. This checkbox has been enabled because we chose to use Windows authentication earlier in the example. If you are using Forms authentication you can clear this checkbox. The client application services will use .clientdata files to store per-user data under the Application.UserAppDataPath, which is usually something like C:UsersNickAppDataRoamingClientServices1.0.0.0 under Windows Vista (slightly different under Windows XP). Using a custom connection string enables you to use a SQL Server Compact Edition (SSCE) database file to store the credentials information. This is required for offline support of Windows authentication.

Unfortunately, the designer is limited in that it doesn't enable you to specify any existing connections you may have. If you modify the app.config file, you can tweak the application to use the same connection.

This might be a blessing in disguise, because the |SQL/CE| datasource property (which is the default) actually lets the client application services manage the creation and setup of the SSCE database file (otherwise you have to ensure that the appropriate tables exist).

You will notice that the files that are created are .spf instead of the usual .sdf file extension — they are still SSCE database files that you can explore with Visual Studio 2008 (note that SQL Server Management Studio will not work with them, as they are SSCE v3.5, which is currently not supported).

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

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