Value does not fall within the expected range erro

2019-04-09 17:39发布

问题:

I'm developing an ASP.Net MVC 4 application. I have a module that sends out the invoice in an email (using MVCMailer)

for some reason at I get the error: Value does not fall within the expected range

If I step through the code it works without any error but as soon as I just run it it gives me that error. The section producing the error is:

        return Populate(x =>
                {
                    x.From = new System.Net.Mail.MailAddress("Sales@Scheduler.com", "Scheduler");
                    x.Subject = System.Configuration.ConfigurationManager.AppSettings["SiteName"].ToString() + " Inovice #" + (invoice.invoiceID + 1000).ToString() + " (" + (invoice.invoiceStatus == InvoiceStatus.paid ? "Paid" : "Pending") + ")";
                    x.ViewName = "Invoice";
                    x.To.Add(new System.Net.Mail.MailAddress(ToEmailAddress, ToName));
                });

I should also mention that I have checked all of the values being passed in and they all seem to be correct. I personally can't find anything useful in the stack trace but here it is:


Value does not fall within the expected range.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Value does not fall within the expected range.

Source Error:

Line 136:                var ToName = (defaultUser != null && !string.IsNullOrEmpty(defaultUser.EmailAddress)) ? invoice.company.contactFirstName + " " + invoice.company.contactLastName : "Cant Reach";
Line 137:
Line 138:                return Populate(x =>
Line 139:                    {
Line 140:                        x.From = new     System.Net.Mail.MailAddress("Sales@Scheduler.com", "Scheduler");


Source File: e:\Website Projects\Scheduler\Solution\Scheduler\Mailers\UserMailer.cs        Line: 138

Stack Trace:

[ArgumentException: Value does not fall within the expected range.]
   System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) +0
   System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) +10759150
   System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name) +40
   System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name) +36
   System.Web.Hosting.IIS7WorkerRequest.GetRemoteAddress() +18
   System.Web.HttpWorkerRequest.IsLocal() +16
   System.Web.HttpRequestWrapper.get_IsLocal() +19
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +54
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +97
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +260
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   Mvc.Mailer.StringResult.ExecuteResult(ControllerContext context) +538
   Mvc.Mailer.StringResult.ExecuteResult(ControllerContext context, String mailerName) +164
   Mvc.Mailer.MailerBase.EmailBody(String viewName, String masterName) +345
   Mvc.Mailer.MailerBase.PopulateHtmlBody(MailMessage mailMessage, String viewName, String masterName) +73
   Mvc.Mailer.MailerBase.PopulateBody(MailMessage mailMessage, String viewName, String masterName, Dictionary`2 linkedResources) +439
   Mvc.Mailer.MailerBase.Populate(Action`1 action) +171
   Scheduler.Mailers.UserMailer.EmailInvoice(Invoice invoice) in e:\Website Projects\Scheduler\Solution\Scheduler\Mailers\UserMailer.cs:138
   Scheduler.Helpers.EmailHelper.SendInvoice(Invoice invoice) in e:\Website Projects\Scheduler\Solution\Scheduler\Helpers\EmailHelper.cs:105
   Scheduler.Helpers.CreditCardHelper.ChargeCreditCardToUpgradeSubscription(CreditCardTransact    ionViewModel transaction, Invoice invoice, Boolean isScheduler, Boolean emailInvoice) in e:\Website Projects\Scheduler\Solution\Scheduler\Helpers\CreditCardHelper.cs:460
   Scheduler.Controllers.ManagerController.SubscriptionPlan(SubscriptionPlanViewModel model) in e:\Website Projects\Scheduler\Solution\Scheduler\Controllers\ManagerController.cs:2370
   lambda_method(Closure , ControllerBase , Object[] ) +180
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +211
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +57
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +223
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9628972
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

回答1:

Use new mailer instance for each email. https://github.com/smsohan/MvcMailer/issues/96