Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RazorEngine parse throws Unable to compile

I'm trying to compile a razor file with this line

RazorEngine.Razor.Parse("Hello world");

But it just throws

base {System.Exception} = {"Unable to compile template. Source file 'C:\\Windows\\TEMP\\vlu4zahf.0.cs' could not be found\n\nOther compilation errors may have occurred. Check the Errors property for more information."}

And the error property looks like this:

[0] = {error CS2001: Source file 'C:\Windows\TEMP\vlu4zahf.0.cs' could not be found}
[1] = {warning CS2008: No source files specified}

So there is no good information.

I'm running .NET 4.0 and Razor Engine 3.2.0.0

Update 1

I have located the error to this line in the RazorEngine

Tuple.Create(
                compileResult.CompiledAssembly.GetType("CompiledRazorTemplates.Dynamic." + context.ClassName),
                compileResult.CompiledAssembly);
like image 345
Simon Edström Avatar asked Apr 04 '13 07:04

Simon Edström


2 Answers

This is most likely a permissions issue for the user account compiling the razor file. Make sure it has all but full control permission on C:\Windows\Temp

See here for background info: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/85f9b330-a938-4afe-a615-db83085e52d2/

like image 148
Adrian Avatar answered Oct 09 '22 16:10

Adrian


Adrian's answer guided me toward some research as to why c:\windows\temp would be needed/used in the first place. It turns out the use of this directory is directly related to the IIS app pool which I had created for the app. The pool I was using had the "Load User Profile" setting set to False. Turns out this attribute is set to False by default merely to retain backward-compatibility for apps designed to run on IIS 6 (before the load profile option existed) - and the compatibility issue is isolated to cases where such apps are leveraging the %temp% directory. Although it's a default, setting it to False is not the preferred practice according to Microsoft.

To the contrary, this setting should be set True, and doing so will provide your application with a %temp% directory under the user's profile that runs the app pool. Doing so mitigates the need to alter system volume permissions.

This setting is accessible in the AppPool's Advanced Settings in the IIS Manager.

This can also be accomplished in powershell (run as administrator) by running the following:

Import-Module WebAdministration
$appPoolName = "ReplaceWithYourAppPoolName"
$appPool = Get-Item IIS:\AppPools\$appPoolName
$appPool.processModel.loadUserProfile = $true
$appPool | Set-Item
like image 35
guerillapresident Avatar answered Oct 09 '22 16:10

guerillapresident