I'm experiencing a weird issue when attempting to use Postal within an MVC application that also uses Mvc.Unity4.
I believe the issue is thrown due to lacking access to the HttpContext
.
I attempt to send the email from inside one of my Controllers using Postal:
dynamic e = new Email("AccountActivation");
e.To = "name@email.com"
e.Send();
Attempting to send an email results in the following exception within Unity.Mvc4.UnityDependencyResolver
:
[NullReferenceException: Object reference not set to an instance of an object.]
Unity.Mvc4.UnityDependencyResolver.get_ChildContainer() +57
Unity.Mvc4.UnityDependencyResolver.GetService(Type serviceType) +241
System.Web.Mvc.DefaultViewPageActivator.Create(ControllerContext controllerContext, Type type) +87
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +216
Postal.EmailViewRenderer.RenderView(IView view, ViewDataDictionary viewData, ControllerContext controllerContext, ImageEmbedder imageEmbedder) +182
Postal.EmailViewRenderer.Render(Email email, String viewName) +204
Postal.EmailService.CreateMailMessage(Email email) +72
Postal.EmailService.Send(Email email) +65
I'm not too familiar with Mvc.Unity4, as this was added by a different developer.
Grasping at straws, I did try to register the correct Postal types within the Application_Start. The initialization of the Unity container is occurring in Bootstrapper.cs
:
container.RegisterType<UsersController>(new InjectionConstructor());
container.RegisterInstance<IEmailService>(new EmailService());
Within my controller, I have:
private IEmailService _emailService;
public UsersController()
{
_emailService = new Postal.EmailService();
}
public UsersController(Postal.EmailService emailService)
{
_emailService = emailService;
}
[HttpPost]
public async Task<ActionResult> SendEmail(EmailViewModel viewModel)
{
dynamic e = new Email("AccountActivation");
e.ViewData.Add("To", "name@email.com");
e.ViewData.Add("From", "no-reply@email.com");
_emailService.Send(e);
More code...
}