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.

.NET 6 - Angular: ng serve not running until requesting page

See original GitHub issue

When I run dotnet watch run, it doesn’t run the ng-serve command until I access https://localhost:5001 in my browser. With .NET 5 it used to start immediately. I have launchBrowser: false in my launchSettings.json.

.NET: 6.0.101 Angular: 12.2.2

(Sorry, I don’t know if this is a design-proposal).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jepperaskdkcommented, Jan 5, 2022

When I run dotnet watch run, it doesn’t run the ng-serve command until I access https://localhost:5001 in my browser. With .NET 5 it used to start immediately. I have launchBrowser: false in my launchSettings.json. .NET: 6.0.101 Angular: 12.2.2 (Sorry, I don’t know if this is a design-proposal).

I’m not sure, but it might solve the problem: Assuming you are running the app as a console application (Not using IIS Express) and the angular app resides inside ClientApp folder, you can check the following ways.

If you want to run the angular server independently add this to your Configure() method inside Startup.cs :

app.UseSpa(spa =>
        {

            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseProxyToSpaDevelopmentServer("http://localhost:4200"); // Use this instead to use the angular cli server
            }
        });

If you want to start both of the inside the aspnet core app add this to your Configure() method inside Startup.cs :

app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start");
                spa.Options.StartupTimeout = TimeSpan.FromSeconds(60); // Increase the timeout if angular app is taking longer to startup

            }
        });

Did you copy the code from here? It looks like .NET 5 or lower. https://stackoverflow.com/a/60598202/3717691

With .NET 6, there seems to no longer be a Startup.cs and the SPA is configured in .csproj, with these values:

        <SpaRoot>ClientApp\</SpaRoot>
        <SpaProxyServerUrl>https://localhost:44495</SpaProxyServerUrl>
        <SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>

Perhaps it can still be configured as you propose,

0reactions
javiercncommented, Oct 19, 2022

We did change this in 6.0 and we only start the proxy on first request. It is normally not an issue since the most common case is to open a page to the app, which triggers the launch. I have however filed an issue here to track the work needed to provide the option to start the proxy immediately instead of on first request.

I suspect the reason that we did it this way is because it made some async code to check if the server was already running easier to author.

We do not have plans to change it ourselves unless we receive significant feedback about it, but we would accept PRs as long as they roughly align to what is described in the referenced issue.

I am going to close this issue in favor of the other issue as it has all the context there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - dotnet 6 Run "ng serve" independently
Net core 6 and I need to run angular independently from MSBuild to avoid restarting the angular server every time I rebuild.
Read more >
ng serve
Option Description Value Type Default Value ‑‑hmr Enable hot module replacement. boolean false ‑‑host Host to listen on. string localhost ‑‑open Opens the url in default...
Read more >
Create an ASP.NET Core app with Angular in Visual Studio
In this article, you learn how to build an ASP.NET Core project to act as an API backend and an Angular project to...
Read more >
Angular TypeScript Tutorial in Visual Studio Code
Let's quickly run our Angular application by navigating to the new folder and typing ng serve to start the web server and open...
Read more >
How to Publish Angular with ASP.NET Core
In this article, we are going to cover how to publish Angular SPA production files by configuring ASP.NET Core's project file.
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