aspnetcore: AngularCliMiddleware is not working with the new Angular CLI's build system
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Serving an Angular 17 SPA build with the new Angular CLI’s build system. Angular.io guide for esbuild
UseAngularCliServer(npmScript: start) is running into a timeout.
For example, consider the following code in Startup.cs:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSpa(
spa =>
{
spa.Options.SourcePath = "ClientApp";app.UseSpa(
spa =>
{
spa.Options.SourcePath = "ClientApp";
spa.UseAngularCliServer("start");
});
spa.UseAngularCliServer("start");
});
}
After a timeout the following error is thrown: “The Angular CLI process did not start listening for requests within the timeout period of 120 seconds. Check the log output for error information.”
So it does not support the new Angular build system.
Expected Behavior
The ASP .NET core Backend and the Angular SPI is served.
Steps To Reproduce
Create a new “Angular and ASP.NET Core” project from Visual Studio 2022 project templates. Change the angular.json of the newly created project as following: Using the browser-esbuild builder
The following is what you would typically find in angular.json for an application:
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
Changing the builder field is the only change you will need to make.
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser-esbuild",
Exceptions (if any)
TimeoutException: The Angular CLI process did not start listening for requests within the timeout period of 120 seconds. Check the log output for error information. Microsoft.AspNetCore.SpaServices.Extensions.Util.TaskTimeoutExtensions.WithTimeout<T>(Task<T> task, TimeSpan timeoutDelay, string message) Microsoft.AspNetCore.SpaServices.Extensions.Proxy.SpaProxy.PerformProxyRequest(HttpContext context, HttpClient httpClient, Task<Uri> baseUriTask, CancellationToken applicationStoppingToken, bool proxy404s) Microsoft.AspNetCore.Builder.SpaProxyingExtensions+<>c__DisplayClass2_0+<<UseProxyToSpaDevelopmentServer>b__0>d.MoveNext() Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
.NET Version
8.0.101
Anything else?
In Microsoft.AspNetCore.SpaServices.AngularCli.AngularCliMiddleware.cs is a condition for waiting until the output of the ng serve command is matching a specific regex pattern:
openBrowserLine = await scriptRunner.StdOut.WaitForMatch(
new Regex("open your browser on (http\\S+)", RegexOptions.None, RegexMatchTimeout));
But the output has changed due to the new Angular build system.
Old build system:
New build system:
So the regex pattern does not match any more. This pattern has to be fitted!
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 16
- Comments: 16
Finally I solved my problem. I don’t know if the best solution and my knowledge in .NET is 0. I had this code before I changed the build system:
And I realized that if I run my frontend and backend separately (by executing
npm startfrom my frontend), everything was working perfectly. So I was researching how to execute my process directly from .NET withoutspa.UseAngularCliServer(npmScript: "start")and I did usingProcessStartInfo.The final code looks like this:
This code works perfectly, and I didn’t need to use any other possible solutions involving modified NuGet packages.
I hope the solution helps others with the same problem!
my suggestion is to change the regex pattern to “(open your browser on (http\S+)|Local: http\S+)”
There has been an update to Microsoft.AspNetCore.SpaServices.Extensions package and the fix to this issue not in there. Any update on this would be appreciated.
@javiercn @jiayac, can you try to make sure some fix or official workaround is available for this? The development-time experience for the Microsoft-provided ASP.NET Angular template is not only obsolete, but broken with Angular 17+ projects (Angular 19 due next month) that use the new standard Vite/Esbuild. This is a significant issue.
Hi, I have the same problem. I changed to the new build system
applicationand now I receive the timeout after 120 seconds. I tried a lot of things but I couldn’t. And I didn’t find updated posts talking about this problem.Any help or anyone got a solution?
Thanks!
We are printing the expected text before the serve command:
"start": "echo open your browser on https://localhost:4200/ && ng serve"