Jun 28, 2012

Odd Outlook Error

In a recent Pet Project , I was working on an application which used the MS Office libraries, specifically the Outlook ones.

At almost the very start of the code I was trying to run the following  :

       Microsoft.Office.Interop.Outlook.Application app = null;
     app = new Microsoft.Office.Interop.Outlook.Application();

During runtime, the application was always throwing an exception while executing the 2nd line.

Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005.

After trying many options to correct it, I stumbled upon a document which dealt with some similar issues. I tried them and Lo, it worked..

Apparently these errors were triggered because I was running Outlook and the application on different user permission levels (one of them as administrator and the other one as regular user). I changed both to Administrator (since my VS.Net link is modified to open always in admin mode) and it all started working smoothly. Serendipity!!

N.B.: These errors only appear if I already have an Outlook 2010 instance started. If Outlook is not started, the application can run smoothly (it can start an Outlook instance by itself).

Jun 20, 2012

How to work with Pre-processor directives for conditional execution

Recently, we had to use a lot of conditional execution of our codes.
The situation was such:
1. In a ASP.Net MVC 3 Unit test project we had to read some variables from the static application bag.

2. Even though we cud inject into and read from the Session Bag, we couldn’t find a easy way for the Application.

3. Using a Mock framework would have been the right solution, but we didn’t have the time to introduce a mocking framework into the solution.

4. From our experience of using the Pre-processor directive, “# if DEBUG” earlier, we thought of  depending on the other BUILD CONFIGURATIONS for the same effect. Such that when we run the UNIT TESTS, with a TEST Config, it should return back a fixed value instead of trying to read from the Application Bag.

5. It wasn’t easy, not much help online either, most advocated using the #define statement at the beginning of the class, which was impractical, since it would require recompile to shift between the different configurations.

 

Without further ado, lets step through the steps which we took to achieve this.

0. Create a Test Solution.

    • Add 2 Projects. A Windows Project and a class library.
    • The Windows App Project contains a form, with a Button and a text box.
    • The Class Library, mimics a Utility project which will lookup from Application or other Cache values and will be called from all other layers.
    • Name the Class Library Project, as Common. Add a class Factory, which has a static property, MyProperty, which returns an int. 
      • image
    • On the Button Click event of the Form call the Factory.MyProperty. Display the value in the Text box
      • image

1. Create a new Build Configuration Item for the Unit Tests.

    • Click on the Configuration Manager, from the Menu
      • image
    • From the “Active Solution Configurations” combo, Select New…
      • image 
    • Add a new Config, lets say UNITTEST, From the “Copy Settings From” combo, select appropriate config, lets select Debug
      • image
    • For Both the Projects set the Build Configuration as “UNITTEST”
      • image
    • Open the Property Page for the “Common” Project and click on the “Build” Tab
    • From the “Configurations” combo, select,  “UNITTEST”
    • In the “Conditional Compilation symbols” textbox, enter “UNITTEST”
      • image
    • Now add the conditional statement targeting the Build Configurations into the Factory Class
      • image
    • We are done. Now the breakpoint will enter the # if UNITTEST section, when when the app is run with “UNITTEST” Build Configuration. Costly exercise of writing Mocking framework averted!
    • ***Remember, the daily build must not use this configuration, while pushing the binaries to the test environments!