【问题标题】:Why I receive "Value does not fall within the expected range"?为什么我收到“价值不在预期范围内”?
【发布时间】:2013-10-24 20:41:01
【问题描述】:

我有一个 asp.net mvc web 应用程序,并且一些外部 cron 作业正在从我的 CronJobController 调用一个特定的 url 并执行一个特定的方法,例如:

/// <summary>
    /// Will be called externally by www.setcronjob.com.
    /// </summary>
    /// <returns></returns>
    public async Task<ActionResult> Index()
    {
        RunTwitterSearchQuery();
        RunFacebookSearchQuery();
        return new ContentResult { Content = "Ok" };
    }

在 RunTwitterSearchQuery 中,我想发送一些电子邮件,我尝试使用 MvcMailerActionMailer,例如(使用 ActionMailer):

var mailController = new MailController();
mailController.HttpContextBase = this.HttpContext;
mailController.MoreThanXFollowersEmail(alertEmailModel).Deliver();

因为 http 上下文为空,我必须以这种方式设置它以避免 NPE,但之后我收到一个非常奇怪的异常(在 MvcMailer 和 ActionMailer 中):

    System.ArgumentException: Value does not fall within the expected range.
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
       at System.Web.Hosting.IIS7WorkerRequest.GetUserAgentInternal()
       at System.Web.Hosting.IIS7WorkerRequest.GetKnownRequestHeader(Int32 index)
       at System.Web.HttpRequest.get_UserAgent()
       at System.Web.HttpRequestWrapper.get_UserAgent()
       at System.Web.WebPages.BrowserHelpers.GetOverriddenUserAgent(HttpContextBase httpContext)
       at System.Web.WebPages.BrowserHelpers.GetOverriddenBrowser(HttpContextBase httpContext, Func`2 createBrowser)
       at System.Web.WebPages.BrowserHelpers.GetOverriddenBrowser(HttpContextBase httpContext)
       at System.Web.WebPages.DisplayModeProvider.<.ctor>b__2(HttpContextBase context)
       at System.Web.WebPages.DefaultDisplayMode.CanHandleContext(HttpContextBase httpContext)
       at System.Web.WebPages.DisplayModeProvider.<>c__DisplayClass6.<GetAvailableDisplayModesForContext>b__5(IDisplayMode mode)
       at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)
       at System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache)
       at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClassc.<FindView>b__a(IViewEngine e)
       at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths)
       at System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName)
       at ActionMailer.Net.Mvc.EmailResult.LocateViews(ControllerContext context) in c:\Dev\actionmailer\src\ActionMailer.Net.Mvc\EmailResult.cs:line 127
       at ActionMailer.Net.Mvc.EmailResult.AddMessageViews(ControllerContext context) in c:\Dev\actionmailer\src\ActionMailer.Net.Mvc\EmailResult.cs:line 155
       at ActionMailer.Net.Mvc.EmailResult.ExecuteResult(ControllerContext context) in c:\Dev\actionmailer\src\ActionMailer.Net.Mvc\EmailResult.cs:line 98
       at ActionMailer.Net.Mvc.MailerBase.Email(String viewName, Object model, String masterName, Boolean trimBody) in c:\Dev\actionmailer\src\ActionMailer.Net.Mvc\MailerBase.cs:line 166
       at SocialCrm.Controllers.MailController.MoreThanXFollowersEmail(AlertEmailModel model) in c:\dev\palminfo\SocialCrm\SocialCrm\Controllers\MailController.cs:line 18
       at SocialCrm.Controllers.CronJobController.SendAlertEmail(SearchResultModel searchResultModel, Int32 searchQueryId, List`1 alerts, String emailTo, String streamName) in c:\dev\palminfo\SocialCrm\SocialCrm\Controllers\Batch\CronJobController.cs:line 146
       at SocialCrm.Controllers.CronJobController.<RunTwitterSearchQuery>b__3() in c:\dev\palminfo\SocialCrm\SocialCrm\Controllers\Batch\CronJobController.cs:line 83

这很丑陋,我非常感谢在这个问题上提供任何帮助。

更新:当使用 MvcMailer this 是我收到的错误。

【问题讨论】:

  • 我能看到的最明显的事情是确保 searchQueryId 是有效的 Int32,而不是 Int64、字符串等。
  • @ChrisPratt 是的,例如我用searchQueryId = 1 测试。我感觉这个问题主要是关于 httpContext 在以这种异步方式使用时缺少一些字段......
  • 嗨@Cristian Boariu,你能解决它吗?我在使用 ActionMailer 时遇到了完全相同的问题。

标签: c# asp.net-mvc actionmailer mvcmailer


【解决方案1】:

如果您使用的是 MvcMailer,请尝试发布在 github 上的解决方案,以解决看起来非常相似的问题。解决方案是每次发送电子邮件时创建一个新的 UserMailer:Github Answer

【讨论】:

  • 也试过了,没用。
猜你喜欢
  • 1970-01-01
  • 2011-08-09
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多