Unity配置错误-类型没有带参数的构造函数
Unity configuration error - type does not have constructor that takes parameters
背景:我有一个助手 class,我用它来设置 ExceptionManager
的策略。在这个 class 中,我想注入 IExceptionHandler
接口的各种实现,我想通过配置文件来控制它。
事情是这样的:
助手class和界面:
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 实现:
public class MyExceptionHandler: IExceptionHandler
{
public MyExceptionHandler()
{
//Do some stuff here
}
public Exception HandleException(Exception exp, Guid iId)
{
//Handle exception and log
}
}
统一bootstrapclass:
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配置文件
<?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>
所以,经过大量的编写和格式化,这是对我所拥有内容的简化。问题是,当我调用 RegisterTypes
时,我在标题中得到错误,指出 ErrorHelper
没有构造函数接受名称 exceptionHandler
的参数,而构造函数中的参数名称很明显是 exceptionHandler
.
如果有人能指出我哪里出了问题,请指出。
PS1: 很抱歉问题很长
PS2:我是 DI 和 Unity 的新手
构造函数中的参数类型是IExceptionHandler,不是MyExceptionHandler。
尝试:<param name="exceptionHandler" type="IExceptionHandler">
背景:我有一个助手 class,我用它来设置 ExceptionManager
的策略。在这个 class 中,我想注入 IExceptionHandler
接口的各种实现,我想通过配置文件来控制它。
事情是这样的: 助手class和界面:
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 实现:
public class MyExceptionHandler: IExceptionHandler
{
public MyExceptionHandler()
{
//Do some stuff here
}
public Exception HandleException(Exception exp, Guid iId)
{
//Handle exception and log
}
}
统一bootstrapclass:
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配置文件
<?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>
所以,经过大量的编写和格式化,这是对我所拥有内容的简化。问题是,当我调用 RegisterTypes
时,我在标题中得到错误,指出 ErrorHelper
没有构造函数接受名称 exceptionHandler
的参数,而构造函数中的参数名称很明显是 exceptionHandler
.
如果有人能指出我哪里出了问题,请指出。
PS1: 很抱歉问题很长
PS2:我是 DI 和 Unity 的新手
构造函数中的参数类型是IExceptionHandler,不是MyExceptionHandler。
尝试:<param name="exceptionHandler" type="IExceptionHandler">