1

I am new to Unity DI ,And got stuck with an error while resolving the dependency. The error message says "The current type, Data.Core.Repository.ILogger, is an interface and cannot be constructed. Are you missing a type mapping?"

The config and code as below.

CONFIG

 <configSections>
    <section name="unity"
    type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name ="BusinessLogic"/>
    <assembly name ="Data.Core"/>
    <namespace name="Data"/>
    <namespace name="Data.Core"/>
    <namespace name="Data.Core.Implimentation"/>
    <namespace name="Data.Core.Repository"/>
    <namespace name="BusinessLogic" />

    <typeAliases>
      <typeAlias alias="Logger1" type="Data.Core.Implimentation.Logger1,Data.Core.Implimentation" />
      <typeAlias alias="Logger2" type="Data.Core.Implimentation.Logger2,Data.Core.Implimentation" />
      <typeAlias alias="ILogger" type="Data.Core.Repository.ILogger,Data.Core.Repository" />
    </typeAliases>
    <container>
      <register type="ILogger" mapTo="Logger2" name="Loggerxcs" >
     </register>
    </container>

Console App (front end)

    var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
        IUnityContainer container = new UnityContainer().LoadConfiguration(section);


        // Resolving Dependancy  Error is @ here
        LoggerBL _logger = container.Resolve<LoggerBL>();

Business Logic

 // File Logger
    private ILogger _logger;// = new Data.Core.Implimentation.Logger2();

    public LoggerBL(ILogger logger)
    {
        _logger = logger;
    }

    public string LogToFile()
    {
        return _logger.LogToFile();
    }
}

and Its all fine when I tried to resove the depndancy from c# code , using following

         IUnityContainer container = new UnityContainer();
       container.RegisterType<ILogger, Logger2>();

but when I moved the same to config I have got the above error. Thanks in advance.

Everts
  • 10,408
  • 2
  • 34
  • 45
Niyas Kc
  • 21
  • 3
  • Hi @RB. Thank you very much for the update . But I have already loaded the configuration as you can find it on my front end code (posted above) as , " IUnityContainer container = new UnityContainer().LoadConfiguration(section);" – Niyas Kc May 29 '16 at 08:54

1 Answers1

0

The problem is that in the configuration version you have named your mapping (as "Loggerxcs").

However, your LoggerBL takes an unnamed ILogger as its parameter.

Remove

name="Loggerxcs" 

From your configuration and you should be fine.

RB.
  • 36,301
  • 12
  • 91
  • 131