0

I registered my type like this:

container.RegisterType<IUnitOfWork, MainDataContext>(new PerRequestLifetimeManager(), new InjectionMember[] { new InjectionConstructor("") });

and i try to resolve it like this:

_unitOfWork = container.Resolve<IUnitOfWork>(new ParameterOverride("connectionString", currentConnection));

but at a alarming rate (1 every 200) this resolution returns null! retrying to resolve does not work...

Details:
- Problem first detected on Unity version 5.6 but persist on 5.7
- Using .net 4.7 with 4.7 runtime
- If i inspect the container registrations, the one i'm hoping to get is listed there!

Leonardo
  • 10,737
  • 10
  • 62
  • 155
  • 1
    It looks like your using [service locator anti pattern](https://stackoverflow.com/questions/22795459/is-servicelocator-an-anti-pattern), this might be the root cause here. DI manes you should be injecting dependencies, there's no dependency injection in `container.Resolve`, this is just late binding. I'm **guessing** that the lifetime manager is struggling to figure out when this goes out of scope because there is no dependency chain – Liam Mar 01 '18 at 13:57
  • [This answer](https://stackoverflow.com/a/22862854/542251) covers a lot of themes quite well. Constructor injection is the way to go – Liam Mar 01 '18 at 14:01
  • Please show the relevant parts of your composition root so we can see how the DI container is being registered with MVC. – NightOwl888 Mar 01 '18 at 17:44

0 Answers0