CODEGATOR

.NET info worth sinking your teeth into!
Welcome to CODEGATOR Sign in | Join | Help
in Search

Martin Cook

Yet another C# developer with a blog.

WPF / Visual Studio 2008 Unit Test Workaround

In my day job I have been closing down my last project and ramping up for a new one. Project transitions don't often leave me with much time to have fun on CODEGATOR. Sad Oh well, I have to pay the bills somehow, right?

One thing I did want to blog about today was a workaround I discovered for a bug that's been perplexing me for awhile. I have been re-creating some of my Windows code for WPF, including all of my customized common dialogs. The dialogs all work great in an application but they fail during automated unit testing in Visual Studio 2008. Specifically, a unit test that calls a method to create and display a Window object fails with the following message:

"An unhandled exception of type 'System.Runtime.InteropServices.InvalidComObjectException' occurred in PresentationCore.dll Additional information: COM object that has been separated from its underlying RCW cannot be used."

 

Here is a quick sample of code that causes this problem: 

class AboutBoxDialog

{

    public bool ShowDialog()

    {

        System.Windows.Window w = new System.Windows.Window();

        w.ShowDialog();

    }

}

Nothing fancy, just some code to display a WPF window. Now, here is the code for my unit test:

[TestClass()]

public class AboutBoxDialogTest

{

    [TestCleanup]

    public void TestCleanup()

    {

        // Required due to a bug in the current release of WPF/VS 2008.

        System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeShutdown();

    }

 

    [TestMethod()]

    public void Test1()

    {

        AboutBoxDialog target = new AboutBoxDialog();

        target.ShowDialog();

    }

}

Now, without the code in the TestCleanup method, this unit test fails after the window is closed. Why? I don't know, but the problem drove me nuts for days before I ran across a solution on Microsoft's website. Here is a link to the original source. I don't know exactly which versions of Visual Studio / WPF are affected by this problem.

 

See ya!

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Martin

I work as a software engineer specializing in designing and building object-oriented business solutions for Windows platforms using C#. I have been programming professionally for roughly 20 years.

This Blog

Syndication

Terms of Service | Privacy Statement