Base solution for your next web application
Open Closed

UnitOfWorkManager.Current is null #530


User avatar
0
mhdbaz created

UnitOfWorkManager.Current is null inside the the AppService classes if I did not add [UnitOfWork] Attribute in the controller action

[HttpPost]
        [UnitOfWork]
        //[ValidateAntiForgeryToken()]
        public virtual async Task<ActionResult> Register(SignUpUserInput model)
        {
            var ttt = _unitOfWorkManager.Current;
            //await _userAppService.SignUpUser(model);
            //throw new UserFriendlyException("Test UI Exception");
            try
            {
                CheckModelState();
                //var validateCaptchaResult = ValidateCaptcha();
                //if (!validateCaptchaResult.Succeeded)
                //{
                //    throw new Exception(validateCaptchaResult.Errors.FirstOrDefault() ??  string.Empty);
                //}
                await _userAppService.SignUpUser(model);
                return RedirectToAction("RegistrationCompleted");
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ViewBag.ErrorMessage += "<br>" + ex.InnerException.Message;
                }
                return View(model);
            }
        }

The code above if I removed [UnitOfWork] attribute on the Controller action the UnitOfWorkManager.Current will be null insdie the _userAppService.

Is that normal by Design ? Should I add [UnitOfWork] to all controller actions ?


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    If you are injecting application service via class (like YourAppService) instead of interface (like IYourAppService) then you should make your app service method as virtual. Thus, you can remove UnitOfWork attr from the controller.

  • User Avatar
    0
    mhdbaz created

    <cite>hikalkan: </cite> If you are injecting application service via class (like YourAppService) instead of interface (like IYourAppService) then you should make your app service method as virtual. Thus, you can remove UnitOfWork attr from the controller.

    Ok thanks