Configuring WCF services programmatically for Silverlight

So, like always there was this rather strange problem I came across a couple of days ago. For most normal applications I would be more than happy to just configure my WCF services in the web.config/app.config file. The annoying XML for System.ServiceModel has become quite friendly with me. But the situation now was that I was working on a silverlight application and I had to modify the binding dynamically. Some crazy requirements require my Silverlight application to start using a different service endpoint based on some internal events. The first thing I had to do was shoot out the idea of having the nice friendly config file, delete it off my project and create a code to generate an impromptu binding each time the service is requested.

My normal way of creating a binding configuration is to set all the string and buffer lengths to maximum. (Atleast, the application Im working on requires this). One of the important things that need to be set to max value is the ReaderQuota:

[XML config sample]

So I tried to do this programmatically and guess what?! In Silverlight the XmlDictionaryReaderQuotas object that is required to define the ReaderQuota cannot be instantiated. Now, how the heck am I supposed to create it? Not just that, there is not direct way to set the “ReaderQuota” property of the BasicHttpBinding. The only way to set that property is to use Reflection!

First of all creating a decent configuration file in Silverlight is a pain and on top of that you can even set the values programmaticaly. Were the creators of Silverlight 4.0 drunk when they designed the configuration module?

Anyway, here is how you set up the ReaderQuotas with “MAXIMUM” reader sizes for a WCF binding in Silverlight:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
binding.CloseTimeout = new TimeSpan(00, 05, 00);
binding.OpenTimeout = new TimeSpan(00, 05, 00);
binding.ReceiveTimeout = new TimeSpan(00, 05, 00);
binding.SendTimeout = new TimeSpan(00, 05, 00);
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;             

binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, XmlDictionaryReaderQuotas.Max, null);

Hopefully this will help someone out. In fact it was not that tough to Google this solution out, but the implications of this solution just irked me into writing this post.

Failed to load IIS Metabase

Almost everytime I start a new computer I keep having new types of problems with IIS. Though it’s my second favorite server, I really dislike some of it’s cranky behaviors.

The most common of these uncommon IIS problems I’ve has is : Failed to load IIS Metabase.

The solution to this themed out to be quite simple. Run this command in your visual studio command prompt

aspnet_regiis -i

That’s it! Also make sure that each site you’re hosting has the required privileges to IUSR and you are running the required version of .Net