Question

I'm trying to write an end-to-end 'unit' test of my client server system.

In my test, I create an instance of the server and one of the client.

I then invoke the client which calls a WCF service on the server. It works well.

However -- I can see that at least in one case (StructureVault) the exact same instance is reused in both application object hierarchies, which is undesireable, not least because both applications register cache items in the object. Furthermore, lock(this){} wrappings on the client cause deadlock when server tries to access the same during WCF call (which alerted me to the problem).

I've found discussions here on SO and tried the construction below. It did not solve my issue.

I can see that the problematic object is created twice as expected, but the lastly created object is used in both hierarchies.

The GetTestRoot... calls in code below calls ObjectFactory.Initialize to wire up before returning the root of hierarchy. The StructureVault object should be Singleton within each hierarchy.

        For<IStructureVault>()
            .Singleton().Use<StructureVault>();

Any suggestions on what to do/try?!

Code:

        var testRootCalculationServer =
            DbIntegrationTestHelper
                .GetTestRootCalculationServer("2",
                                              UserNameConst.SystemName,
                                              Rap1DEnvironment.LocalDevEnv);
        StructureMap.ObjectFactory.EjectAllInstancesOf<IStructureVault>();
        StructureMap.ObjectFactory.Initialize(x=>{});

        var testRootRap1DGui1 =
            DbIntegrationTestHelper
                .GetTestRootRap1DGui("1",
                                     UserNameConst.SystemName,
                                     Rap1DEnvironment.LocalDevEnv, false);
Was it helpful?

Solution

In the end I found the problem.

In the CalculationServer, created as the first 'test root' above, I'm hosting the WCF service. Whenever a new request is made, a new instance of the WCF service is spawned, making use of the at-that-time-current ObjectFactory. That ObjectFactory was still in the state where it was left when instantiating the second 'test root' above.

Moral: When hosted WCF services spawn a new instance, be aware of what the contents of the StructureMap.ObjectFactory is!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top