aspnet / JavaScriptServices

[Archived] This repository has been archived
Apache License 2.0
3.03k stars 518 forks source link

Angular-CLI-Based-Template: Service injection doesnt work with SSR - Can't resolve all parameters for e: (?, [object Object]) #1408

Closed naveedahmed1 closed 6 years ago

naveedahmed1 commented 6 years ago

Providers in the app aren't being resolved during SSR. It works perfectly with no-SSR but when we enable SSR it through below error:

NodeInvocationException: Can't resolve all parameters for MyService: (?, ?, ?, ?, [object Object]).

Its the same issue as reported by @asadsahi https://github.com/aspnet/JavaScriptServices/issues/1288#issuecomment-347213170

Below is the log file:

Hosting environment: Production
Content root path: C:\inetpub\wwwroot
Now listening on: http://localhost:28163
Application started. Press Ctrl+C to shut down.
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An unhandled exception has occurred: Can't resolve all parameters for e: (?, [object Object]).
      Error: Can't resolve all parameters for e: (?, [object Object]).
    at u (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:322479)
    at e.Nw2y.e._getDependenciesMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:496723)
    at e.Nw2y.e._getTypeMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:494503)
    at e.Nw2y.e.getNonNormalizedDirectiveMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:487640)
    at e.Nw2y.e._getEntryComponentMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498828)
    at C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498583
    at Array.forEach (<anonymous>)
    at e.Nw2y.e._getEntryComponentsFromProvider (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498555)
    at C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:497797
    at Array.forEach (<anonymous>)
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: Can't resolve all parameters for e: (?, [object Object]).
Error: Can't resolve all parameters for e: (?, [object Object]).
    at u (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:322479)
    at e.Nw2y.e._getDependenciesMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:496723)
    at e.Nw2y.e._getTypeMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:494503)
    at e.Nw2y.e.getNonNormalizedDirectiveMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:487640)
    at e.Nw2y.e._getEntryComponentMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498828)
    at C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498583
    at Array.forEach (<anonymous>)
    at e.Nw2y.e._getEntryComponentsFromProvider (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498555)
    at C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:497797
    at Array.forEach (<anonymous>)
   at Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance.<InvokeExportAsync>d__7`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance.<InvokeExportAsync>d__13`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.<InvokeExportWithPossibleRetryAsync>d__10`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.<InvokeExportWithPossibleRetryAsync>d__10`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.SpaPrerenderingExtensions.<>c__DisplayClass0_0.<<UseSpaPrerendering>b__1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An exception was thrown attempting to execute the error handler.
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: Can't resolve all parameters for e: (?, [object Object]).
Error: Can't resolve all parameters for e: (?, [object Object]).
    at u (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:322479)
    at e.Nw2y.e._getDependenciesMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:496723)
    at e.Nw2y.e._getTypeMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:494503)
    at e.Nw2y.e.getNonNormalizedDirectiveMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:487640)
    at e.Nw2y.e._getEntryComponentMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498828)
    at C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498583
    at Array.forEach (<anonymous>)
    at e.Nw2y.e._getEntryComponentsFromProvider (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498555)
    at C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:497797
    at Array.forEach (<anonymous>)
   at Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance.<InvokeExportAsync>d__7`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance.<InvokeExportAsync>d__13`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.<InvokeExportWithPossibleRetryAsync>d__10`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.<InvokeExportWithPossibleRetryAsync>d__10`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.SpaPrerenderingExtensions.<>c__DisplayClass0_0.<<UseSpaPrerendering>b__1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HL9GUBPQSO55", Request id "0HL9GUBPQSO55:00000001": An unhandled exception was thrown by the application.
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: Can't resolve all parameters for e: (?, [object Object]).
Error: Can't resolve all parameters for e: (?, [object Object]).
    at u (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:322479)
    at e.Nw2y.e._getDependenciesMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:496723)
    at e.Nw2y.e._getTypeMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:494503)
    at e.Nw2y.e.getNonNormalizedDirectiveMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:487640)
    at e.Nw2y.e._getEntryComponentMetadata (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498828)
    at C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498583
    at Array.forEach (<anonymous>)
    at e.Nw2y.e._getEntryComponentsFromProvider (C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:498555)
    at C:\inetpub\wwwroot\ClientApp\dist-server\main.bundle.js:1:497797
    at Array.forEach (<anonymous>)
   at Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance.<InvokeExportAsync>d__7`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance.<InvokeExportAsync>d__13`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.<InvokeExportWithPossibleRetryAsync>d__10`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.<InvokeExportWithPossibleRetryAsync>d__10`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.SpaPrerenderingExtensions.<>c__DisplayClass0_0.<<UseSpaPrerendering>b__1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.<Invoke>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.<ProcessRequestsAsync>d__2.MoveNext()
