Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have a type registered as IServiceA. The type is dependent upon another service IServiceB in its constructor. Both types registered using Autofac something like this:

C#
public static void Configure(ContainerBuilder builder)
{
      builder.RegisterType<ServiceA>().As<IServiceA>();
      builder.RegisterType<ServiceB>().As<IServiceB>();
}



When doing integrationtests we need to inject a fake instead of the real IServiceB. For every test we do, we create a new Lifetime Scope and injects our fakes when this is created. This is the way we do that:

C#
public ILifetimeScope CreateLiftetimeScope()
{
    lifetimeScope = Container.BeginLifetimeScope(p =>
    {
        foreach (var injectionAction in injectionActions)
        {
            injectionAction(p);
        }
    });

    return lifetimeScope;
}


Our problem is that for every time we create a fake for e.g. IServiceB we need to re-register IServiceA, otherwise it will use the old registration of IServiceB. Why is the new IServiceB not used by the old registrations of IServiceA? Are we doing something wrong here?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900