Ken Brubaker

The ClavèCoder

<October 2008>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678


Navigation

Subscriptions

News

Kenneth Brubaker
Senior Application Architect

Locations of visitors to this page

Post Categories



How to create AppDomain instances for Team Test Unit Tests

I'm creating an application service that manages multiple AppDomains for worker threads. You have to do this in just the right way to get it to work with Visual Studio Team Edition unit tests.

Summary:

Background:

When you manually create an AppDomain using AppDomain.CreateInstance the defaults for settings are the executable's values. This is fine for a normal application. However, for unit tests, your code is running in an AppDomain other than the executable's domain, so that all the DLLs etc. can be loaded (NUnit works the same way). When you create instances in the new AppDomain, the type resolver is suddenly looking in the folder of the unit test harness executable, rather than in your test assembly's folder. You fix the problem by creating an AppDomainSetup instance and setting the ApplicationBase path to your own AppDomain's value. If that AppDomain is going to access your configuration file, be sure to also set the ConfigurationFile property to your AppDomain's value, also. The example I saw on the Team Test forum also set the AppDomain's Evidence to match the current AppDomain's also.

 

Example:

    AppDomain currentDomain = AppDomain.CurrentDomain;

    AppDomainSetup currentInfo = currentDomain.SetupInformation;

 

    string fiendlyName = "FriendlyName";

    Evidence securityInfo = new Evidence(currentDomain.Evidence);

 

    AppDomainSetup info = new AppDomainSetup();

    info.ApplicationBase = currentInfo.ApplicationBase;

    info.ConfigurationFile = currentInfo.ConfigurationFile;

 

    AppDomain domain = AppDomain.CreateDomain(fiendlyName, securityInfo, info);

 

References:

posted on Friday, December 14, 2007 4:52 PM by kenbrubaker





Powered by Dot Net Junkies, by Telligent Systems