Antaris / RazorEngine

Open source templating engine based on Microsoft's Razor parsing engine
http://antaris.github.io/RazorEngine
Other
2.14k stars 577 forks source link

Could not resolve template #527

Open tbconrad opened 6 years ago

tbconrad commented 6 years ago

I have a console application that uses RazorEngine to send email based on the run of the application. I am successfully able to send emails both in the development IDE and from running the console application via the executable. However, when I run the console application from a scheduled task I receive an error "Could not resolve template" on line, var emailHtmlBody = Engine.Razor.RunCompile("ResponseErrors.cshtml", null, model);

has anyone run into this?

     var config = new TemplateServiceConfiguration
     {
        TemplateManager = new ResolvePathTemplateManager(new[] {"EmailTemplates"}),
        DisableTempFileLocking = true
     };

     Engine.Razor = RazorEngineService.Create(config);
     var emailHtmlBody = Engine.Razor.RunCompile("ResponseErrors.cshtml", null, model);

UPDATE 1:

UPDATE 2: There is no RazorEngine... folder that was created in the user's temp folder.

csimone86 commented 6 years ago

Perhaps the scheduled task has no sufficient permission to read from folder where template is?

tbconrad commented 6 years ago

@csimone86 I thought that too but the account is a local administrator on the server and the templates are in the application folder.

tbconrad commented 6 years ago

So I was able to reproduce this. I created a sample console app and put it up on GitHub as RazorEngine_Test. Compile and run the project and it will run successfully from the debug or release folder. Copy all the files from the folder you ran it from and paste them into a new folder then run it again.

2018-07-26 12:57:22,991 INFO RazorEngine_Test.Logger Error Details: Could not resolve template ExceptionEmail.cshtml Stack: at RazorEngine.Templating.ResolvePathTemplateManager.ResolveFilePath(String name) at RazorEngine.Templating.ResolvePathTemplateManager.GetKey(String name, ResolveType resolveType, ITemplateKey context) at RazorEngine.Templating.RazorEngineService.GetKey(String cacheName, ResolveType resolveType, ITemplateKey context) at RazorEngine.Templating.DynamicWrapperService.GetKey(String name, ResolveType resolveType, ITemplateKey context) at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass16_0.b__0(TextWriter writer) at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter) at RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(IRazorEngineService service, String name, Type modelType, Object model, DynamicViewBag viewBag) at RazorEngine_Test.TemplatedErrorEmail.SendExceptionEmail(ExceptionEmailClass exceptionEmailClass) in C:_Development\personal\RazorEngine_Test\RazorEngine_Test\Classes\TemplatingErrorEmail.cs:line 23

tbconrad commented 6 years ago

UPDATE 3: It seems that the TemplateServiceConfiguration is not working as designed or the examples I worked off of are incorrect. I updated the test project with the 2 lines below and that solved my issue. I don't think this was the intended implementation method but it works. Posting because I know someone will run into the same issue.

var config = new TemplateServiceConfiguration
{
    TemplateManager = new ResolvePathTemplateManager(new[] {"EmailTemplates"}),
    DisableTempFileLocking = true
};

Engine.Razor = RazorEngineService.Create(config);

//new
string path = AppDomain.CurrentDomain.BaseDirectory;
string filePath = $"{path}EmailTemplates\\ExceptionEmail.cshtml";
//

var emailHtmlBody = Engine.Razor.RunCompile(filePath, null, model);
csimone86 commented 6 years ago

Perhaps are you in an Isolated (hosted) environment? I do not know exactly what I mean... I mean, is there a process that run you code?

Perhaps the base directory you see is the directory where the exe of the process that run your code reside... so you need to force it

tbconrad commented 6 years ago

@csimone86 No isolated or hosted environment, just an on-prem server trying to run a console application from a local administrator account