Question

I have several components that launch

ObjectFactory.AssertConfigurationIsValid()

on startup validating one single registry. The problem is - it takes 20 seconds each time to complete this task. I added the validation to a separate project with integration tests.

There are some services, that cooperate with NServiceBus and require a service references added to the project. In the registry I declare them like that:

For<IAmAService>().LifecycleIs(new NServiceBusThreadLocalStorageLifestyle()).Use(() => new AmAServiceClient());

When I run this test I get an exception like this:

StructureMap.StructureMapException: StructureMap Exception Code: 207

Internal exception while creating Instance '8872f479-f8c2-4cea-85e3-8ecd85537dac' of PluginType MyProject.IAmAService, MyProject, Version=3.2.0.999, Culture=neutral, PublicKeyToken=null. Check the inner exception for more details. ---> System.InvalidOperationException: Could not find default endpoint element that references contract 'MyProject.IAmAService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

I went for help to the structuremap page and decided to use mocks for those services:

private Establish ctx = () =>
{
    ObjectFactory.ResetDefaults();

    var fakeService = fake.an<IAmAService>();

    ObjectFactory.Initialize(x => x.IncludeRegistry<Registry>());
    ObjectFactory.Inject(fakeService);
};

But that didn't work. I still get the same exception. I tried this because I thought it's similar, but HybridLifycycle didn't work for me (still the same exception, so I guess it's not the point).

Some remarks:

  • splitting the registry for every component is not really an option
  • I don't want to add service references to the integration tests project. I need the integration tests to be dependency free

Given all that: what can I do?

Was it helpful?

Solution

A colleague at work suggested a simple test:

  1. We commented out the instance in the tested registry and run the test. It passed.
  2. Then we added a new registration in the registry, the same service, but without the lifecycle method, commented the old one and run the test. Just as in the beginning it failed.
  3. Then we commented the injection in the Establish part of the test, but kept both instances in the tested registry - it failed again.

We assumed that our solution would be to replace the old instance in the registry with the new one, not adding a new one.

private Establish ctx = () =>
{
    ObjectFactory.ResetDefaults();

    var fakeService = fake.an<IAmAService>();

    ObjectFactory.Initialize(x => x.IncludeRegistry<Registry>());
    ObjectFactory.EjectAllInstancesOf<IAmAService>();
    ObjectFactory.Inject(fakeService);
};

It worked.

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