我想你的方法需要一个 HttpContext 但不是通过 WebRequest 执行的。
HttpContext 不容易模拟,但并非不可能。尝试使用此代码分配 HttpContext 当前
HttpContext.Current = HttpContextHelper.CreateHttpContext(
new HttpRequest("SomePage.asmx", "http://localhost/SomePage.asmx", ""),
new HttpResponse(new StringWriter())
);
HttpContext helper 是一个基于这篇博文http://www.necronet.org/archive/2010/07/28/unit-testing-code-that-uses-httpcontext-current-session.aspx的帮助类
public class HttpContextHelper
{
public static HttpContext CreateHttpContext(HttpRequest httpRequest, HttpResponse httpResponse)
{
var httpContext = new HttpContext(httpRequest, httpResponse);
var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
new HttpStaticObjectsCollection(), 10, true,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc, false);
httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, CallingConventions.Standard,
new[] { typeof(HttpSessionStateContainer) },
null)
.Invoke(new object[] { sessionContainer });
return httpContext;
}
}
但是(这很重要):您可能做错了。 BackgroundWorker 不打算在 MVC 中使用,而是在 Windows 窗体中使用。请尝试改用 TPL。
public ActionResult SendMail()
{
Task.Factory.StartNew(() => MailSender.SendMail(...));
return View(...);
}
或者甚至更好,使用异步:
[AsyncTimeout(150)]
[HandleError(ExceptionType = typeof(TimeoutException),
View = "TimeoutError")]
public async Task<ActionResult> SendMailAsync(CancellationToken cancellationToken )
{
ViewBag.SyncOrAsync = "Asynchronous";
return View("SendMail", await MailSender.SendMailAsync(cancellationToken));
}
这里解释http://www.asp.net/mvc/overview/performance/using-asynchronous-methods-in-aspnet-mvc-4