當試圖在也使用Mvc.Unity4的MVC應用程序中使用Postal時,我遇到了一個奇怪的問題。MVC Postal Emailer與Mvc.Unity4衝突
我相信這個問題是由於缺少對HttpContext
的訪問。
我試圖從裏面我的一個控制器發送的電子郵件使用郵政:
dynamic e = new Email("AccountActivation");
e.To = "[email protected]"
e.Send();
試圖內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
我不是對Mvc.Unity4太熟悉了,因爲這是由不同的開發人員添加的。
抓住吸管,我嘗試在Application_Start中註冊正確的Postal類型。統一容器的初始化是發生在Bootstrapper.cs
:
container.RegisterType<UsersController>(new InjectionConstructor());
container.RegisterInstance<IEmailService>(new EmailService());
在我的控制,我有:
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", "[email protected]");
e.ViewData.Add("From", "[email protected]");
_emailService.Send(e);
More code...
}
爲了澄清,我創建併發送Postal郵件的電話確實位於Application_Start之外的控制器中。在Application_Start運行後,我只是試圖在Application_Start中註冊Postal對象,將郵件代碼註冊到控制器中。 – 2015-02-05 20:45:07
請發佈郵政及其所有依賴關係的相關DI配置。 – NightOwl888 2015-02-06 02:13:09