Interface Service提供null异常错误

时间:2015-09-28 14:45:17

标签: asp.net-mvc dependency-injection

我创建了一个名为ILoginService的接口和一个实现此服务的类,名为LoginService。我通过此服务传递存储过程,该服务返回标量值。

问题是ILoginService对象返回一个空值,使Object引用未设置为对象的实例错误

请帮助这段代码如下:

LoginService代码

 public partial class LoginService : ILoginService
{

    TimesheetsManagementEntities TimesheetDatabase = new TimesheetsManagementEntities();

    private readonly ILoginService _LoginService;
    object UserAutheticationStatusObject;

    int UserAutheticationStatusCode;

    public LoginService(ILoginService LoginService) {


        _LoginService = LoginService;
    }


    public int UserAuthetication(string EmailId, string LoginPassword)
    {
        using (TimesheetDatabase) {
             TimesheetDatabase.Database.Connection.Open();
             var command = TimesheetDatabase.Database.Connection.CreateCommand();
             command.CommandText = "dbo.UserLoginAuthentication";
             command.CommandType = System.Data.CommandType.StoredProcedure;
             UserAutheticationStatusObject = (command.ExecuteScalar());

        }

        UserAutheticationStatusCode = (int)UserAutheticationStatusObject;

            return UserAutheticationStatusCode;
    }
}

控制器代码

 public class AccountsController : Controller
{

    private readonly ILoginService _LoginService;

    int UserAuthenticationStatus;




    public AccountsController(ILoginService LoginService) {

        _LoginService = LoginService;

    }


    public AccountsController() { }



    public ActionResult Login()
    {
        LoginViewModel LoginModel = new LoginViewModel();

      UserAuthenticationStatus = _LoginService.UserAuthetication(LoginModel.EmailId, LoginModel.LoginPassword);



        return View(LoginModel);
    }
}

ViewModel代码

public class LoginViewModel
{
    public string EmailId { get; set; }
    public string LoginPassword { get; set; }

}

1 个答案:

答案 0 :(得分:0)

这似乎有点不对,你的LoginService类没有无参数构造函数,唯一可用的构造函数需要一个相同类型的LoginService对象...我只是没有'看看这是如何运作的......

此外,您的ILoginService似乎已注入您的AccountController,请确保您的DependencyInjection容器可以正确地设置您的ILoginService课程。