mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-08-01 18:45:54 +08:00
Mocking HttpContext in background tasks
This commit is contained in:
parent
29171b6531
commit
330937e821
@ -1,4 +1,6 @@
|
||||
using Orchard.Messaging.Events;
|
||||
using System;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Messaging.Events;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Messaging.Models;
|
||||
using Orchard.Security;
|
||||
@ -9,8 +11,12 @@ namespace Orchard.Email.Services {
|
||||
|
||||
public EmailMessageEventHandler(IContentManager contentManager) {
|
||||
_contentManager = contentManager;
|
||||
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
public void Sending(MessageContext context) {
|
||||
if (context.Recipients != null) {
|
||||
foreach (var rec in context.Recipients) {
|
||||
@ -27,7 +33,12 @@ namespace Orchard.Email.Services {
|
||||
}
|
||||
|
||||
foreach (var address in context.Addresses) {
|
||||
context.MailMessage.To.Add(address);
|
||||
try {
|
||||
context.MailMessage.To.Add(address);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Logger.Error(e, "Unexpected error while trying to send email.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,16 +14,15 @@ using Orchard.Mvc.Extensions;
|
||||
namespace Orchard.Tokens.Providers {
|
||||
public class ContentTokens : ITokenProvider {
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly UrlHelper _urlHelper;
|
||||
|
||||
public ContentTokens(IContentManager contentManager, IWorkContextAccessor workContextAccessor) {
|
||||
public ContentTokens(IContentManager contentManager, UrlHelper urlHelper) {
|
||||
_contentManager = contentManager;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_urlHelper = urlHelper;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
private UrlHelper UrlHelper { get { return new UrlHelper(_workContextAccessor.GetContext().HttpContext.Request.RequestContext); } }
|
||||
|
||||
public void Describe(DescribeContext context) {
|
||||
context.For("Content", T("Content Items"), T("Content Items"))
|
||||
@ -115,8 +114,8 @@ namespace Orchard.Tokens.Providers {
|
||||
}
|
||||
|
||||
context.For<string>("Url")
|
||||
.Token("Absolute", url => new UrlHelper(_workContextAccessor.GetContext().HttpContext.Request.RequestContext).MakeAbsolute(url))
|
||||
.Chain("Absolute", "Text", url => new UrlHelper(_workContextAccessor.GetContext().HttpContext.Request.RequestContext).MakeAbsolute(url))
|
||||
.Token("Absolute", url => _urlHelper.MakeAbsolute(url))
|
||||
.Chain("Absolute", "Text", url => _urlHelper.MakeAbsolute(url))
|
||||
;
|
||||
|
||||
context.For<TextField>("TextField")
|
||||
@ -178,7 +177,7 @@ namespace Orchard.Tokens.Providers {
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
return UrlHelper.RouteUrl(_contentManager.GetItemMetadata(content).DisplayRouteValues);
|
||||
return _urlHelper.RouteUrl(_contentManager.GetItemMetadata(content).DisplayRouteValues);
|
||||
}
|
||||
|
||||
private string EditUrl(IContent content) {
|
||||
@ -186,7 +185,7 @@ namespace Orchard.Tokens.Providers {
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
return UrlHelper.RouteUrl(_contentManager.GetItemMetadata(content).EditorRouteValues);
|
||||
return _urlHelper.RouteUrl(_contentManager.GetItemMetadata(content).EditorRouteValues);
|
||||
}
|
||||
|
||||
private string Body(IContent content) {
|
||||
|
@ -1,11 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Autofac;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Mvc.Routes;
|
||||
using Orchard.Settings;
|
||||
|
||||
namespace Orchard.Mvc {
|
||||
public class MvcModule : Module {
|
||||
@ -39,7 +46,21 @@ namespace Orchard.Mvc {
|
||||
return new HttpContextWrapper(HttpContext.Current);
|
||||
}
|
||||
|
||||
return new HttpContextPlaceholder();
|
||||
// this doesn't work in a background service, throws an exception in ContentManager.Handlers
|
||||
//var siteService = context.Resolve<ISiteService>();
|
||||
//var baseUrl = siteService.GetSiteSettings().BaseUrl;
|
||||
|
||||
var session = context.Resolve<ISessionLocator>().For(typeof(ContentItem));
|
||||
var shellSettings = context.Resolve<ShellSettings>();
|
||||
|
||||
var tableName = "Settings_SiteSettings2PartRecord";
|
||||
if (!string.IsNullOrEmpty(shellSettings.DataTablePrefix)) {
|
||||
tableName = shellSettings.DataTablePrefix + "_" + tableName;
|
||||
}
|
||||
var query = session.CreateSQLQuery("SELECT BaseUrl FROM " + tableName);
|
||||
var baseUrl = query.UniqueResult<string>();
|
||||
|
||||
return new HttpContextPlaceholder(baseUrl);
|
||||
}
|
||||
|
||||
static RequestContext RequestContextFactory(IComponentContext context) {
|
||||
@ -59,7 +80,7 @@ namespace Orchard.Mvc {
|
||||
}
|
||||
}
|
||||
else {
|
||||
httpContext = new HttpContextPlaceholder();
|
||||
httpContext = HttpContextBaseFactory(context);
|
||||
}
|
||||
|
||||
return new RequestContext(httpContext, new RouteData());
|
||||
@ -73,17 +94,39 @@ namespace Orchard.Mvc {
|
||||
/// standin context for background tasks.
|
||||
/// </summary>
|
||||
class HttpContextPlaceholder : HttpContextBase {
|
||||
private readonly string _baseUrl;
|
||||
|
||||
public HttpContextPlaceholder(string baseUrl) {
|
||||
_baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
public override HttpRequestBase Request {
|
||||
get { return new HttpRequestPlaceholder(); }
|
||||
get { return new HttpRequestPlaceholder(new Uri(_baseUrl)); }
|
||||
}
|
||||
|
||||
public override IHttpHandler Handler { get; set; }
|
||||
|
||||
public override HttpResponseBase Response {
|
||||
get { return new HttpResponsePlaceholder(); }
|
||||
}
|
||||
}
|
||||
|
||||
private class HttpResponsePlaceholder : HttpResponseBase {
|
||||
public override string ApplyAppPathModifier(string virtualPath) {
|
||||
return virtualPath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// standin context for background tasks.
|
||||
/// </summary>
|
||||
class HttpRequestPlaceholder : HttpRequestBase {
|
||||
private readonly Uri _uri;
|
||||
|
||||
public HttpRequestPlaceholder(Uri uri) {
|
||||
_uri = uri;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// anonymous identity provided for background task.
|
||||
/// </summary>
|
||||
@ -97,6 +140,41 @@ namespace Orchard.Mvc {
|
||||
return new NameValueCollection();
|
||||
}
|
||||
}
|
||||
|
||||
public override Uri Url {
|
||||
get {
|
||||
return _uri;
|
||||
}
|
||||
}
|
||||
|
||||
public override NameValueCollection Headers {
|
||||
get {
|
||||
return new NameValueCollection {{"Host", _uri.Authority}};
|
||||
}
|
||||
}
|
||||
|
||||
public override string AppRelativeCurrentExecutionFilePath {
|
||||
get {
|
||||
return "~/";
|
||||
}
|
||||
}
|
||||
|
||||
public override string ApplicationPath {
|
||||
get {
|
||||
return _uri.LocalPath;
|
||||
}
|
||||
}
|
||||
|
||||
public override NameValueCollection ServerVariables {
|
||||
get {
|
||||
return new NameValueCollection {
|
||||
{ "SERVER_PORT", _uri.Port.ToString(CultureInfo.InvariantCulture) },
|
||||
{ "HTTP_HOST", _uri.Authority.ToString(CultureInfo.InvariantCulture) },
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user