Can XAML config file be downloaded from server?

Sep 20, 2011 at 2:29 AM

Hi, I had a question just  as the title.

Can XAML config file be downloaded from the server?

I konw it possible to download xaml config file from server in enterprise library 5.0 silverlight Integration Pack.

Coordinator
Sep 20, 2011 at 10:19 AM
Edited Sep 20, 2011 at 11:51 AM

Hi songlei,

The answer is definately yes. Currently we are processing a stream found in a resource uri inside our app. This stream is actualy a loaded to string viriable before it is deserialized to an object using System.Windows.Markup.XamlReader. So one could definately change the code there to load the string from a resource located outside the application.

Original code in Unity.cs file:

public static Unity CreateFromXaml(Uri builderResourceUri) {
    var streamInfo = System.Windows.Application.GetResourceStream(builderResourceUri);

    if ((streamInfo != null) && (streamInfo.Stream != null)) {
        return CreateFromXaml(streamInfo.Stream);
    }

    return null;
}

The only side effect using a web request to get the stream here is that a web request to the server will have to be asynchronous (for silverlight). So you will have to expose a ConfigurationLoaded event. The result would be something like the following (not tested code!!). Append the code below to the Unitiy class of the configuration and make the necessary tweeks to make it work for you.

 

        /// <summary>
        /// Creates a <see cref="Unity"/> from a XAML included as an Application Resource.
        /// </summary>
        /// <param name="remoteResourceUri">Absolute <see cref="Uri"/> that identifies the XAML loacated in the remote server.</param>
        /// <returns>An instance of <see cref="Unity"/> build from the XAML.</returns>
        public static void CreateFromXamlAsync(Uri remoteResourceUri) {
            System.Net.WebClient client = new System.Net.WebClient();
            client.OpenReadCompleted += new System.Net.OpenReadCompletedEventHandler(client_OpenReadCompleted);
            client.OpenReadAsync(remoteResourceUri);
            
        }
 
        private static void client_OpenReadCompleted(object sender, System.Net.OpenReadCompletedEventArgs e) {
            Unity config = null; 
            // get the configuration instance.
            if (!e.Cancelled && e.Error == null && e.Result != null) { 
                config = CreateFromXaml(e.Result);
            }
 
            // trigger the event.
            var handler = ConfigurationLoaded;
            if (handler != null) {
                ConfigurationLoaded(sender, new UnityConfigurationEventArgs() { Config = config });
            }
        }
 
        public static event EventHandler<UnityConfigurationEventArgs> ConfigurationLoaded;
       public class UnityConfigurationEventArgs : EventArgs 
       {
            public Unity Config { getset; }
       }

Hope I helped. Cheers! Constantinos