1

When Verify() my container I get the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred in SimpleInjector.dll

Additional information: The configuration is invalid. Creating the instance for type ICommunicationApiFacade failed. The constructor of type CommunicationApiFacade contains the parameter with name 'getLogger' and type Func<Object, ILogger> that is not registered. Please ensure Func<Object, ILogger> is registered, or change the constructor of CommunicationApiFacade.

This is my IocConfig class

container.RegisterSingleton<Func<object, ILogger>>(
    x => LogManager.GetLogger(x.GetType().FullName));

container.RegisterSingleton<Func<string, ICommunicationClient>>(
    tenant => new CommunicationClient(
        new Uri(configuration.AppSettings["communication_api." + tenant]),
        new TenantClientConfiguration(tenant)));

container.RegisterSingleton<ICommunicationApiFacade, CommunicationApiFacade>();

This is my CommunicationApiFacade class

internal class CommunicationApiFacade : ICommunicationApiFacade
{
    private readonly ILogger _logger;
    private readonly Func<string, ICommunicationClient> _communicationFactory;

    public CommunicationApiFacade(Func<object, ILogger> getLogger,
        Func<string, ICommunicationClient> communicationFactory)
    {
        _logger = getLogger(this);
        _communicationFactory = communicationFactory;
    }
    ...
}

Interestingly, this logger is being used in other places. By removing any appearance of ILogger from CommunicationApiFacade all works fine.

Steven
  • 166,672
  • 24
  • 332
  • 435
Rober
  • 726
  • 8
  • 27
  • 1
    I copy-pasted your configuration to a Console application, but everything works. Could it be that the `ILogger` abstraction that `CommunicationApiFacade` refers to is a different type than the `ILogger` that your Composition Root refers to? These things usually happen when mixing Common.Logging and log4net. – Steven Mar 01 '17 at 19:31
  • Prevent injecting a factory for the creation of loggers completely. This is leads to needed complexity and the only reason this is done is to implement context based injection. Please take a look at [Context based injection](https://simpleinjector.readthedocs.io/en/latest/advanced.html#context-based-injection) section of the documentation. – Steven Mar 01 '17 at 19:34
  • 1
    You can let Simple Injector report fully qualified type names to make it easier to find these errors. Just set: `container.Options.UseFullyQualifiedTypeNames = true`. – Steven Mar 01 '17 at 19:37
  • That is correct @Steven the referenced abstraction was wrong in `CommunicationApiFacade`, after fixing the imports all worked fine, thanks. – Rober Mar 01 '17 at 19:48
  • Concerning loggers, also take a look at [the following advise](https://stackoverflow.com/questions/5646820/logger-wrapper-best-practice). – Steven Mar 01 '17 at 19:51

1 Answers1

1

I copy-pasted your configuration to a Console application, but everything works. Could it be that the ILogger abstraction that CommunicationApiFacade refers to is a different type than the ILogger that your Composition Root refers to? These things usually happen when mixing Common.Logging and log4net.

Steven
  • 166,672
  • 24
  • 332
  • 435