Application is shutting down...
asadsahi commented 6 years ago

@naveedahmed1 have you managed to reproduce this with default cli template? Is it possible to share the repo with this error as Steve request earlier. My reproduction was on my project which was fairly complicate perhaps.

I can definitely confirm that we we get this error and the issue is still unidentified. @SteveSandersonMS as I mentioned here with some more experiments, I suspect this is something to do with how SSR setup. It might not be with this cli template specifically, but this is exactly we are unsure right now.

Update: I have put a question on SO few days ago to expand the help on this issue.

SteveSandersonMS commented 6 years ago

@naveedahmed1 @asadsahi Thanks for filing this as a separate issue, but I still can't do anything with it until you have a repro based on the default project template. It's unclear that there is something wrong with the default template, as opposed to being a general Angular usage issue or something wrong in a more complex project configuration.

I know this might be slightly frustrating, but to avoid confusing anyone else who's reading, I'll close this for now until there is a viable repro. Hope that's OK.

naveedahmed1 commented 6 years ago

@asadsahi can you update you packages.json and add --aot at the end of start and build commands and see if it works.

For small project, this seems to solve problem but for relatively large project throws below error:

An unhandled exception occurred while processing the request.

TaskCanceledException: A task was canceled.
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

InvalidOperationException: The NPM script 'build:ssr' timed out without indicating success. Output was: 

> MyProject@0.0.0 build:ssr D:\MyProject\ClientApp
> npm run build -- --app=ssr --output-hashing=media "--watch"
asadsahi commented 6 years ago

@naveedahmed1 with --aot flag added I get this:

image

without aot flag back to the same error.

naveedahmed1 commented 6 years ago

Are you sure this module is compatible with Angular 5 and universal?

asadsahi commented 6 years ago

@naveedahmed1 this seemed a little convincing, also I tried using this ng-bootstrap with out of the box angular cli spa template and that works too, which lead me believe that it should work without issues.

chrisvfabio commented 6 years ago

@naveedahmed1 Thank you!! It worked 💯. Finally getting angular/material to resolve in lazy-loaded modules

artemzakharov commented 6 years ago

@SteveSandersonMS After running into this error as well, I've put together a repository that replicates the issue. In my case, my home page needed to display a shared navigation bar component that was injected with a service responsible for shopping cart functions, and this service's parameters stopped resolving when I turned on SSR.

I've added the files based on my own project structure, which resembles the one recommended by Angular's style guide:

  1. Core functionality is compartmentalized into modules within the core-modules folder
  2. Core modules that contain app-wide singleton services (like my 'Cart' service) are imported into the core module using .forRoot(), and the core module is then imported into the app module to provide those services
  3. Individual core modules are imported into lazily loaded modules without .forRoot() when a component is being imported (like the shared navbar)

In my reproduction repo, the base template is modified such that it attempts to lazily load a component that contains the shared navigation bar component. The cart service is injected into this navbar component, causing the error. Commenting out the SSR code in Startup.cs causes no error to occur, confirming that it's something in the way the template handles SSR. Note: removing all injections from the faulty service causes the bug to stop appearing; however, this is obviously not a realistic solution (plus other services that injected services were fine), so I'm injecting the Router to ensure that the error appears.

You can find the repo in my account under 'ssr-error', or clone it straight from here:

https://github.com/artemzakharov/ssr-error.git

Just clone & run dotnet build, and you should be good to go.

SteveSandersonMS commented 6 years ago

My best guess is that Angular Universal doesn't currently work well in non-AoT builds. But it's hard to tell. I don't think that anything in ASP.NET or JavaScriptServices specifically has anything to do with the error you're getting. Reason:

To reproduce the error in a pure standalone Angular app:

That will give the Can't resolve all parameters for CartService: (?) error without involving any non-Angular app code. So either your app is set up incorrectly, or there's a bug in Angular.

But you'll find that it works OK if you do an AoT build:

My guess is therefore that AppServerModuleNgFactory is required for the lazy loading to work with SSR, but it relies on AoT, and it doesn't look like there's any non-AoT equivalent as far as I can tell.

So I'd recommend that you do one or all of the following:

artemzakharov commented 6 years ago

Good find, I'll follow up with the Angular Universal team. I've also confirmed that it's an issue specific to lazy loading - importing the (otherwise) lazily loaded component directly causes everything to be resolved successfully.

naveedahmed1 commented 6 years ago

@artemzakharov please do share the link if you open this issue with universal team.

artemzakharov commented 6 years ago

Just opened an issue with them, you can find the link above :+1: