asynchronousasp.net-mvc-5entity-framework-6asp.net-identity

Error in user Registration of user using async controller


I have simple project using the template of Microsoft MVC's Authentication. upon debugging on the development machine and IIS express, I can create an new user I can Log in using its default controllers, But when i tried to publish it on IIS i catch a null reference exception on the Registration Process. here is the stacktrace:

    [NullReferenceException: Object reference not set to an instance of an    object.]
        TrabahoNow.Web.Controllers.<Register>d__8.MoveNext() +224
        System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
        System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
        System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +84
        System.Web.Mvc.Async.<>c__DisplayClass37. <BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
        System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult  asyncResult) +10
        System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
        System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
        System.Web.Mvc.Async.AsyncInvocationWithFilters.   <InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
        System.Web.Mvc.Async.<>c__DisplayClass46.  <InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
        System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
        System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
        System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
        System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
        System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
        System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
        System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
        System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
        System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
        System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
        System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
        System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
        System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
        System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
        System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
        System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
        System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
        System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
        System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
        System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
        System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
        System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
        System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
        System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651796
        System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

I have no Idea what cause it coz it is running on express. here is my controller action:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser()
            {
                UserName = model.Email,
                FirstName = model.FirstName.ToUpper(),
                LastName = model.LastName.ToUpper(),
                MiddleName = model.MiddleName.ToUpper(),
                Email = model.Email,
                PhoneNumber = model.Phone,
                DesiredPosition = model.DesiredPosition,
                BirthDate = DateTime.Now,


            };

            var result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                   await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");                    

            }
            else
            {
                AddErrors(result);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

This fails in IIS Server. Can someone help me figure this one out??

here is my set up..

.NET 4.5.1

EF 6.0

MVC 5


Solution

  • Given the exception that you have, and confirmation that the error happens when you construct an instance of your ApplicationUser class, your issue is highly likely to be that either model.FirstName, model.LastName or model.MiddleName is null.

    The call to ToUpper() will be the root cause of your exception.

    Either check for null:

    FirstName = string.IsNullOrEmpty(model.FirstName) ? null : model.FirstName
    

    Or, if C# 6 is available to you, use the null propagation operator:

    FirstName = model.FirstName?.ToUpper();