LicenseInitializer does not work under unit testing with MSTest

2393
2
Jump to solution
05-15-2012 02:49 AM
JuhoVainio
Occasional Contributor
I have set up a unit testing class for our ArcMap extension. I need the ArcObjects for generating sample geometry objects etc. And for that I need the LicenseInitializer. Everything works fine for the first test, but the second test always fails at error:
Error: System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used..  at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, ref Boolean pfNeedsRelease) at ESRI.ArcGIS.UCOMIArcGISVersion.GetActiveVersion(ref esriProductCode code, ref String ver, ref String path) at ESRI.ArcGIS.RuntimeManager.get_ActiveRuntime() at UnitTestHelpers.LicenseInitializer.InitializeApplication(esriLicenseProductCode[] productCodes, esriLicenseExtensionCode[] extensionLics) in LicenseInitializer.Designer.cs: line 50


If I run just one test, everything works ok, but it's the second test that causes the error. I also noticed that if I debug the test and pause it for a while, then the error also rises. But if I just run the test without any interruptions, it runs fine.

How should I create and shutdown the initializer properly? Any other suggestions how to make this working?
0 Kudos
1 Solution

Accepted Solutions
JuhoVainio
Occasional Contributor
Yes, I wrote a helper class for making the LicenceInitializer a singleton.

    /// <summary>     /// ArcObject License Helper for initializing the license. Wrapped to this helper class because MSTest always creates a new instance of     /// a test class for EACH TEST that it runs and LicenseInitializer must only be initialized ONCE.     /// </summary>     public static class LicenseHelper     {         private static LicenseInitializer licenseInitializer = null;          /// <summary>         /// Initialize the ArcObjects license. Must be run before any usage of AO objects.         /// </summary>         public static void InitializeLicense()         {             if (licenseInitializer != null)             {                 return;             }              licenseInitializer = new LicenseInitializer();             licenseInitializer.InitializeApplication(new[] {esriLicenseProductCode.esriLicenseProductCodeArcView},                                                      new[] {esriLicenseExtensionCode.esriLicenseExtensionCodeDataInteroperability});         }          /// <summary>         /// Shutdown and release the ArcObjects license. AO objects cannot be handled after this method is called.         /// </summary>         public static void ShutdownApplication()         {             if (licenseInitializer != null)             {                 licenseInitializer.ShutdownApplication();             }         }     }


Now you just put the
LicenseHelper.InitializeLicense();
in the test class' constructor and
LicenseHelper.ShutdownApplication();
to the cleanup method.

View solution in original post

0 Kudos
2 Replies
BKuiper
Occasional Contributor III
Exact the same problem here. Using ArcEngine 10.1 (final release). Did you figure out how to do this ?
0 Kudos
JuhoVainio
Occasional Contributor
Yes, I wrote a helper class for making the LicenceInitializer a singleton.

    /// <summary>     /// ArcObject License Helper for initializing the license. Wrapped to this helper class because MSTest always creates a new instance of     /// a test class for EACH TEST that it runs and LicenseInitializer must only be initialized ONCE.     /// </summary>     public static class LicenseHelper     {         private static LicenseInitializer licenseInitializer = null;          /// <summary>         /// Initialize the ArcObjects license. Must be run before any usage of AO objects.         /// </summary>         public static void InitializeLicense()         {             if (licenseInitializer != null)             {                 return;             }              licenseInitializer = new LicenseInitializer();             licenseInitializer.InitializeApplication(new[] {esriLicenseProductCode.esriLicenseProductCodeArcView},                                                      new[] {esriLicenseExtensionCode.esriLicenseExtensionCodeDataInteroperability});         }          /// <summary>         /// Shutdown and release the ArcObjects license. AO objects cannot be handled after this method is called.         /// </summary>         public static void ShutdownApplication()         {             if (licenseInitializer != null)             {                 licenseInitializer.ShutdownApplication();             }         }     }


Now you just put the
LicenseHelper.InitializeLicense();
in the test class' constructor and
LicenseHelper.ShutdownApplication();
to the cleanup method.
0 Kudos