« Silverlight 3, databinding and anonymous typesCode Camp Montreal - Conference »

Silverlight Configuration (almost) like regular .NET

09/17/09

Permalink 10:31:47 pm, Categories: Welcome

Silverlight is about cutting off fat from .NET. However sometimes, I think it cuts where it shouldn't, leading to awkward voids. As mentioned in a previous post, bound commands are probably the most visible void. The way I discuss to work around the problem works perfectly however, so much that I truly think it's a shame that controls still don't support bound commands out of the box after 3 versions. By the way, please go here to vote for commands in the next version. While you're at it, wsHttpBinding would be great as well ;-)

Today, I discuss such harshly missing parts, and again, propose a nice implementation of something close enough to be used almost normally, the client-side configuration file. Most of you may have noticed that there's no ConfigurationManager class in Silverlight. Point of fact, there's no System.Configuration.dll assembly altogether. You cannot do something like this:

MyConfig config = (MyConfig)ConfigurationManager.GetSection("myConfig");

Moreover, there's no upfront "configuration file" in a Silverlight application. That being said, it's easy to add random text file to the .xap, since it's nothing else than a zip file (try renaming the extension, and open it as any other zip archive. While you're at it, try the same with .docx files...). The trick is to compile the file as "Content". Right-click the file in the solution explorer, Properties, and then Build Action = Content. The file will be put in the .xap file. Here's an example of a Silverlight configuration file:

BuildAction = Content

Then you create DTO (Data Transfer Objects) to hold your section's data:

        [XmlType("mything")]
        [XmlRoot("mything")]
        public class Mything
        {
            public Mything()
            {
                this.Subthings = new List<Subthing>();
            }

            [XmlElement("subthing")]
            public List<Subthing> Subthings { get; set; }
        }

        [XmlType("subthing")]
        public class Subthing
        {
            [XmlAttribute("name")]
            public string Name { get; set; }
        }

Then, you use the new ConfigurationManager class to get your section:
Calling GetSection on the new ConfigurationManager class

All that mimics as much as possible the ConfigurationManager's behavior from .NET. It does add strongly-typed return values though. But keep in mind that this is all fake: the configuration file as shown above is nothing more than an XML file, and won't obey to the rules of a "real" configuration file. For example, I don't even have the mandatory <configSections> tag.

The new ConfigurationManager class I've added to Foundation has two methods: the one you see above, plus an extra overload that takes a second type param of type IConfigurationSectionHandler<TSection> (again, this has been added to Foundation and is not part of Silverlight's code base). That allows you to more control over what's going on, and resembles .NET configSection's "type" attribute. The overload I use in this example uses an XmlSerializer behind the scene (thus the xml attributes on the DTO classes).

Download the code here:
http://omni-foundation.googlecode.com/svn/foundation/trunk

Enjoy!

2 comments

Comment from: Dave [Member] Email
I'm currently improving ConfigurationManager so that it mimics even more precisely what .NET's ConfigurationManager does. I'll post changes to it here later today.
09/18/09 @ 10:22
Comment from: Dave [Member] Email
As I said, I committed the changes so that the ConfigurationManager now works a bit more like its .NET counterpart. For example, the signature is now the same: object GetSection(string sectionName);

Plus, the configuration file now asks for a valid configuration section:

<configuration>
<configSections>
<section name="mySection" type="assembly-qualified name for an IConfigurationSectionHandler" />
</configSections>
...
<mySection value="abc" />
</configuration>
06/02/10 @ 10:55

This post has 2 feedbacks awaiting moderation...

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
September 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

Here on this blog you'll find continuous thoughts and information about Omniscient's Foundation framework.

Search

The requested Blog doesn't exist any more!

XML Feeds

b2