IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Scope problem when testing services at startup

Open giggio opened this issue 9 years ago • 7 comments

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"

giggio avatar Feb 21 '17 18:02 giggio

https://github.com/IdentityServer/IdentityServer4/pull/825 🎉

user1336 avatar Feb 21 '17 20:02 user1336

I merged the PR - thanks for reporting!

leastprivilege avatar Feb 22 '17 11:02 leastprivilege

@leastprivilege Cool! Would you know when this will be released?

giggio avatar Feb 24 '17 14:02 giggio

In the he next weeks. Feel free to use the myget in the meantime.

leastprivilege avatar Feb 25 '17 21:02 leastprivilege

@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

user1336 avatar Feb 27 '17 07:02 user1336

We shipped 1.2.0 yesterday.

leastprivilege avatar Feb 27 '17 09:02 leastprivilege

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.

lock[bot] avatar Jan 15 '20 01:01 lock[bot]