Scope problem when testing services at startup
If you turn on Scope Validation for DI, you will get an error when IdentityServer starts. This is because the services are tested at the root DI context. See https://github.com/IdentityServer/IdentityServer4/blob/ff5ca5c992a4001d76af33e42861fb1383ee2c11/src/IdentityServer4/Configuration/IdentityServerApplicationBuilderExtensions.cs#L35-L37
To resolve, create a scope before running the tests and use it on the tests:
using (var scope = app.ApplicationServices.CreateScope())
{
// run tests using scope
}
Issue / Steps to reproduce the problem
On .NET Core 1.1, at the ConfigureServices method, change the method signature to return an IServiceProvider and at the end enable scope validation, like so:
return services.BuildServiceProvider(validateScopes: true);
Relevant parts of the log file
fail: IdentityManager.WebApp.Startup[0]
System.InvalidOperationException: Cannot resolve scoped service 'IdentityServer4.Stores.IClientStore' from root provider.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, ServiceProvider serviceProvider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.AspNetCore.Builder.IdentityServerApplicationBuilderExtensions.TestService(IApplicationBuilder app, Type service, ILogger logger, String message, Boolean doThrow)
at Microsoft.AspNetCore.Builder.IdentityServerApplicationBuilderExtensions.Validate(IApplicationBuilder app)
at Microsoft.AspNetCore.Builder.IdentityServerApplicationBuilderExtensions.UseIdentityServer(IApplicationBuilder app)
at IdentityManager.WebApp.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in C:\p\foo\Startup.cs:line 350
At this log, line 350 is the call to app.UseIdentityServer();.
Tested on versions:
"IdentityServer4": "1.1.0" "IdentityServer4.EntityFramework": "1.0.0" "IdentityServer4.AspNetIdentity": "1.0.0"
https://github.com/IdentityServer/IdentityServer4/pull/825 🎉
I merged the PR - thanks for reporting!
@leastprivilege Cool! Would you know when this will be released?
In the he next weeks. Feel free to use the myget in the meantime.
@giggio You should take in mind that the validate scope functionality currently contains a bug that is not rolled out yet. https://github.com/aspnet/DependencyInjection/pull/470
We shipped 1.2.0 yesterday.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.