我是 Nhibernate 的新手,我正在尝试修复我们项目中出现的警告,但我无法理解异常是从哪个方法或类引发的。
WARN - Unable to construct instance of specified SQLExceptionConverter System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance(Type type)
at NHibernate.Exceptions.SQLExceptionConverterFactory.ConstructConverter(String converterClassName, IViolatedConstraintNameExtracter violatedConstraintNameExtracter)
我在 app.config 文件中找到了以下配置,并试图在那里放置断点并在该类中进行调试,但它甚至没有到达那里
<property name="sql_exception_converter">OdbcExceptionConverter, Solution.Folder.ProjectName.Project</property>
有什么关于如何调查这个的建议吗?
ODBC转换器类:
public class OdbcExceptionConverter : ISQLExceptionConverter
{
public Exception Convert(AdoExceptionContextInfo adoExceptionContextInfo)
{
var odbcException = ADOExceptionHelper.ExtractDbException(adoExceptionContextInfo.SqlException) as OdbcException;
if (odbcException == null)
{
return SQLStateConverter.HandledNonSpecificException(adoExceptionContextInfo.SqlException, adoExceptionContextInfo.Message, adoExceptionContextInfo.Sql);
}
if (odbcException.Errors.Any())
{
foreach (var error in odbcException.Errors)
{
switch (((OdbcError)error).SQLState)
{
case "23000":
return new ConstraintViolationException(adoExceptionContextInfo.Message, odbcException, adoExceptionContextInfo.Sql, null);
}
}
}
return SQLStateConverter.HandledNonSpecificException(adoExceptionContextInfo.SqlException, adoExceptionContextInfo.Message, adoExceptionContextInfo.Sql);
}
}
System.MissingMethodException:没有为此对象定义无参数构造函数。
所以只需将默认公共构造函数添加到您的转换器类。像这样的东西:
public class OdbcExceptionConverter : ISQLExceptionConverter
{
//...
public OdbcExceptionConverter() //<-- Added default parametless constructor
{}
//...
}
如果它不起作用,您可以通过设置自定义激活器来进一步调试它。像这样的东西:
//Somewhere before session factory is created:
NHibernate.Cfg.Environment.ObjectsFactory = new DebugActivatorFactory();
public class DebugActivatorFactory : ActivatorObjectsFactory, IObjectsFactory
{
object IObjectsFactory.CreateInstance(System.Type type)
{
try
{
return base.CreateInstance(type);
}
catch (Exception e)
{
throw new ApplicationException("Failed to create instance of " + type.AssemblyQualifiedName);
}
}
}
也许你有多个
OdbcExceptionConverter
类,而你修改了错误的一个。我看不出为什么有可用公共 ctor 的其他公共课会抛出这样的异常。