smsohan / MvcMailer

A Mailer for ASP.Net MVC that forms the Email Body using MVC Views (Razor etc.) following Ruby on Rails ActionMailer style
MIT License
584 stars 178 forks source link

Value does not fall within the expected range. #60

Closed SimonSorcerer closed 11 years ago

SimonSorcerer commented 11 years ago

Hello

I'm trying to include MVC Mailer into our project and I'm getting this exception "Value does not fall within the expected range." when the code hits PopulateBody function call.

I don't think I'm doing anything special. Here's the call from controller:

    _userMailer.PasswordReset().SendAsync();

Here's the Welcome function:

    public virtual MailMessage UserWelcome(Profile profile)
    {
        var mailMessage = new MailMessage{ Subject = "Welcome!" };

        mailMessage.To.Add(profile.EmailAddress);
        PopulateBody(mailMessage, viewName: "UserWelcome");

        return mailMessage;
    }

I suspect the exception is thrown because the controller context is not valid at the time of the call and this is caused because I'm running the app locally on Azure service emulator - but I'm not really sure. Any idea if this is the case and how this could be fixed - or what else coud it be?

Many thanks in advance.

smsohan commented 11 years ago

I've not seen this before. I would suggest looking at the stack trace for more info. But MvcMailer would need a valid http context same as your controllers.


Sent from my iPhone Sohan SM

On 2012-09-26, at 10:21 AM, Matej notifications@github.com wrote:

Hello

I'm trying to include MVC Mailer into our project and I'm getting this exception "Value does not fall within the expected range." when the code hits PopulateBody function call.

I don't think I'm doing anything special. Here's the call from controller:

_userMailer.PasswordReset().SendAsync();

Here's the Welcome function:

public virtual MailMessage UserWelcome(Profile profile)
{
    var mailMessage = new MailMessage{ Subject = "Welcome!" };

    mailMessage.To.Add(profile.EmailAddress);
    PopulateBody(mailMessage, viewName: "UserWelcome");

    return mailMessage;
}

I suspect the exception is thrown because the controller context is not valid at the time of the call and this is caused because I'm running the app locally on Azure service emulator - but I'm not really sure. Any idea if this is the case and how this could be fixed - or what else coud it be?

Many thanks in advance.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/60.

SimonSorcerer commented 11 years ago

Thanks for answer. Here's the top of the stack trace - looking at this and checking the source - it looks like it really is some problem with HttpContext, or more precisely route table... I'll try to write a quick test to see replicate the situation - I may have some odd settings there

[ArgumentException: Value does not fall within the expected range.] Mvc.Mailer.StringResult.ExecuteResult(ControllerContext context, String mailerName) +406 Mvc.Mailer.MailerBase.EmailBody(String viewName, String masterName) +327 Mvc.Mailer.MailerBase.PopulateHtmlBody(MailMessage mailMessage, String viewName, String masterName) +31

SimonSorcerer commented 11 years ago

I was able to find the cause once I've checked the mailerBase code. The current httpContext is being stored in mailerBase constructor, and I was injecting my mailerBase child class into controllers with singleton lifestyle in Windsor castle. Which of course made the stored httpContext valid only the first time the controller (with mailer class injection) instance was created. So setting the lifestyle of my mailer class to transient solved the problem.

smsohan commented 11 years ago

Nice! Good to know you fixed it.


Sent from my iPhone Sohan SM

On 2012-10-04, at 8:02 AM, Matej notifications@github.com wrote:

I was able to find the cause once I've checked the mailerBase code. The current httpContext is being stored in mailerBase constructor, and I was injecting my mailerBase child class into controllers with singleton lifestyle in Windsor castle. Which of course made the stored httpContext valid only the first time the controller (with mailer class injection) instance was created. So setting the lifestyle of my mailer class to transient solved the problem.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/60#issuecomment-9142186.