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.