I have been chasing this for quite some time now, about 4 days on and off. But now I have nailed it, thanks to Christer Cederborg – my Unity beacon in the dependency injection mist.
OK – the problem is as follows; I have an object factory that encapsulates the calls to Unity for registration and resolving. This is done because I want to control if the resolved objects should be wrapped with PolicyInjection or not.
However I ran into problem when I created test for my code. In the top most layer an resolved started to fail for the layers beneath them. I started to get an error which basically said:
Ambiguous match found. (Strategy type Instance Interception Strategy…
As often, I didn’t understand the error message and started to chase down the wrong things. I first thought that this had to do with the fact that I was mocking (with Moq) the object that I was resolving. And since Moq creates a type on the fly I could not possible create a policy match for it.
But that was not it. In fact it was simple and of course my own doing.
My ObjektFactory is a singleton – I want to resolve against the same container. But the reference to the UnityContainer was done inline right where variable was declared. Ugly and it jumped up and bit me…
OK – the solution was to have a Init-method that creates a new UnityContainer. And to call it in the test-setup (before each test) and in the application before the ObjektFactory is configured.
Easy – when you looked at the right place.
Published byon Last updated