简体   繁体   中英

Does anyone know if the Autofac implicit relationship type, A needs all the kinds of B, is supported if B is a concrete class?

Does anyone know if the Autofac implicit relationship type, A needs all the kinds of B, is supported if B is a concrete class?

For example, in the constructor in the following code, _circuitBreakerPolicies is not populated with any of the CircuitBreakerPolicy instances I've registered in the Container. Instead, _circuitBreakerPolicies is empty. CircuitBreakerPolicy is a concrete class.

private readonly IEnumerable<CircuitBreakerPolicy> _circuitBreakerPolicies;

public HealthController(
    IEnumerable<CircuitBreakerPolicy> circuitBreakerPolicies)
{
    _circuitBreakerPolicies = circuitBreakerPolicies;
}

However, if I declare an input-parameter of IEnumerable<ISomeInterface> for the constructor above, the input parameter is populated with all registered instances of ISomeInterface which is the expected behaviour. If the constructor input parameter is an IEnumerable of a concrete-class, should Autofac populate the input parameter or not?

An example of how I register the concrete-class is:

builder.Register(
    ctx => Policy.Handle<Exception>().CircuitBreaker
        (
            exceptionsAllowedBeforeBreaking: 3,
            durationOfBreak: TimeSpan.FromMinutes(1))
        ).Keyed<CircuitBreakerPolicy>("testPolicy");

I've read Autofac's documentation on implicit relationship types ( http://docs.autofac.org/en/latest/resolve/relationships.html ) but was unable to find any insight into my question.

Yes, it does. From container perspective it doesn't matter if you resolve abstract or concrete class. But you made a mistake by registering dependency as Keyed<> .

If it was intentional, and If you need to register dependencies as Keyed<> or Named<> you should tell the builder witch key (or name) they should use to resolve dependency. You can even register many objects with the same key, but you must use that key to resolving. The container will resolve and inject all objects with the same key (or name) for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM