Consuming XAML Configuration from a Silverlight Application
Retired Content |
---|
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
The latest Enterprise Library information can be found at the Enterprise Library site. |
On this page: |
---|
Embedding the XAML Configuration in the XAP | Loading the XAML Configuration from the Server - Adding Enterprise Library Configuration to an Already Existing Container, Circumvent the Browser Cache When Downloading XAML Configuration Files |
There are two options for consuming a XAML configuration file from your application. You can either embed it in the XAP file, or download it from a web server.
Embedding the XAML Configuration in the XAP
By default, the Enterprise Library 5.0 Silverlight Integration Pack will look in the main XAP file for a XAML configuration file called configuration.xaml. If it finds that file there embedded as a page or resource, it will attempt to load it when you first call Enterprise Library. This approach gives you almost the same experience as the .NET Framework configuration files on the desktop.
If you want to embed the configuration file in the main XAP, take the following steps:
- Add the configuration.xaml file to your Silverlight application project.
- Right click the configuration.xaml file and select Properties.
- Ensure the build properties are either set to "Page" or "Resource."
Embedding the configuration file in the XAP does have a (considerable) down side. If you wish to make a change to the configuration file, you'll have to rebuild the XAP file.
Loading the XAML Configuration from the Server
It is possible to load the XAML configuration from a different location, for example from the server. This enables you to change your application's configuration without having to redeploy it.
The following code snippet shows how you can download a XAML file called ConfigurationFromServer.xaml from the server. This XAML file exists in the root of the server from which the application's XAP file was downloaded.
EnterpriseLibraryContainer.EnterpriseLibraryConfigurationCompleted += (sender, e) =>
{
if (e.ConfiguredSuccessfully)
{
// Continue application initialization
}
else
{
// todo: handle download or configuration error
}
};
var configUri = new Uri("/ConfigurationFromServer.xaml", UriKind.Relative);
// Begin downloading the XAP file. There is also an overload that allows you
// to specify credentials to be used when downloading the file.
EnterpriseLibraryContainer.ConfigureAsync(configUri);
This sample loads a XAML file from the server, by calling EnterpriseLibraryContainer.ConfigureAsync(configUri). All download operations in Silverlight occur asynchronously, so this also holds true for downloading a XAML file that contains Enterprise Library's configuration. Therefore, you have to attach an event handler to the EnterpriseLibraryConfigurationCompleted event, to continue your application initialization after Enterprise Library is loaded successfully.
If asynchronous configuration fails for some reason, for example because the connection to the server is down or the configuration file could not be found, then it's up to the developer of the application to handle the situation gracefully. Because Enterprise Library is not configured yet, the application cannot start, so you should either try to load a different XAML file or display an error message.
Ed Says: | |
---|---|
If Enterprise Library could not be configured, the Exception Handling Application Block will also not work. So you cannot call the Exception Handling block or log the error using the Logging block, but you should also check in your application_UnhandledException to see if Enterprise Library was configured successfully. |
The following snippet demonstrates how you can configure an Enterprise Library container using a string.
var configDictionary = (IDictionary)XamlReader.Load(stringWithXAMLConfiguration);
var configSource = DictionaryConfigurationSource.FromDictionary(configDictionary);
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
Adding Enterprise Library Configuration to an Already Existing Container
It is also possible to add Enterprise Library configuration to an already existing instance of Unity. The following code sample demonstrates how you can do this:
IUnityContainer container = new UnityContainer();
// .. (Configure the container programmatically)
// Read configuration from a string
var configDictionary = (IDictionary)XamlReader.Load(stringWithXAMLConfiguration);
var configSource = DictionaryConfigurationSource.FromDictionary(configDictionary);
// Configure Unity with the configuration
var containerConfigurator = new UnityContainerConfigurator(container);
EnterpriseLibraryContainer.ConfigureContainer(containerConfigurator,
configSource);
Circumvent the Browser Cache When Downloading XAML Configuration Files
Internally, the EnterpriseLibraryContainer class uses the WebClient class to download XAML configuration files from the server. The WebClient class uses the browser cache when downloading files. While this can improve the startup performance of your application, it can also cause issues if you have modified the configuration file on the server.
If you want to make sure that you always download the latest version of the configuration file, you should employ a cache-busting technique. For example, you can append to the URL a query string parameter that changes for each download request.
Sharon Says: | |
---|---|
In Stock Trader V2, we're appending a version parameter in the query string to each request to download a configuration file. This ensures that when the configuration and this parameter are updated, each download request gets a new URI and therefore will not use a previously cached version of the configuration file. |
Next Topic | Previous Topic | Home
Last built: July 8, 2011