{"action":"created","issue":{"url":"https://api.github.com/repos/OrchardCMS/Orchard/issues/6033","labels_url":"https://api.github.com/repos/OrchardCMS/Orchard/issues/6033/labels{/name}","comments_url":"https://api.github.com/repos/OrchardCMS/Orchard/issues/6033/comments","events_url":"https://api.github.com/repos/OrchardCMS/Orchard/issues/6033/events","html_url":"https://github.com/OrchardCMS/Orchard/issues/6033","id":116927907,"number":6033,"title":"EmailActivity fails when run via background task","user":{"login":"bmingles","id":1900643,"avatar_url":"https://avatars.githubusercontent.com/u/1900643?v=3","gravatar_id":"","url":"https://api.github.com/users/bmingles","html_url":"https://github.com/bmingles","followers_url":"https://api.github.com/users/bmingles/followers","following_url":"https://api.github.com/users/bmingles/following{/other_user}","gists_url":"https://api.github.com/users/bmingles/gists{/gist_id}","starred_url":"https://api.github.com/users/bmingles/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bmingles/subscriptions","organizations_url":"https://api.github.com/users/bmingles/orgs","repos_url":"https://api.github.com/users/bmingles/repos","events_url":"https://api.github.com/users/bmingles/events{/privacy}","received_events_url":"https://api.github.com/users/bmingles/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-11-14T14:18:03Z","updated_at":"2015-11-14T16:18:24Z","closed_at":null,"body":"In Orchard 1.9.2 EmailActivity no longer seems to work when run after a TimeActivity.\r\n\r\n    2015-11-10 23:03:47,308 [7] Orchard.Exceptions.DefaultExceptionPolicy - (null) - An unexpected exception was caught\r\n     (null)\r\n    System.NullReferenceException: Object reference not set to an instance of an object.\r\n       at Orchard.Mvc.ViewEngines.Razor.WebViewPage`1.get_DisplayHelperFactory() in c:\\git\\mysite\\orchard\\src\\Orchard\\Mvc\\ViewEngines\\Razor\\WebViewPage.cs:line 70\r\n       at Orchard.Mvc.ViewEngines.Razor.WebViewPage`1.InitHelpers() in c:\\git\\mysite\\orchard\\src\\Orchard\\Mvc\\ViewEngines\\Razor\\WebViewPage.cs:line 168\r\n       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)\r\n       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)\r\n       at Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.RenderRazorViewToString(String path, DisplayContext context) in c:\\git\\mysite\\orchard\\src\\Orchard\\DisplayManagement\\Descriptors\\ShapeTemplateStrategy\\ShapeTemplateBindingStrategy.cs:line 174\r\n       at Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.Render(ShapeDescriptor shapeDescriptor, DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo, HarvestShapeHit harvestShapeHit) in c:\\git\\mysite\\orchard\\src\\Orchard\\DisplayManagement\\Descriptors\\ShapeTemplateStrategy\\ShapeTemplateBindingStrategy.cs:line 157\r\n       at Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.<>c__DisplayClass26.<>c__DisplayClass28.<Discover>b__15(DisplayContext displayContext) in c:\\git\\mysite\\orchard\\src\\Orchard\\DisplayManagement\\Descriptors\\ShapeTemplateStrategy\\ShapeTemplateBindingStrategy.cs:line 133\r\n       at Orchard.DisplayManagement.Descriptors.ShapeAlterationBuilder.<>c__DisplayClass3.<>c__DisplayClass5.<BoundAs>b__2(DisplayContext displayContext) in c:\\git\\mysite\\orchard\\src\\Orchard\\DisplayManagement\\Descriptors\\ShapeAlterationBuilder.cs:line 56\r\n       at Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context) in c:\\git\\mysite\\orchard\\src\\Orchard\\DisplayManagement\\Implementation\\DefaultDisplayManager.cs:line 203\r\n       at Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Execute(DisplayContext context) in c:\\git\\mysite\\orchard\\src\\Orchard\\DisplayManagement\\Implementation\\DefaultDisplayManager.cs:line 99\r\n       at Orchard.DisplayManagement.Implementation.DisplayHelper.ShapeExecute(Object shape) in c:\\git\\mysite\\orchard\\src\\Orchard\\DisplayManagement\\Implementation\\DisplayHelper.cs:line 86\r\n       at Orchard.DisplayManagement.ShapeDisplay.Display(Object shape) in c:\\git\\mysite\\orchard\\src\\Orchard\\DisplayManagement\\ShapeDisplay.cs:line 39\r\n       at Orchard.Email.Services.SmtpMessageChannel.Process(IDictionary`2 parameters)\r\n       at Orchard.Messaging.Services.DefaultMessageService.Send(String type, IDictionary`2 parameters) in c:\\git\\mysite\\orchard\\src\\Orchard\\Messaging\\Services\\DefaultMessageService.cs:line 20\r\n       at Orchard.Events.DelegateHelper.<>c__DisplayClass2e`3.<BuildAction>b__2d(Object target, Object[] p) in c:\\git\\mysite\\orchard\\src\\Orchard\\Events\\DelegateHelper.cs:line 122\r\n       at Orchard.Events.DefaultOrchardEventBus.TryInvokeMethod(IEventHandler eventHandler, Type interfaceType, String messageName, String interfaceName, String methodName, IDictionary`2 arguments, IEnumerable& returnValue) in c:\\git\\mysite\\orchard\\src\\Orchard\\Events\\DefaultOrchardEventBus.cs:line 84\r\n       at Orchard.Events.DefaultOrchardEventBus.TryInvoke(IEventHandler eventHandler, String messageName, String interfaceName, String methodName, IDictionary`2 arguments, IEnumerable& returnValue) in c:\\git\\mysite\\orchard\\src\\Orchard\\Events\\DefaultOrchardEventBus.cs:line 70\r\n       at Orchard.Events.DefaultOrchardEventBus.TryNotifyHandler(IEventHandler eventHandler, String messageName, String interfaceName, String methodName, IDictionary`2 eventData, IEnumerable& returnValue) in c:\\git\\mysite\\orchard\\src\\Orchard\\Events\\DefaultOrchardEventBus.cs:line 53\r\n\r\nThe error seems to originate in Orchard.Environment.WorkContextExtensions from a null WorkContext in httpContext.Items:\r\n\r\n    public WorkContext GetContext(HttpContextBase httpContext) {\r\n        return httpContext.Items[_workContextKey] as WorkContext;\r\n    }\r\n\r\nThe call that passes the httpContext is in Orchard.WorkContextExtensions:\r\n\r\n    public static WorkContext GetWorkContext(this RequestContext requestContext) {\r\n\r\n        ...\r\n\r\n        return workContextAccessor.GetContext(requestContext.HttpContext);\r\n    }\r\n\r\nI propose the following change:\r\n\r\n    public static WorkContext GetWorkContext(this RequestContext requestContext) {\r\n        \r\n        ...\r\n        \r\n        if (requestContext.HttpContext != null && requestContext.HttpContext.IsBackgroundContext())\r\n        {\r\n            return workContextAccessor.GetContext();\r\n        }\r\n        else\r\n        {\r\n            return workContextAccessor.GetContext(requestContext.HttpContext);\r\n        }\r\n    }"},"comment":{"url":"https://api.github.com/repos/OrchardCMS/Orchard/issues/comments/156716535","html_url":"https://github.com/OrchardCMS/Orchard/issues/6033#issuecomment-156716535","issue_url":"https://api.github.com/repos/OrchardCMS/Orchard/issues/6033","id":156716535,"user":{"login":"bmingles","id":1900643,"avatar_url":"https://avatars.githubusercontent.com/u/1900643?v=3","gravatar_id":"","url":"https://api.github.com/users/bmingles","html_url":"https://github.com/bmingles","followers_url":"https://api.github.com/users/bmingles/followers","following_url":"https://api.github.com/users/bmingles/following{/other_user}","gists_url":"https://api.github.com/users/bmingles/gists{/gist_id}","starred_url":"https://api.github.com/users/bmingles/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bmingles/subscriptions","organizations_url":"https://api.github.com/users/bmingles/orgs","repos_url":"https://api.github.com/users/bmingles/repos","events_url":"https://api.github.com/users/bmingles/events{/privacy}","received_events_url":"https://api.github.com/users/bmingles/received_events","type":"User","site_admin":false},"created_at":"2015-11-14T16:18:24Z","updated_at":"2015-11-14T16:18:24Z","body":"Nevermind, I see the change to WorkContextAccessor. I'll check it out."}}