i am first time using any DI framework called unity app block. i am getting error. the error is :-
The type name or alias ILogger could not be resolved. Please check your configuration file and verify this type name.
i was trying inject dependency from out side into my main proj. suppose i want to save data to anywhere by dependency. say suppose i want to save data to file or console, database etc.
here i am telling you how i develop my app for incorporating Unity.
first i create a class library project called “Ilogger” it has only one interface. full code of this interface
namespace Ilogger
{
public interface ILog
{
void Write(string msg);
}
}
secondly i create a class library project called “ConsoleWriter” it has only one class which inherit Ilogger interface.so i just add the reference of Ilogger project into ConsoleWriter proj. full code of this ConsoleWriter
namespace ConsoleWriter
{
public class ConsoleWriter : Ilogger.ILog
{
#region ILog Members
public void Write(string msg)
{
Console.WriteLine(msg);
Console.ReadLine();
}
#endregion
}
}
3rd step i create a class library project called “FileWriter
” it has only one class which inherit Ilogger interface.so i just add the reference of Ilogger project into FileWriter proj. full code of this FileWriter
namespace FileWriter
{
public class FileWriter : Ilogger.ILog
{
#region IWriter Members
public void Write(string msg)
{
using (StreamWriter streamWriter = new StreamWriter("c:\\TestUnity.txt", true))
{
streamWriter.WriteLine(msg);
}
}
#endregion
}
}
next i create my win apps from where i inject dependecy at runtime. in this project i add some dll reference of unity block and those are.
Microsoft.Practices.Unity
Microsoft.Practices.Unity.Configuration
system.configuration
i add one app.config file and it has entry like
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias type="ILogger.ILog, ILogger" alias="ILogger" />
<namespace name="ILogger.ILog"/>
<container>
<register mapTo="FileWriter.FileWriter, FileWriter" name="MyFileWriter" type="ILogger"/>
<register mapTo="ConsoleWriter.ConsoleWriter, ConsoleWriter" name="MyConsoleWriter" type="ILogger"/>
</container>
</unity>
</configuration>
here is the main code from where error is thrown
string strCountryCode = “USA”;
IDictionary<string, string> loggers = new Dictionary<string, string>();
loggers.Add("USA", "MyFileWriter");
loggers.Add("GBR", "MyConsoleWriter");
IUnityContainer container = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)System.Configuration.ConfigurationManager.GetSection("unity");
//container.LoadConfiguration();
section.Containers.Default.Configure(container);
Ilogger.ILog logger = container.Resolve<Ilogger.ILog>(loggers[strCountryCode]);
logger.Write("Hello World");
this line giving error section.Containers.Default.Configure(container);
i am using DI framework unity first time so i am not being able to catch what mistake i made. so please anyone help me to get the error and tell me how to fix it.
thanks
there may be several issues;
in the entry point of application, where you are trying to read unity configuration, you need to add the assembly which contains
ILoginterface; the assembly may be missing.you need to add aliases using full paths; the full path needs to include the assembly name.
in order to observe exact problem, instead of using a configuration file, try to make registrations in the code.