Bruno Cunha e Silva
Bruno Cunha e Silva

Reputation: 134

.NET Web API 2 Dependency injection - List of dependencies

I have a MS WebApi 2 project that's using Unity as the DependencyResolver.

I'm having a problem to create a factory class that has a dependency of all registered providers for a specific interface, because more providers will arise in the future and the factory should not change just because a new provider is supported.

If my factory is implemented like below, the Unity DependencyResolver fails to instantiate it.

namespace Wintouch.Web.Api.Implementations
{
    public class MyServiceProviderFactory : IServiceProviderFactory
    {
        private IEnumerable<IServiceProvider> _ServiceProviders;

        public MyServiceProviderFactory(IEnumerable<IServiceProvider> serviceProviders)
        {
            this._ServiceProviders = serviceProviders;
        }

        public IServiceProvider GetServiceProvider(string providerKey)
        {
            return this._ServiceProviders.First(e => e.key = providerKey);
        }
    }
}

Is there a way to resolve the list of dependencies without having a dependency for the IDependencyResolver?

Thanks

Upvotes: 2

Views: 360

Answers (1)

cangosta
cangosta

Reputation: 1234

Try injecting the list of services through a params array.

public MyServiceProviderFactory(params IServiceProvider[] serviceProviders)

Upvotes: 2

Related Questions