UriFormatException only on IIS [Solved]

Hi,

My project is with Radzen Blazor Studio + Blazor WebAssembly + ASP.NET Core Identity

I have a problem only when deploy on IIS server. After login where Index Page with service injected fails (no error show) web and If I configure in developtment mode in Production show bellow error:

# An unhandled exception occurred while processing the request
UriFormatException: Invalid URI: The hostname could not be parsed.
...
Program+<>c.<$>b__0_0(IServiceProvider sp) in Program.cs** **XXXXX.Server.Pages.Pages__Host.<ExecuteAsync>b__14_1() in _Host.cshtml

In Debug IIS Express and Dotnet works fine. Only fails with IIS Server.

The code that generate error is bellow (I'm sure because when delete this code not fails):

[Inject]
public MercatService MercatService { get; set; }

MercatService is autogenerated by Radzen Blazor Studio

The best is that if I use first another url like "web:2000/unauthorized" I have no problems in navegate with panelmenu. Directly "web:2000" no works.

Thanks in advance

Unfortunately I’ve never seen such error before and I don’t know what might cause it nor how to reproduce it.

Incredible, after days trying, I have the solution. Maybe it was because I access the Web Server by IP instead of name:

Delete/Comment next code autogenerated by Blazor Studio in Program.cs (Server)

//builder.Services.AddSingleton(sp =>
//{
// // Get the address that the app is currently running at
// var server = sp.GetRequiredService();
// var addressFeature = server.Features.Get();
// string baseAddress = addressFeature.Addresses.First();
// return new HttpClient { BaseAddress = new Uri(baseAddress) };
//});

Works in Debug VS and Production IIS.

Thank you anyway, Radzen Blazor Studio is awesome!

Well, this was the default template at some point from Microsoft however I see that it was changed to:

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

We will update our template as well.

UPDATE: Actually I spoke to soon - there is no such code in the server part of the hosted WebAssembly Program.cs - we will remove it completely.

1 Like