Skip to content

Error in using Razor engine in Console app #295

@michaelsync

Description

@michaelsync

I wanted to use the Liquid template but I run into this issue #294
So I am trying with Razor. I have the following code to send the email with Razor template from .NET Core console app. (Not MVC or No DI)

 var model = new { Name = "LUKE", Numbers = new[] { "1", "2", "3" } };

            var template = @"    
                            Hi @Model.Name here is a list @foreach(var i in Model.Numbers) { @i }
                        ";


            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Host = smtpServer;
            client.Port = 25;

            FluentEmail.Core.Email.DefaultSender = new SmtpSender(client);            
            FluentEmail.Core.Email.DefaultRenderer = new RazorRenderer();

            await FluentEmail.Core.Email
                .From(sender)
                .To(reportingEmail)
                .Subject(subject)
                .UsingTemplate(template, model)
                .SendAsync();

The error message.

System.InvalidOperationException: Cannot find compilation library location for package 'System.Text.Json'
   at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List`1 assemblies)
   at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
   at RazorLight.Compilation.DefaultMetadataReferenceManager.<>c.<Resolve>b__12_1(CompilationLibrary library)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly, DependencyContext dependencyContext)
   at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly)
   at RazorLight.Compilation.RoslynCompilationService.EnsureOptions()
   at RazorLight.Compilation.RoslynCompilationService.get_ParseOptions()
   at RazorLight.Compilation.RoslynCompilationService.CreateSyntaxTree(SourceText sourceText)
   at RazorLight.Compilation.RoslynCompilationService.CreateCompilation(String compilationContent, String assemblyName)
   at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate)
   at RazorLight.Compilation.RazorTemplateCompiler.CompileAndEmit(RazorLightProjectItem projectItem)
   at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
--- End of stack trace from previous location where exception was thrown ---
   at RazorLight.EngineHandler.CompileTemplateAsync(String key)
   at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag)
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at FluentEmail.Razor.RazorRenderer.ParseAsync[T](String template, T model, Boolean isHtml)
   at FluentEmail.Razor.RazorRenderer.FluentEmail.Core.Interfaces.ITemplateRenderer.Parse[T](String template, T model, Boolean isHtml)
   at FluentEmail.Core.Email.UsingTemplate[T](String template, T model, Boolean isHtml)

I already have Newtonsoft.Json package but because of this error, I also installed "Install-Package System.Text.Json -Version 6.0.0" too. but still getting the same error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions