Ninject属性注入不起作用

时间:2017-07-22 00:32:41

标签: c# asp.net-mvc nhibernate ninject

我遇到了问题。我正在研究一个简单的mvc 5应用程序与nhibernate与repo和ninject。我打算使用日志记录功能扩展应用程序,因此我安装了log4net,对其进行了配置并确保其正常工作。到目前为止一切都很好。

然后我决定通过在BaseController中创建HandleError方法来统一表单验证错误处理,BaseController是抽象基本控制器,系统中的所有其他控制器都从该控制器继承,以确保我可以在调试模式I属性中记录验证消息使用log4net将ILoggingService接口注入LoggingService类。不幸的是,该属性未被注入,并且在尝试在处理方法中使用其方法时,我得到一个空引用异常。

下面是代码:

BaseController:

public abstract class BaseController : Controller
{
    [Inject]
    public IUnitOfWork UnitOfWork { get; set; }

    [Inject]
    protected ILoggingService LoggingService { get; set; }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!filterContext.IsChildAction)
        {
            UnitOfWork.BeginTransaction();
        }
    }

    protected override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if (!filterContext.IsChildAction)
        {
            UnitOfWork.Commit();
        }
    }

    protected void HandleResponse(PageResponseCode pageResponseCode, ValidationResponseCode validationResponseCode, string message)
    {
        ViewBag.Result = pageResponseCode;

        SetValidationMessage(validationResponseCode);

        ViewBag.ErrorMessage = message;
    }

    protected void HandleResponse(PageResponseCode pageResponseCode, ValidationResponseCode validationResponseCode)
    {
        ViewBag.Result = pageResponseCode;

        SetValidationMessage(validationResponseCode);
    }

    private void SetValidationMessage(ValidationResponseCode validationResponseCode)
    {
        switch (validationResponseCode)
        {
            case ValidationResponseCode.CredentialsInvalid:
                LoggingService.Debug("Login failed. Username or Password provided was invalid");
                ViewBag.ErrorMessage("Credentials provided invalid. Please review and try again.");
                break;
            case ValidationResponseCode.FormInvalid:
                ViewBag.ErrorMessage = "Form is not valid. Please review and try again.";
                break;
        }
    }
}

内核绑定:

        kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
        kernel.Bind<ILoggingService>().To<LoggingService>().InRequestScope();

        kernel.Bind(x => x.FromAssembliesMatching("*")
            .SelectAllClasses()
            .Excluding<UnitOfWork>()
            .Excluding<LoggingService>()
            .BindDefaultInterface());

LoggingService:

public class LoggingService : ILoggingService
{
    public LoggingService()
    {
        _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    }

    public void Debug(string message, Exception ex)
    {
        _log.Debug(message,ex);
    }

    public void Debug(string message)
    {
        _log.Debug(message);
    }

    public void Info(string message, Exception ex)
    {
        _log.Info(message,ex);
    }

    public void Info(string message)
    {
        _log.Info(message);
    }

    public void Warn(string message, Exception ex)
    {
        _log.Warn(message,ex);
    }

    public void Warn(string message)
    {
        _log.Warn(message);
    }

    public void Error(string message, Exception ex)
    {
        _log.Error(message,ex);
    }

    public void Error(string message)
    {
        _log.Error(message);
    }

    private readonly ILog _log;
}

我做错了什么?

谢谢,

0 个答案:

没有答案