Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]" displayed instead of view

I have a controller

public class InvitationController : Controller
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create(InvitationModel invitationmodel)
    {
        if (ModelState.IsValid)
        {
            var regLink = _repo.SaveAndGetRegistrationLink(invitationmodel);


            IEMailer mailer = new EMailer();
            var inv = mailer.Invitation(invitationmodel.Email, regLink);

            await Task.WhenAll(new AsyncEmailSender().SendEmail(inv));

            return RedirectToAction("Index");
        }

        return View(invitationmodel);
    }
}

It works fine on my localhost (redirects to a desired page after sending the email). I published my website to smarterasp.net

And now it shows a string instead of redirecting:

System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]

What's the reason, and how to fix it?

Elmah didn't log anything. The email was actually sent.

I tried adding MVC dlls. All the files below sit next to my website dll.

  • System.Web.Mvc
  • Microsoft.Web.Infrastructure
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Razor
  • System.Web.Helpers

edit

Host admin sent me a log from iis:

Exception: System.NullReferenceException

Message: Object reference not set to an instance of an object.

StackTrace: at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
at System.Web.LegacyAspNetSynchronizationContext.Post(SendOrPostCallback callback, Object state)
at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.PostAction(Object state)
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.AwaitTaskContinuation.<ThrowAsyncIfNecessary>b__1(Object s)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
like image 549
Andrzej Gis Avatar asked Jul 18 '13 02:07

Andrzej Gis


2 Answers

Maybe they have some kind of beta version, I am not sure. But System.Web.LegacyAspNetSynchronizationContext in the stack suggests that you need to add UseTaskFriendlySynchronizationContext flag in your Web.config.

Check the following: http://forums.asp.net/t/1778103.aspx/1

like image 160
mostruash Avatar answered Oct 01 '22 02:10

mostruash


I had this exact same issue, I found the problem was when I had upgraded to .NET framework 4.5 from 4.0, when I upgraded MVC 3 did not upgrade and would not handle .NET 4.5. I found that I had to upgrade to MVC 4, I found a package on NuGet called Upgrade MVC 3 To MVC 4 by Nandip Makawana. When I used this it cleared up the issue nicely but I got another error. With the upgrade my Web.config file changed and it needed to be fixed. which was easy and explained in the error thrown.

<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="webpages:Version" value="2.0.0.0" /> <---------- This line
<add key="PreserveLoginUrl" value="true" />

the webpages version changed to 2 and needed to change back to 1

<add key="webpages:Version" value="1.0.0.0" />

After this change my application worked quite nicely. I hope this helps someone, it took me 3 days looking for an answer myself.

like image 37
AaronH Avatar answered Oct 01 '22 04:10

AaronH