我有一个控制器
公共类InvitationController:控制器
{
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务<的ActionResult>创建(InvitationModel invitationmodel)
{
如果(ModelState.IsValid)
{
VAR注册连接= _repo.SaveAndGetRegistrationLink(invitationmodel);
IEMailer邮件=新邮件器();
VAR INV = mailer.Invitation(invitationmodel.Email,注册连接);
等待Task.WhenAll(新AsyncEmailSender()SendEmail(INV));
返回RedirectToAction(指数);
}
返回查看(invitationmodel);
}
}
它工作正常在我的本地(重定向到所需的页面发送邮件后)。 我发表我的网站smarterasp.net
和现在它显示重定向的字符串,而不是:
System.Threading.Tasks.Task`1 [System.Web.Mvc.ActionResult]
是什么原因,以及如何解决它?
ELMAH没有记录任何东西。电子邮件实际发送的。
我尝试添加MVC的DLL。下面的所有文件,坐在旁边的我的网站的dll。
System.Web.Mvc Microsoft.Web.Infrastructure System.Web.Razor System.Web.WebPages System.Web.WebPages.Razor System.Web.Helpers修改
主机管理员从IIS给我发了一个日志:
异常信息:System.NullReferenceException
消息:对象引用未设置到对象的实例。
堆栈跟踪:在System.Web.ThreadContext.AssociateWithCurrentThread(布尔setImpersonationContext)
在System.Web.HttpApplication.OnThreadEnterPrivate(布尔setImpersonationContext)
在System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback回调,对象的状态)
在System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback回调,对象的状态)
在System.Web.LegacyAspNetSynchronizationContext.Post(SendOrPostCallback回调,对象的状态)
在System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.PostAction(对象状态)
在System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback回调,对象的状态,任务和放大器; currentTask)
---从previous位置的堆栈跟踪,其中引发异常的结束---
在System.Threading.Tasks.AwaitTaskContinuation< ThrowAsyncIfNecessary> b__1(对象S)
在System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(对象状态)
在System.Threading.ExecutionContext.RunInternal(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)
在System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
在System.Threading.ThreadPoolWorkQueue.Dispatch()
在System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
解决方案
也许他们有某种形式的测试版,我不知道。但 System.Web.LegacyAspNetSynchronizationContext
在堆栈中表明,你需要添加 UseTaskFriendlySynchronizationContext
标记在 Web.config文件
。
检查以下内容: http://forums.asp.net/t/1778103。 ASPX / 1
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.Helpersedit
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