C# Unity配置错误-类型没有接受参数的构造函数
背景:我有一个helper类,用于设置C# Unity配置错误-类型没有接受参数的构造函数,c#,.net,unity-container,C#,.net,Unity Container,背景:我有一个helper类,用于设置异常管理器的策略。在这个类中,我希望注入IExceptionHandler接口的各种实现,并希望通过一个配置文件来控制它 事情是这样的: 助手类和接口: public class ErrorHelper: IErrorHelper { private static IExceptionHandler _exceptionHandler; public ErrorHelper(IExceptionHandler exceptionHandler
异常管理器
的策略。在这个类中,我希望注入IExceptionHandler
接口的各种实现,并希望通过一个配置文件来控制它
事情是这样的:
助手类和接口:
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();
}
public class MyExceptionHandler: IExceptionHandler
{
public MyExceptionHandler()
{
//Do some stuff here
}
public Exception HandleException(Exception exp, Guid iId)
{
//Handle exception and log
}
}
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引导类:
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();
}
public class MyExceptionHandler: IExceptionHandler
{
public MyExceptionHandler()
{
//Do some stuff here
}
public Exception HandleException(Exception exp, Guid iId)
{
//Handle exception and log
}
}
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
}
}
公共类UnityBootstrap
{
私有静态IUnityContainer\u unityContainer;
公共静态无效注册表类型(IUnityContainer容器)
{
_unityContainer=容器;
var section=(UnityConfigurationSection)ConfigurationManager.GetSection(“unity”);
配置(_unityContainer);
}
公共静态无效设置策略()
{
var helper=_unityContainer.Resolve();
//设置例外管理器和例外策略
}
}
统一配置文件
<?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
没有接受nameexceptionHandler
参数的构造函数,而构造函数中的参数名显然是exceptionHandler
如果有人能指出我在这方面的错误,请指出
PS1:很抱歉问了这么长的问题
PS2:我对DI和Unity非常陌生,构造函数中的参数类型是IEExceptionHandler而不是MyExceptionHandler
试试看:
好吧,现在,我真不敢相信它竟然如此明显地盯着我的脸。谢谢,成功了。