toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.51k stars 260 forks source link

System.IO.FileNotFoundException. Can't CompileRender when special charatecter is in path #341

Closed Basyras closed 4 years ago

Basyras commented 4 years ago

I am using RazorLight nuget package v. 2.0.0-beta7 (same problem in RazorLight.NetCore3 ) in my WPF ( .NET Framework 4.6.1 )application and i keep getting System.IO.FileNotFoundException when calling engine.CompileRenderAsync().

My project path is

C:\Honza\Prace\Programovani\C#\Bucek\Smlouvy\Smlouvy

After seeing this exception message: System.IO.FileNotFoundException: 'File C:\Honza\Prace\Programovani\C not found.' i tried build solution and moved bin/debug directory (moving the whole project worked too) to desktop directory then i started this application and i did not get this exception anymore. (Also i tried move only the .cshtml template file and i got the same exception)

My code:

private static IRazorLightEngine engine;
static TemplateFiller()
{
             templatesDirectory = Path.Combine(Directory.GetCurrentDirectory(), 
             @"Resources\SmlouvyBusinessResources\PdfBuilder");

            engine = new RazorLightEngineBuilder()
              .UseFileSystemProject(templatesDirectory)
              .UseMemoryCachingProvider()
              .Build();
}

public static string GetFamilyHtml(RazorContractModel model)
{
            CustomerModel Customer = model.Customer;
            string result = engine.CompileRenderAsync("PdfTemplateEmpty", model).GetAwaiter().GetResult();
}

Stack trace:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   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)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.EngineHandler.<CompileTemplateAsync>d__15.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.EngineHandler.<CompileRenderAsync>d__19`1.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at SmlouvyBusiness.Utilities.Controllers.TemplateFiller.GetFamilyHtml(RazorContractModel model) in C:\Honza\Prace\Programovani\C#\Bucek\Smlouvy\Smlouvy\DocumentWorks\Utilities\Controllers\TemplateFiller.cs:line 142
   at SmlouvyBusiness.Utilities.Controllers.TemplateFiller.CreataFamilyPDF(ContractModel model) in C:\Honza\Prace\Programovani\C#\Bucek\Smlouvy\Smlouvy\DocumentWorks\Utilities\Controllers\TemplateFiller.cs:line 102
   at SmlouvyWPF.Views.Dialogs.CreateContractDialog.<>c__DisplayClass23_0.<LoadPdfAsync>b__0() in C:\Honza\Prace\Programovani\C#\Bucek\Smlouvy\Smlouvy\Smlouvy\Views\Dialogs\CreateContractDialog.xaml.cs:line 136
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
Basyras commented 4 years ago

I could not reproduce same exception in .net core 3.1

Basyras commented 4 years ago

Closing this issue. To see detailed explanation with workaround see #355