1

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...
}
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Josh Greenberg
  • 323
  • 1
  • 7

2 Answers2

0

I think your suspicion is right. HttpContext is not available during application initialization. Therefore Postal will not work (because it relies on ControllerContext and ViewContext) in the Application_Start event.

Since you are using DI, this also extends to the constructor of every class that is configured using Unity - you cannot use Postal in the constructor, but can in methods that are called after Application_Start is complete.

You need to move the call to Postal outside of Application_Start or else use the native .NET mail libraries in this one case (because they rely on System.Net and have no dependencies on HttpContext).

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Just to clarify, my call to create and send the Postal email does reside in a controller outside of Application_Start. I was only attempting to register the Postal objects within the Application_Start, with the emailing code inside a controller, after Application_Start has run. – Josh Greenberg Feb 05 '15 at 20:45
  • Please post the relevent DI configuration for postal and all its dependencies. – NightOwl888 Feb 06 '15 at 02:13
0

Sorry to answer my own question, but I was finally able to get this working.

Once I removed the async from the method, everything began working as expected.

So by changing...

public async Task<ActionResult> SendEmail(EmailViewModel viewModel)

To this...

public ActionResult SendEmail(EmailViewModel viewModel)

I was then able to send the emails without triggering the exception inside Unity.Mvc4.UnityDependencyResolver.get_ChildContainer().

I'm not sure why I wasn't able to call Postal's .Send() from within an async method (side note, I tried to call .SendAsync() with the same Unity issue resulting). If anyone can shed some light on why this would work within an async method, it would be much appreciated.

Thanks.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Josh Greenberg
  • 323
  • 1
  • 7