Castle Windsor 3.2 提供了一个很酷的附加功能,即容器中的诊断日志记录。这帮助我将容器日志重定向到用于存储应用程序日志的 log4net 日志文件。
我现在想做的是能够Exception
在注入我的可选属性时实际捕获容器检测到的内容。
在我的具体情况下,当 Castle 尝试执行我的代码以将属性注入类中时引发了Oracle
数据库错误:ORA-28000: the account is locked
Database
BaseController
public class BaseController : Controller
{
/// <summary>
/// Repository interface injected by Castle Windsor IoC container.
/// See <see cref="MyProject.Widgets.CastleWindsor.Facilities.PersistenceFacility.Init()"/> for more information.
/// </summary>
public ILogRepository Database { get; set; }
}
这个Database
属性是null
当我在一个继承自BaseController
. 这一切都是因为温莎城堡“吞下了”例外。用户得到的唯一消息是:Object reference not set to an instance of an object
. 好的,但我想向用户展示真正的异常/原因,即ORA-28000: the account is locked
. 由于前面提到的诊断日志记录,这条消息被 Castle Windsor 记录下来。这很酷,但我希望能够真正捕获catch
块内的异常:
public class SubCatListController : BaseController
{
public ActionResult SubCatList(string subcat)
{
try
{
var sub = Database.GetLogSubCategory(subcat);
}
catch(Exception e) // I'd like to get the real exception from Castle Windsor here...
{
Logger.Error(e.Message, e);
}
}
}
这种情况可以通过属性注入实现吗?