Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
    • Creating Dummy Targets for Configuration Objects

      13 Aug 2008 by Dean / No Comments

      The ConfigurationManager class introduced in .NET 2.0 makes it easy to read application settings from an XML file. I especially like the ability to derive a class from ConfigurationSection to hold custom settings for your application. This MSDN tutorial on creating custom configuration sections can help you get started.

      I used this to make the configuration files for several of our Infovark add-ins, but ran into a snag with our main API library. In order to interoperate with COM, we had to put out Infovark.Api.dll in the GAC.

      This presents a big problem for using *.config files. If your assembly is in the GAC, your configuration file must live in the GAC as well. (By default, configuration files are sidecar files located in the same directory as your *.exe file.) Since the GAC lives in a special place on a Windows machine, it’s difficult to read and write from that location without special permissions. And you can forget about browsing to it using Windows Explorer. This makes it tough for folks to change configuration options, which defeats the whole point of XML-based configuration files.

      It’d be nice if we could load the configuration file from an specific spot on the computer. But while the Configuration object has both Save() and SaveAs() methods, there’s no corresponding Load() method. Huh? According to MSDN, the “right” way to point your application at a different configuration file is to create a whole new app domain with the appropriate settings. Um… sure.

      How about we just hack up a workaround instead?

      Using a dummy target

      You can fool the configuration object into loading settings from whatever .config file you want, if you don’t mind a hack or two. The Configuration object exposes an OpenExeConfiguration() method that takes a string. Despite its name, you don’t have to pass it an .exe file. Any file path will do, as long as the path exists.

      Since my .dll was in the GAC, I didn’t have a target for the OpenExeConfiguration() to use. I could have pointed it at another .dll — or at a .txt file for that matter — but that wouldn’t be very intuitive. Instead, I created a temporary file without an extension in the location I wanted to save the configuration file. Then I can open a Configuration object using the dummy target. Saving the Configuration object will cause it to write a file named “[configurationTarget].config” to the path I specified. You can see the code I used below.

      ///
      1.         /// Loads a .NET configuration file using the specified target.
      2.         /// Since configuration files are normally sidecar files, you
      3.         /// normally provide the path to an .exe or .dll file. Unlike
      4.         /// ConfigurationManager.OpenExeConfiguration(), this method
      5.         /// creates a dummy file without an extension to use as its target
      6.         /// if the target file does not always exist.
      7.         ///
      8.         ///
      9. The path and name of the dummy file used as the target.
      10.         /// A Configuration object
      11.         public Configuration LoadConfiguration(string configurationTarget)
      12.         {
      13.             bool useDummyTarget=false;
      14.             try
      15.             {
      16.                 FileInfo fi = new FileInfo(configurationTarget);
      17.                 if (!fi.Exists)
      18.                 {
      19.                     useDummyTarget = true;
      20.                     using (StreamWriter sw = fi.CreateText())
      21.                     {
      22.                         sw.WriteLine("Hi! This file only exists to make the Microsoft .NET framework happy.");
      23.                         sw.WriteLine("It's important because Infovark can't load its configuration file without it.");
      24.                         sw.WriteLine("(Don't ask. It's a long, long story.)");
      25.                         sw.Flush();
      26.                         sw.Close();
      27.                     }
      28.                 }
      29.  
      30.                 return ConfigurationManager.OpenExeConfiguration(configurationTarget);
      31.             }
      32.             catch(Exception e)
      33.             {
      34.                 throw new ConfigurationErrorsException("Unable to load a configuration file using " + configurationTarget + " as a target. See inner exception for details.", e);
      35.             }
      36.             finally
      37.             {
      38.                 // Clean up our dummy file.
      39.                 if (useDummyTarget) File.Delete(configurationTarget);
      40.             }
      41.         }

      Once I’ve opened the Configuration object, I don’t need the dummy file any more. I delete it to avoid have weird extension-less files hanging around.

      It’s not pretty, but it gets the job done.

      Continue Reading

    • It Doesn’t Get Better Than This

      06 Aug 2008 by Dean / No Comments

      Our latest revision got high marks from our source control tools.

      We are totally 1337.

      Continue Reading

    • Tools: ReSharper 4.0

      31 Jul 2008 by Dean / No Comments

      We just finished our trial period for ReSharper from JetBrains. We’re buying licenses right now. It’s become indispensable to us. It’s that good.

      ReSharper is like pair programming for introverts. It’s like a real-time FxCop, offering refactorings and best practices advice while you type.

      Gordon had used ReSharper in its 2.0 days. I’d heard many positive things about ReSharper, but hadn’t tried it myself. The recently released 4.0 version offers support for C# 3.5, including the var keyword, object and collection initializers, and lambda expressions. Check out the in-depth review by Simon Hart if you want more details. Or just try it yourself.

      Continue Reading

    • How to avoid Visual Studio Help

      28 Jul 2008 by Gordon / 1 Comment

      For what seems like the thirteen-thousandth time, I just accidentally pushed the F1 key while I was writing some code. It’s pretty close to the escape key. I didn’t mean to push it. I guess I just have fat fingers.

      I really, really hate pressing F1 in Visual Studio. Usually, it takes about a minute to display Microsoft’s help documentation thingy, which is impossible to navigate, frequently wrong and and generally not very helpful. This afternoon, the document explorer decided it had to go and update itself, which took about five minutes before it could take it’s usual minute to load the non-relevant, non-help, that I didn’t even want in the first place!

      During this time, Visual Studio was COMPLETELY Unusable. The help dialog blocks the main visual studio  thread – and all attempts to get back to work were greeted with a friendly, informative “This may take several minutes” dialog.

      Time Passes…
      Time Passes…
      Time Passes…

      Arggh! Gord Mad!… And it turns out it’s not just me. This annoys other folks, too!

      Right. That’s it Visual Studio. You’ve made me go through this song and dance for THE LAST TIME!

      For starters, where do we all go for help? To Google, that’s where. So, I added an external tool using the Tools>External Tools Method:

      Adding an External Tool

      I set up my command to point to Firefox, and passed as the arguments:

      http://www.google.com/search?site=&hl=en&q=$(CurText)+c%23&

      (The +c%23& part of the command appends “C#” to whatever is highlighted in the IDE. If you’re not using C#, you could leave it out, or substitute it with whatever else you usually search for)

      Then, I flipped over to the Keyboard bindings screen (Tools > Options > Keyboard:)

      VS 2008 Keybinding

      VS 2008 Keyboard Binding Screen

      And I re-mapped the F1 key to my new ExternalCommand1.

      There! Now, whenever I press F1, Visual Studio opens a new tab on my web browser, and searches Google for whatever I have highlighted in the IDE.

      Purposefully punishing developers with a minute or two wait everytime they press a certain key is just plain unforgivable. They get really distracted trying to work around the “functionality”, and then further distracted writing ranty blog posts about it…

      Continue Reading

    • Enums are Ints that Ain’t

      28 Jul 2008 by Dean / No Comments

      Enumerations are incredibly useful in Microsoft .NET, but they can be odd to work with at times. While researching something to do with the new System.Addin namespace in C# 3.5, I was reminded of some enum craziness I’d forgotten.

      Enumerations are implemented as collection of integer constants. You can cast any integer to an enum type, regardless of whether it’s been defined in the collection. That makes the code below legal, despite the fact that no item in our enum has a value of 55.

      1. public enum Color
      2. {
      3.     Black = 0,    
      4.     Red = 1,
      5.     Green = 2,
      6.     Blue = 3
      7. }
      8.  
      9. Brush.Color = (Color)55;

      You can’t rely on the compiler to enforce legal enum values. This means if you’re using enums in case statements, you ought to include a default statement to catch those cases where an unexpected value gets passed. It’s always a good defensive coding measure, but I’d mistakenly assumed I could skip it in the case of enums. Not anymore.

      [Edit: See this old post from Greg Vaughn for some other examples of Enum wackiness. Weird.]

      Continue Reading

    • Previous
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 16
    • Next
    • Categories

      • .NET (41)
      • AJAX (3)
      • Books (7)
      • HTML (9)
      • Infovark (8)
      • Programming (48)
      • REST (11)
      • SQL (3)
      • Testing (3)
      • Tools (13)
      • UI (3)
      • WCF (11)
      • Web Services (8)
      • WPF (4)
      • XML (4)
    • Archives

    • Get future articles


       

    • Blogroll

      • Ajaxian
      • Anne Van Kesteren
      • Brain.Save()
      • Coding Horror
      • Eric Sink
      • Joel Spolsky
      • John Resig
      • Mark Pilgrim
      • Raymond Chen
      • Scott Hansleman
      • Secret Geek
      • Steve Yegge
      • The Daily WTF
      • The Database Programmer
    • Meta

      • Log in
      • Entries RSS
      • Comments RSS
      • WordPress.org
  • Site map

    • News
    • Product
    • Download
    • Buy
    • Support
    • About
  • Recent Posts

    • Review: Brownfield Application Development in .NET
    • Using Modal Dialogs with a Splash Screen in WPF
    • Highlighting query terms in a WPF TextBlock
    • Getting XAML Hyperlink text to wrap
    • How to format the XAML Hyperlink NavigateUri
  • Twitter

    Copyright 2011 Infovark, Inc. All rights reserved.