Reputation: 7508
Background: I have a helper class that I use to set the policies for the ExceptionManager
. In this class I want to inject various implementations of the IExceptionHandler
interface and I want to control this via a configuration file.
So this is how things look: Helper class and interface:
public class ErrorHelper: IErrorHelper
{
private static IExceptionHandler _exceptionHandler;
public ErrorHelper(IExceptionHandler exceptionHandler)
{
_exceptionHandler = exceptionHandler;
}
public IList<ExeptionPolicyDefinition> GetPolicies()
{
//Do stuff here and return policies
//This is the place where _exceptionHandler is used
}
}
public interface IErrorHelper
{
IList<ExeptionPolicyDefinition> GetPolicies();
}
IExceptionHandler implementation:
public class MyExceptionHandler: IExceptionHandler
{
public MyExceptionHandler()
{
//Do some stuff here
}
public Exception HandleException(Exception exp, Guid iId)
{
//Handle exception and log
}
}
The unity bootstrap class:
public class UnityBootstrap
{
private static IUnityContainer _unityContainer;
public static void RegisterTypes(IUnityContainer container)
{
_unityContainer = container;
var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(_unityContainer);
}
public static void SetPolicies()
{
var helper = _unityContainer.Resolve<IErrorHelper>();
//Set ExceptionManager and ExceptionPolicy
}
}
Unity configuration file
<?xml version="1.0" encoding="utf-8"?>
<unity xmlns="http://schemas/microsoft.com/practices/2010/unity">
<alias alias="IExceptionHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.IExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<alias alias="MyExceptionHandler" type="TEST.Shared.MyExceptionHandler, Test.Shared"/>
<alias alias="ErrorHelper" type="TEST.Helpers.Errorhelper, TEST.Helpers"/>
<alias alias="IErrorHelper" type="TEST.Helpers.IErrorhelper, TEST.Helpers"/>
<container>
<register type="IExceptionHandler" mapTo="MyExceptionHandler"/>
<register type="IErrorHelper" mapTo="ErrorHelper">
<constructor>
<param name="exceptionHandler" type="MyExceptionHandler">
<dependency type="MyExceptionHandler"/>
</param>
</constructor>
</register>
</container>
</unity>
So, after a lot of writing and formatting, this is a simplification of what I have. The problem is that when I call RegisterTypes
I get the error in the title, stating that there is no constructor for ErrorHelper
that accepts parameters of name exceptionHandler
when clearly the parameter name in the constructor is exceptionHandler
.
If anyone can point to where I'm going wrong with this please do.
PS1: Sorry for the long question
PS2: I'm pretty new to DI and Unity
Upvotes: 1
Views: 2379
Reputation: 8785
The type of the parameter in the constructor is IExceptionHandler not MyExceptionHandler.
Try: <param name="exceptionHandler" type="IExceptionHandler">
Upvotes: 2