dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.43k stars 10.01k forks source link

Blazor Web App - IIS - App shutdown on refresh - HTTP Error 503.0 - Server has been shutdown #57707

Open kuldeepcis-lab opened 1 month ago

kuldeepcis-lab commented 1 month ago

Is there an existing issue for this?

Describe the bug

I'm using the .NET 8 Blazor web app template, which includes both a client and server project. In the server project, I have an API controller that I call using HttpClient. When running the application on Kestrel (using commands like dotnet run or dotnet projectassembly.dll), everything works as expected. However, when I deploy the application to either a local or remote IIS, or run it via Visual Studio on IIS, the site starts but fails after requesting the Web API controllers. It returns a 503 error for all requests, and the application becomes unresponsive with no indication of what went wrong.

Expected Behavior

The application should work smoothly and get data from database on API calls and don't shut down on refresh or Api calling.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Go to repository and clone BlazorWebappOidc and Open the application in Visual Studio.
  2. Add an IIS Settings to launchsettings.json to run the application on IIS locally then Run "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:46294", "sslPort": 44381 } },"profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } }
  3. Add an API controller to server project and add a couple of method to get data from database.
  4. Register HTTPClient at client side project to call the API.
  5. lets run the project on the IIS profile and try to fetch data via method we have added into API controller this will return response Error 503 Shut down or just refresh the page it will show the error.

Exceptions (if any)

No response

.NET Version

DOTNET 8

Anything else?

For reference What I am getting on the browser please see the images.

Screenshot_15 Screenshot_16

cc: @guardrex https://github.com/dotnet/blazor-samples/issues/348

javiercn commented 1 month ago

@kuldeepcis-lab thanks for contacting us.

If you register a custom middleware at the beginning of the pipeline and put a breakpoint on it, are you able to hit it when you make the API call?

app.Use((ctx,nxt) => 
{
   return nxt();
});
kuldeepcis-lab commented 1 month ago

@kuldeepcis-lab thanks for contacting us.

If you register a custom middleware at the beginning of the pipeline and put a breakpoint on it, are you able to hit it when you make the API call?

app.Use((ctx,nxt) => 
{
   return nxt();
});

I had tried it and only once breakpoint hit when application start for the first time then didn't hit the breakpoint on API Call. please see the below images :-

Screenshot_17 Screenshot_18

javiercn commented 1 month ago

@kuldeepcis-lab thanks for the additional details.

Your breakpoint is in the wrong place. It should be inside the function, not at the place it's defined. (It should be on line 229).

kuldeepcis-lab commented 1 month ago

Okay, I will put the breakpoint at line 229 and test it again.

kuldeepcis-lab commented 1 month ago

I have tried the breakpoint inside of middleware but called only once when the application started then never hit.

javiercn commented 1 month ago

@kuldeepcis-lab thanks for the additional details.

Where are you issuing the HttpClient call from. Is it during SSR, is the app running on WebAssembly or is it Interactive Server.

kuldeepcis-lab commented 1 month ago

@javiercn thanks for the inspection. I am using Interactive Server mode and making the HttpClient call from the Client-side razor component to get data from an API controller.

image Screenshot_19

javiercn commented 1 month ago

@kuldeepcis-lab thanks for the additional details.

What's the base address for your HTTP Client instance? Is the API hosted in the same server as the application?

kuldeepcis-lab commented 1 month ago

Hi @javiercn This is the Base address https://localhost:44328 and APIs are hosted on the same server.

javiercn commented 1 month ago

@kuldeepcis-lab thanks for the additional details.

I'm going to route this to the Server folks, as this doesn't seem necessarily Blazor related.

However, I would suggest you avoid going through the indirection of calling your own server via HttpClient. Your Blazor code is already running on the server and should be able to access the services directly (in fact, it's one of the key advantages of the Server model).

By using an HttpClient + API combo you are incurring in extra unnecessary costs and potential problems like port exhaustion that you'll only discover when you deploy at scale.