使用Postal with QueueBackgroundWorkItem - 无法创建请求生命周期范围>因为HttpContext不可用

时间:2016-03-17 13:04:20

标签: c# autofac postal

我正在使用Postal发送电子邮件。 我的应用程序是一个ASP.net MVC应用程序。我使用Autofac(MVC5集成)

我设置像这样的邮政:

body {
background-image: url("C:/Users/Aca/Desktop/Sajt/Slike/pattern.png");
background-repeat: repeat;
}
#strana {
margin: auto;
background-color: white;
width: 800px;
height: 2000px;
}

header {
background-color: white;
border: solid red 2px;
height: 100px;
}

#logo {
float: left;
display: inline-block;
width: 143px;
height: 120px;
margin-top: -17px;
padding-right: 47px;
}

nav {
background-color: white;
/*border: solid red 2px;*/
position: absolute;
display: inline-block;
margin-right: 100px;
height: 70px;
width: 400px;
margin: auto;
margin-top: 10px;
text-align: left;
}

#navbar {
position: absolute;
width: 350px;
display: inline-block;
/*border: solid red 2px;*/
padding: 10px 0px 10px 0px;
margin: 12px 22px 0px 22px;
text-align: center;
}

li {
margin-left: 0px;
display: inline-block;
}

.nav {
text-decoration: none;
color: red;
font-family: cursive;
font-weight: bold;
font-size: 15px;
border: solid red 2px;
padding-left: 3px;
padding-right: 3px;
}

.nav:hover {
background-color: red;
color: white;
}

#main {
height:400px;
position: relative;
text-align: left;
}

.button{
}

此操作失败,并显示以下错误:

  

发生了'System.InvalidOperationException'类型的异常   Autofac.Integration.Mvc.dll但未在用户代码中处理

     

其他信息:无法创建请求生存期范围   因为HttpContext不可用。

        dynamic email = new Email("TemplateName");
        email.To = "test@email.com";
        email.From = "sendertest@email.com";

        email.TemplateValue = "This is a value";

        var service = new EmailService();

        HostingEnvironment.QueueBackgroundWorkItem(ct =>
        {
            service.Send(email);
        });

如果我从 at Autofac.Integration.Mvc.RequestLifetimeScopeProvider.GetLifetimeScope(Action`1 configurationAction) at Autofac.Integration.Mvc.AutofacDependencyResolver.get_RequestLifetimeScope() at Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(Type serviceType) at System.Web.Mvc.BuildManagerViewEngine.DefaultViewPageActivator.Create(ControllerContext controllerContext, Type type) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at Postal.EmailViewRenderer.RenderView(IView view, ViewDataDictionary viewData, ControllerContext controllerContext, ImageEmbedder imageEmbedder) at Postal.EmailViewRenderer.Render(Email email, String viewName) at Postal.EmailService.CreateMailMessage(Email email) at Postal.EmailService.Send(Email email) at CallSite.Target(Closure , CallSite , EmailService , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1) at EmailSender.<>c__DisplayClass3_0.<Handle>b__0(CancellationToken ct) in .....EmailSender.cs:line 42 at System.Web.Hosting.HostingEnvironment.<>c__DisplayClass91_0.<QueueBackgroundWorkItem>b__0(CancellationToken ct) at System.Web.Hosting.BackgroundWorkScheduler.<RunWorkItemImpl>d__7.MoveNext() 中取出service.Send(email);,那就可以了。

我理解这是因为QueueBackgroundWorkItem不可用 - 但有没有办法可以在后台线程上使用Postal发送电子邮件?

2 个答案:

答案 0 :(得分:2)

Postal只能创建一个MailMessage对象,而无需发送。见http://aboutcode.net/postal/create-mail-message.html

所以你可以在MVC控制器动作中做到这一点,而HttpContext仍然存在。

然后,使用您自己的MailMessage实例在后台主题中发送SmtpClient

答案 1 :(得分:0)

您是否尝试过创建自定义视图引擎?

正如建议的here,可能在ASP.NET请求范围之外使用邮件(我知道您正在使用MVC 5,但对我来说看起来情况相同)。

如果您仍然收到错误,可能是Autofac注册问题:您是否在请求范围内注册了某些内容,例如<div class="div_with_background_image"> <p>some content</p> </div>?在这种情况下,Autofac将无法解决管道外的组件。