Use linked configuration files in your App.Config and Web.config to clean up that mess

If you are like me, (and the government hasn’t abducted you yet), then you hate those app.config’s that are 2,000 lines long and just messy as hell. What to do….oh what to do….

Well, one thing that is a great little gem of .NET 2.0 and above is the ability to put each of your sections in their own file. Sounds too good to be true, right? I go to the connection strings file, and just see …Connection strings. Not wading through the first 200 lines of crap to find the little section I want to work with.

So, how does one take advantage of this little documented trick? Super easy. The magical configSource attribute.¬† Why isn’t this easily found in the docs, no idea. All I know is that it works.

So, lets say you have your messy web.config and turn it into this:

web.config

<?xml  version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
.......blah blah
</configSections>
<connectionStrings  configSource="connectionStrings.config"  />
</configuration>

connectionStrings.config

<?xml  version="1.0" encoding="utf-8"?>
<connectionStrings>
<add  name="SeekfordDB"  connectionString="mystical,magical connection string here" providerName="System.Data.SqlClient"  />
</connectionStrings>

You can do this for most of the sections. Now your web.config and app.config are nice little street signs to point to the real meat. And you have all your data in a nice easy to read modular fashion.

Happy coding!

Visual Studio 2010. Pin to Source Variable Watch. How freaking cool..

I am just getting used to some of the workflow enhancements in Visual Studio 2010. I was a bit miffed when I had to go to another submenu off of my context menu to do a quick watch on a variable, until….I got annoyed enough to hit the innocuous looking Pin to Source option. It eluded me at first what the hell this option was but then it all came to light.

My watch sitting their waiting for me happily right there in code. Watching my variables never got so damn easy. So, thumbs up to the VS team on this one. Monitoring my loops and data is now a lot more convenient seeing the values right where my eyes are wanting to actually look.

The context option:

Awesome pin to source menu option in VS 2010
Pin to Source in VS 2010 - Context Menu

The result of this option:

Example of what the Pin to Source looks like in VS 2010
Pin to source - Real Example

I hope this new feature helps you as much as it does me.

Happy coding!

Validation ($SCHEMA$): Element ‘html’ is not supported in Visual Studio 2010 asp.net

I upgraded to Visual Studio 2010 Beta and so far it has been a pretty good experience. One thing that bit me was building my project to find I had over 4,000 warnings. Geee. I didn’t think I coded that badly.

Well, it turns out each one was a warning about almost every single one of my aspx pages html tags. So, WTF?

This manifests itself not only as the

Validation ($SCHEMA$): Element ‘html’ is not supported

but all your other tags.

Validation ($SCHEMA$): Element ‘div’ is not supported ,etc.

It turns¬† out the default settings for the validator was, wait for it……NOTHING. Freaking blank.

The resolution was that the validator had no idea what to validate against, so everything was wrong. Simple solution though, just set the validation type in your options box.

Visual Studio 2010 Options Box

Enjoy! and Happy Coding!

How to connect to a WCF service that has a mismatched Identity in code…

I had an issue where my client was connecting to a WCF service that was using certificate authentication, but the certificate was for a different dns name.

i.e. server was foobar.comp and the certificate was for foo.foobar.comp.

This presented an issue. Of course, you can change the Identity tag in the configuration file, but I wanted my program to be a little smarter. I grab the error from the WCF service and switch the Identity on the fly. Mainly I did this for a test application in a load balancing scenario due to the load balancer having a different DNS name than the servers. (Obviously!)

Hopefully you have written your own proxy class with a constructor that takes in the configuration name and an EndPointAddress.

You can then use the following style code to create your proxy with the Identity mismatch taken care of.


 protected override MyProxyClient CreateConnection(string Server, string altIdentity)
        {
            if (string.IsNullOrEmpty(altIdentity))
                altIdentity = Server;

            return new MyProxyClient ("MyServiceBindingConfiguration",
                new EndpointAddress(
                    new Uri(
                        string.Format("net.tcp://{0}:8090/seekford/2.0/SeekfordService",
                    Server)
                    ),
                    EndpointIdentity.CreateDnsIdentity(altIdentity))
                );
        }

We only are using the configuration for the primary settings, such as binding info. The actual server endpoint is being set on the fly with the altIdentity param being used to allow the server to masquerade with a different certificate.

Happy Coding!

How to use underline text on a label in WPF…

It was driving me nuts to do such a simple task as setting the font on the label to Underline in WPF. Well, its not that easy. You can’t just set the TextDecoration on the label since the label is a container control. Yes, you can set text to its Content property, but that just implicitly creates a TextBlock.

So, I wrote a quick helper function to let me set underlined text to any label.

Simple usage:

lblServer.Content = CreateUnderlinedTextBlock(  "Your Text Here" );

The actual function

        /// Creates the underlined text block.
        private TextBlock CreateUnderlinedTextBlock(string text)
        {
            // Create an underline text decoration. Default is underline.
            TextDecoration myUnderline = new TextDecoration();

            // Create a linear gradient pen for the text decoration.
            Pen myPen = new Pen();           
            myUnderline.Pen  = new Pen(Brushes.Gray, 1);
            myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;

            // Set the underline decoration to a TextDecorationCollection and add it to the text block.
            TextDecorationCollection myCollection = new TextDecorationCollection();
            myCollection.Add(myUnderline);

            TextBlock blockHead = new TextBlock();
            blockHead.TextDecorations = myCollection;
            blockHead.Text = text;
            return blockHead;
        }

I hope this helps!

Happy coding!