RazorLight: System.UriFormatException: Invalid URI: The hostname could not be parsed
Describe the bug RazorLight works perfectly on my laptop (Windows). It also works perfectly at Azure Functions (Windows) (service functions to do various tasks)
However I have issues at Azure Application Service (Linux): (main application backend)
- Versions 2.0.0-rc.1 - 2.0.0-rc.3 works well without issues!
- Versions 2.0.0-rc.4, 2.0.0-rc.6 and 2.0.0 raise following exception:
Invalid URI: The hostname could not be parsed.
Stack Trace:
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString)
at System.UriBuilder..ctor(String uri)
at RazorLight.Compilation.DefaultAssemblyDirectoryFormatter.GetAssemblyDirectory(Assembly assembly)
at RazorLight.Compilation.DefaultMetadataReferenceManager.<Resolve>b__12_2(Assembly p)
at System.Linq.Enumerable.SelectArrayIterator`2.ToList()
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
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.CompileAndEmitAsync(RazorLightProjectItem projectItem)
at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
at RazorLight.EngineHandler.CompileTemplateAsync(String key)
at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag)
at ... RazorEngineService.cs:line 90
To Reproduce Create an account at Azure (perhaps it allows to do that using a free tier) Create a new Application Service and select Linux as OS:

I use following source codes (cleaned from logging stuff and unnecessary methods)
public class RazorEngineService : IHtmlTemplateRendererService
{
private static readonly IRazorLightEngine EngineService;
static RazorEngineService()
{
EngineService = new RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(typeof(RazorEngineService))
.SetOperatingAssembly(typeof(RazorEngineService).Assembly)
.UseMemoryCachingProvider()
.Build();
}
public async Task<string> GenerateCodeAsync<T>(string path, T model, string key)
{
var cacheResult = EngineService.Handler.Cache.RetrieveTemplate(key);
if (cacheResult.Success)
{
// report is already compiled, render using a cache
var templatePage = cacheResult.Template.TemplatePageFactory();
return await EngineService.RenderTemplateAsync(templatePage, model);
}
return await EngineService.CompileRenderAsync(path, model);
}
}
...
services.AddScoped<IHtmlTemplateRendererService, RazorEngineService>();
Expected behavior It shouldn’t fail 😃 It works well on 2.0.0-rc.3 and earlier. It doesn’t work after that
Information (please complete the following information):
- OS: Unix 4/5
- Platform: .NET Core 3.1
- RazorLight version: 2.0.0 (final)
- Are you using the OFFICIAL RazorLight package? Yes
- Rider 2022.1.1
Production environment (gathered from Azure Kudu):
OS version: Unix 5.4.0.1063
64 bit system: True
64 bit process: True
CLR version: 3.1.22
Development environment (gathered from Azure Kudu):
OS version: Unix 4.15.0.162
64 bit system: True
64 bit process: True
CLR version: 3.1.22
Additional context For now, I have to use 2.0.0-rc.3 until this issue is resolved in the latest RazorLight package I can test any internal builds at the Azure development environment - just let me know 😃
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 26 (1 by maintainers)
Commits related to this issue
- Reproduce #481 — committed to toddams/RazorLight by jzabroski 2 years ago
- Reproduce #481 — committed to toddams/RazorLight by jzabroski 2 years ago
- Proposed fix for #481 — committed to toddams/RazorLight by jzabroski 2 years ago
- fix(Templating.RazorLight): Downgraded RazorLight package to 2.0.0-rc3 to avoid bug described by https://github.com/toddams/RazorLight/issues/481 — committed to neuroglia-io/framework by cdavernas 2 years ago
- Fix System.UriFormatException in `DefaultAssemblyDirectoryFormatter`. Rename to `IAssemblyPathFormatter` as it returns the path to the assembly, not its directory. Fixes #481 — committed to Xtansia/RazorLight by Xtansia 2 years ago
- Fix System.UriFormatException in `DefaultAssemblyDirectoryFormatter` (#489) * Fix System.UriFormatException in `DefaultAssemblyDirectoryFormatter`. Rename to `IAssemblyPathFormatter` as it returns... — committed to toddams/RazorLight by Xtansia 2 years ago
Update - I had to do an emergency hotfix at my day job yesterday, so promise to fix this yesterday was broken. I have not forgotten about it.
Just to add some extra context onto this, the reason 2.0.0-rc.3 is working perfectly for people even though in theory it contains this commit https://github.com/toddams/RazorLight/commit/71383e4b06aa6b3ab27d900977ee6524d73f3024 which changes from
Assembly.CodeBasetoAssembly.Location, Is that 2.0.0-rc.3 contains the same version of the assemblies as 2.0.0-rc.2.Using a script like this:
Outputs the following, note the same checksums for rc.2 and rc.3:
And downloading/extracting the rc.3 package and looking at the assembly in dotpeek:
Yes, use 2.0.0-rc.3, as mentioned in https://github.com/toddams/RazorLight/issues/481#issuecomment-1134234343
Thank @Xtansia not me. I just am the messenger of his good deeds.
@jzabroski Thank you very much
Verified at the following platforms:
Azure Web Service: .NET 6 + Unix Azure Web Service: .NET Core 3.1 + Unix Azure Functions: .NET 6 + Windows
Works like a charm!
Anyone found a workaround to this issue ?
I will fix this today
@jzabroski Thank you John for keeping assisting!
For some unknown reason, both PassthroughAssemblyDirectoryFormatters do not work for me. I’ve tried to register them in Startup.cs, so the place should be the correct one
I’ve opened RazorLight sources (master branch), my Rider IDE doesn’t show usages of a constructor with IAssemblyDirectoryFormatter
My application never goes to that constructor, it ignores my customized formatter. A debugger stops in an original formatter implementation
Well, on Monday I will try to deploy customized builds of RazorLight to the Azure environment and try to find some workarounds