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.
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()
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With