Blazor Server "Error: Invocation canceled due to the underlying connection being closed" when trying to change base path #58981
Labels
area-blazor
Includes: Blazor, Razor Components
Needs: Author Feedback
The author of this issue needs to respond in order for us to continue investigating this issue.
Motivation
I have a .NET 8 Blazor server app where I wish to change the base url for.
This is because I need to deploy my application to IIS and it will be locally hosted using a reverse proxy.
"Why do you need to change the base url for this?" I hear you asking.
Well eventually there should be multiple blazor applications that are all locally hosted and served via a reverse proxy on the same machine, and I distinguish these applications using their base path.
i.e. I have the following 2 apps:
^root/car(.*)
->http://localhost:8081{R:1}
^root/house(.*)
->http://localhost:8082{R:1}
The thing is that my static files are not served correctly if I don't adjust the base path of my application.
Static files would be retrieved via the root of the URL where the first html file loads in, which is localhost without a specified port (and thus will give back an error 404).
Say that for example the following is a snippet of my
App.razor
for my CarApp.Now let's say that the user calls
http://localhost/root/car
, what will happen?app.css
fromhttp://localhost/app.css
app.css
is hosted onhttp://localhost:8081/app.css
not onhttp://localhost/app.css
.A way to prevent this from happening would be to set another rewrite rule in iis to rewrite
http://localhost/app.css
(for example) tohttp://localhost:8081/app.css
.But what if we also want to load in our HouseApp, and this has its
app.css
link specified in the same way?Then we would get back the
app.css
from our CarApp since it would be rewritten to localhost:8081.My Solution
To try and circumvent this issue, I tried setting the base path for blazor server as specified in the manual.
After setting the
base
tag in myApp.razor
the static files can be retrieved from the specified base.Next to this I also set the pathbase using
app.UsePathBase
because some files will still look for the root of the url instead of taking on the base specified by thebase
html tag, andapp.UsePathBase
makes these files also be hosted on the specified path base (an example of such a file would be _framework/blazor.web.js).Lastly I also used
app.MapBlazorHub
as was explained in the documentation to make sure my signalr connection uses the right path.Using these steps would make it so static files are being served with the appropriate base path that I specified in these steps.
So where we the browser first tried to retrieve
app.css
viahttp://localhost/app.css
, it will now try to retrieve it viahttp://localhost/<newpathbase>/app.css
.Issue
When using the steps I wrote above, all the files and requests are correctly retrieved and executed (see screenshot).
But unfortunately, the signalR connection can't be made.
Even when testing this locally and without using IIS or a reverse proxy, I still face the same issue.
I also tried to visit the page via /root/subpath, but then I see the following:
At this point I have no idea what I'm doing wrong and how I can fix my issue, I also found many different issues vaguely relating to this, but that are maybe out of date or do not correctly fit my use case.
As an example I also created a test repo that demonstrates this issue.
The text was updated successfully, but these errors were encountered: