The easiest way is to use a factory instead as the ASP.NET Core IoC Container doesn't support named dependencies or use a 3rd party IoC container which supports it.
public class FileContainerFactory : IFileContainerFactory
{
private readonly IServiceProvider provider;
public class FileContainerFactory(IServiceProvider provider)
{
this.provider = provider;
}
public IFileContainer CreateFileSystemContainer()
{
// resolve it via built in IoC
return provider.GetService<FsFileContainer>();
}
public IFileContainer CreateFtpContainer()
{
// resolve it via built in IoC
return provider.GetService<FtpFileContainer>();
}
}
Then inject the IFileContainerFactory
into your controller.
An alternative is to mark your interfaces with a marker interface and register/inject these
// It defines no new methods or properties, just inherits it and acts as marker
public interface IFsFileContainer : IFileContainer {}
public interface IFtpFileContainer : IFileContainer {}
public class FsFileContainer : IFsFileContainer
{
...
}
in Startup.cs
services.AddTransient<IFsFileContainer, IFileContainer>();
services.AddTransient<IFtpFileContainer, IFileContainer>();