question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.UriFormatException: Invalid URI: The hostname could not be parsed

See original GitHub issue

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 😃

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:26 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
jzabroskicommented, Jun 1, 2022

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.

3reactions
Xtansiacommented, Jul 12, 2022

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.CodeBase to Assembly.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:

#!/usr/bin/env bash

set -e

versions=( 2.0.0-rc.1 2.0.0-rc.2 2.0.0-rc.3 2.0.0-rc.4 2.0.0-rc.6 2.0.0 )
for ver in "${versions[@]}"
do
    echo "${ver}"
    curl -SsL -o "${ver}.nupkg" "https://www.nuget.org/api/v2/package/RazorLight/${ver}"
    unzip -v "./${ver}.nupkg" | grep "RazorLight.dll"
done

Outputs the following, note the same checksums for rc.2 and rc.3:

2.0.0-rc.1
  124928  Defl:N    55590  56% 2020-11-17 14:55 ef20aac6  lib/net5.0/RazorLight.dll
  124928  Defl:N    55672  55% 2020-11-17 14:55 0458dac1  lib/netcoreapp3.1/RazorLight.dll
  124416  Defl:N    55396  56% 2020-11-17 14:55 9bcb50b7  lib/netstandard2.0/RazorLight.dll
2.0.0-rc.2
  124928  Defl:N    55654  56% 2020-11-18 16:15 50e395af  lib/net5.0/RazorLight.dll
  124928  Defl:N    55715  55% 2020-11-18 16:15 be42e02e  lib/netcoreapp3.1/RazorLight.dll
  124416  Defl:N    55451  55% 2020-11-18 16:15 dcf6bd62  lib/netstandard2.0/RazorLight.dll
2.0.0-rc.3
  124928  Defl:N    55654  56% 2020-11-18 16:15 50e395af  lib/net5.0/RazorLight.dll
  124928  Defl:N    55715  55% 2020-11-18 16:15 be42e02e  lib/netcoreapp3.1/RazorLight.dll
  124416  Defl:N    55451  55% 2020-11-18 16:15 dcf6bd62  lib/netstandard2.0/RazorLight.dll
2.0.0-rc.4
  133632  Defl:N    58810  56% 2021-12-08 02:16 d500f4dd  lib/net5.0/RazorLight.dll
  134144  Defl:N    58875  56% 2021-12-08 02:16 24e0b303  lib/netcoreapp3.1/RazorLight.dll
  133120  Defl:N    58623  56% 2021-12-08 02:16 d49e4aa3  lib/netstandard2.0/RazorLight.dll
2.0.0-rc.6
  134656  Defl:N    59196  56% 2022-02-17 00:39 104c7d3e  lib/net5.0/RazorLight.dll
  135168  Defl:N    59355  56% 2022-02-17 00:39 18a37c9d  lib/net6.0/RazorLight.dll
  134144  Defl:N    58878  56% 2022-02-17 00:39 4d59ee87  lib/netcoreapp3.1/RazorLight.dll
  133120  Defl:N    58611  56% 2022-02-17 00:39 e8674d98  lib/netstandard2.0/RazorLight.dll
2.0.0
  134656  Defl:N    59193  56% 2022-03-18 22:55 f707ce47  lib/net5.0/RazorLight.dll
  135168  Defl:N    59338  56% 2022-03-18 22:55 8fc8f9df  lib/net6.0/RazorLight.dll
  134144  Defl:N    58862  56% 2022-03-18 22:55 355e69b3  lib/netcoreapp3.1/RazorLight.dll
  133120  Defl:N    58606  56% 2022-03-18 22:55 b6c9f9af  lib/netstandard2.0/RazorLight.dll

And downloading/extracting the rc.3 package and looking at the assembly in dotpeek: image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid URI: The hostname could not be parsed
I looked into the urls making issue, they had '\' instead of '/' in urls , so in URI parsing it throws error....
Read more >
Invalid URI The hostname could not be parsed
What this error means and how to fix these errors. A family of Microsoft on-premises document management and storage systems.
Read more >
Invalid URI: The hostname could not be parsed - CAREWare
To resolve the "Invalid URI: The hostname could not be parsed" you can change the server hostname used in the client tier server...
Read more >
Error: Invalid URI: The hostname could not be parsed
This issue was observed as the result of either using incomplete URL references for images or images being referenced in Base64 encoding within...
Read more >
Error: System.UriFormatException: Invalid URI
UriFormatException : Invalid URI: The hostname could not be parsed. at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found