2013-10-24 90 views
1

我有一个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上下文为null我必须设置它,这样既避免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是我收到的错误。

+0

我能看到的最明显的事情是确保'searchQueryId'是一个有效的Int32,而不是Int64,字符串等。 –

+0

@ChrisPratt是的,它是,例如我用'searchQueryId = 1'测试。我有一种感觉,这个问题主要是关于httpContext在使用这种异步方式时缺少一些字段... –

+0

Hi @Cristian Boariu,你能解决它吗?我遇到与ActionMailer完全相同的问题。 – GurjeetSinghDB

回答

0

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

+0

尝试了这一点,它并没有帮助。 –

相关问题