cloudscribe/Announcements

recent breaking change in email notifications

Opened this issue · 0 comments

There were changes in the way email messages are generated using razor that may impact your project.

Specifically the message rendering now happens on a background thread where there is no httpcontext. This change resulted in needed changes in the actual views. Previously we were injecting the SiteContext into the layout file used for the email messages and this no longer works because injecting that depends on httpcontext. Note that we still have access to SiteContext in the email templates but as part of the models we pass into our email razor views. Those views in turn forward the model property for SiteContext as ViewData["Site"] so it can be used in the layout file for putting the site info in the footer. Accessing the sitecontext now is like this:

<div>Copyright &copy; @DateTime.Now.Year - @((ViewData["Site"] as ISiteContext)?.SiteName ?? "") All rights reserved.</div>

The embedded views have been fixed but if you have local copies of:

  • Views/Shared/_LayoutEmail.cshtml
  • Views/Shared_LayoutEmailNotification.cshtml
  • Views/Shared/LayoutTextEmailNotification.cshtml

as well as any files in Views/Shared/EmailTemplates

You will get errors after updating to the latest nugets when sending messages such as for email confirmation.

You may also need to check theme specific view folders for any of those files.

To solve the error get the latest copies of those views from here or if you have not customized those views at all, just delete the local copies and you will fallback to using the embedded views.

Error Details

You will see a very long error stack trace that starts something like this, the important part is highlighted:

"fail: cloudscribe.Core.Web.Components.Messaging.SiteEmailMessageSender[0]
error sending account confirmation email: No service for type 'cloudscribe.Core.Models.SiteContext' has been registered. stacktrace: at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorPagePropertyActivator.<>cDisplayClass8_0.b1(ViewContext context)
at Microsoft.Extensions.Internal.PropertyActivator1.Activate(Object instance, TContext context) at ..."

update to this issue, just realized this morning that our project template for Visual Studio and for the dotnet new command were including the old version of email layout templates in the set of files for new projects which means email notification for new projects would be broken.
I just published updated project templates to solve that.

To clarify how to fix existing email layout files in case you have customized them

Remove this from the top:

@inject SiteContext Tenant

Search for "Tenant" and find this:

@(Tenant?.SiteName ?? "Sample")

and replace it with this:

@((ViewData["Site"] as ISiteContext)?.SiteName ?? "")

any other instances of Tenant that you may have added should be similarly replaced