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)
-
0
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.
-
0
<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