RazorHtmlEmails
RazorHtmlEmails copied to clipboard
.net 6 core compatibility
Hi! I really love the idea to use Razor in console applications or services, but it requires couple updates to make it work with .net 6
// It's required by IOC
var diagnosticListener = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton(diagnosticListener);
services.AddSingleton<DiagnosticSource>(diagnosticListener);
// Here we need to register assembly to make razor find compiled views
services.AddRazorPages()
.AddApplicationPart(Assembly.GetExecutingAssembly())
.AddRazorRuntimeCompilation();
// Now we need to add IWebHostEnvironment with custom implementation as default one is internal.
// Implementation is just bunch of properties without any additional logic.
var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
services.AddSingleton<Microsoft.AspNetCore.Hosting.IWebHostEnvironment>(new HostingEnvironment
{
ApplicationName = Assembly.GetEntryAssembly().GetName().Name,
WebRootFileProvider = fileProvider,
ContentRootFileProvider = fileProvider
});