Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mads Justesen 9 posts 40 karma points
    Jun 19, 2023 @ 07:33
    Mads Justesen
    0

    Converting a razor view into a string for a Email in a RecurringHostedServiceBased

    Hej there!

    I'm currently building a newletter service for a client where we are sending out a email every day if any news was added to the site.

    I've made a service that I made based on this guide: https://ppolyzos.com/2016/09/09/asp-net-core-render-view-to-string/ and its working perfectly in the controller which is utilized when a new user is created to send them a confirmation mail, however it doesn't work in the RecurringHostedService.

    When I try to render the string using this method enter image description here I get the following error: enter image description here

    And sadly this is pretty much all I've got to go by.

    I've been unable to figure out why its failing and would really appreciate any help converting a razor view to a string in a recurringjob.

    I also seem to be missing something important in my knowledge about how the RecurringHostedService function and would love it if anyone could tell me why my solution doesn't work.

    Kind regards

    Mads

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jun 20, 2023 @ 08:18
    Marc Goodson
    0

    Hi Mads

    My guess would be it's failing because the recurring task runs on a background thread, and there is no HttpContext, and the Razor View engine thing, probably needs something from the HttpContext...

    this bit

    var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);

    The actionContext when not within the controller won't have the context of the web app to find the view...

    I'm wondering if you changed it to use GetView (https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.viewengines.iviewengine.getview?view=aspnetcore-6.0)

    var viewResult = _razorViewEngine.GetView(null, "~/Views/Emails/MyLovelyEmail.cshtml", false);

    passing in the path to your razor view, whether that would 'workaround' this or whether it would just hit the next issue of the HttpContext not being there on a background thread.

    (Other 'cheaty' workaround, is to have an APIController that contains an endpoint that will return the HTML and has a context, so your initial example just works, but in your background task you have to 'ping' the API in order to get the Html to send...)

    Anyway hopefully knowing the underlying issue helps you find a way!

    Used this: https://github.com/adoconnection/RazorEngineCore on a recent project, not sure if we had it in a background thread, but it's the sort of thing it might do.

    regards

    Marc

  • Mads Justesen 9 posts 40 karma points
    Jun 20, 2023 @ 22:33
    Mads Justesen
    0

    Hi Marc

    Thanks for the answer, tried all of your suggestions except for the Api and in the end I ended up taking the html and turning it into a string myself. Its a rather inefficient solution but it will do for now as I sadly have to turn my attention to other things.

    I hope that I'll get some time in the future to return to this a try and fix it.

    Once again thanks for the assistence.

    Kind regards

    Mads

Please Sign in or register to post replies

Write your reply to:

Draft