UnitTesting and ArcObjects 10

3486
14
08-31-2011 01:34 AM
YvoWeidmann
New Contributor II
Dear List

I am trying to use UnitTests with the provided framework within VisualStudio 2010 and ArcGIS (ArcEditor) with C#.

The main problem seems the initializing of the ArcObjects. As soon as I am using an ArcObject object in a test method the following exception is thrown:
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.

As first I initialize the ArcObjects environment with a test method defined as [AssemblyInitialize]:
-----------------------------
private static AoInitialize aoInitialize;
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context) {
RuntimeManager.BindLicense(ProductCode.EngineOrDesktop);
aoInitialize = new AoInitializeClass();
aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor);
}
-----------------------------

The license is correctly checked out. But as mentioned, ArcObjects-based objects are throwing the InvalidComObjectException.

I tried several ways of initializing, but till now unsuccessfully.

Does anyone has an idea? I am looking for any hint.

Best regards,
Yvo
0 Kudos
14 Replies
JohnHauck
Occasional Contributor II
I've not done much in the way of tests and haven't looked at this one in particular but wanted to share some things that I have seen in the past related to this. I know with VS 2008 by default Unit Test projects were set to execute in MTA mode. All ArcObjects components are marked as single threaded apartment (STA). STAs are limited to one thread each. So you needed to change the mode to STA. Also setting the runtime configuration manager to x86 may have an impact if you are running on a 64 bit system. Prior to VS 2010 SP1 new test projects would always target .NET 4.0 even if 3.5 was set in the new project dialog. .NET 4.0 won't be officially supported by ESRI until the ArcGIS 10.1 release. I would suggest that you also install SP1 for VS 2010 so you can properly target .NET 3.5.

I hope this helps!
0 Kudos
RichardWatson
Frequent Contributor
I use ArcObjects inside integration tests I have written using Visual Studio 2010 SP1 using the built-in C# unit test framework.  What I do is to initialize ArcObjects as part of the test, i.e. I have not tried any assembly level initialization.

I don't have the code in front of me but I think that I did use an attribute to try and force STA.

Oh, and I am using 9.3.1 but I would not think that that would make a difference (other than having to bind to a version).

If you send me an email then I'll send you a code snippet when I get back to the office next Tuesday.  rwatson at telcoridia dot com.
0 Kudos
MelitaKennedy
Esri Notable Contributor
If you email him, use telcordia, not telcoridia!
0 Kudos
YvoWeidmann
New Contributor II
Hi

That sounds very reasonble:
So you needed to change the mode to STA.

Thank you very much for the hint.

But I was looking for the "switch" to change to STA. Can you give me another hint, how to do it.

Thank you very much,
Yvo
0 Kudos
JohnHauck
Occasional Contributor II
Here is a Microsoft article that talks about how to Run Unit Tests in MTA mode. You would just want to use STA instead.
0 Kudos
RichardWatson
Frequent Contributor
Configure Visual Studio to use MTA.  The tests are run on different threads and ArcObjects do not handle this well.
0 Kudos
BKuiper
Occasional Contributor III
I'm having similar problems with 10.1 (final release) and unit testing of ArcObject code. The license retrieval and usage is causing problems. Anybody found a proper way to initialize the license and keep it active during the tests while running the geoprocessor and arcobjects tools in STA ? I tried different things but haven't found a solution so far. Any tips or tricks would be helpful.
0 Kudos
MannusEtten
New Contributor
so after a while nobody has a solution?

Nobody got a reply from Esri. Inc?
I am struggling with the issue too...
0 Kudos
AlexanderGray
Occasional Contributor III
I use VS2008 but I put the AO license and product initialization in the ClassInitialize method of the test class with the appropriate method decorator.  I have never done it in the Assembly initialize method.
0 Kudos