i am extending the identity server to use custom identity server implementation
public static void UseMongoDbForIdentityServer(this IApplicationBuilder app)
{
//Resolve Repository with ASP .NET Core DI help
var repository = (IRepository)app.ApplicationServices.GetService(typeof(IRepository));
//Resolve ASP .NET Core Identity with DI help
var userManager = (UserManager<ApplicationUser>)app.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));
// --- Configure Classes to ignore Extra Elements (e.g. _Id) when deserializing ---
ConfigureMongoDriver2IgnoreExtraElements();
var createdNewRepository = false;
...
}
this is how my startup file looks like
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ConfigurationOptions>(Configuration);
...
services.AddIdentityServer(
options =>
{
options.Events.RaiseSuccessEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseErrorEvents = true;
}
)
.AddMongoRepository()
.AddMongoDbForAspIdentity<ApplicationUser, IdentityRole>(Configuration)
.AddClients()
.AddIdentityApiResources()
.AddPersistedGrants()
.AddDeveloperSigningCredential();
...
}
And this is the error i am getting
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, ServiceProvider serviceProvider) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) IdentityServerSample.MongoDbStartup.UseMongoDbForIdentityServer(IApplicationBuilder app) in MongoDbStartup.cs
var userManager = (UserManager<ApplicationUser>)app.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));
IdentityServerSample.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in Startup.cs
app.UseMongoDbForIdentityServer();
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app) Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsLoggerStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder builder) Microsoft.ApplicationInsights.AspNetCore.ApplicationInsightsStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder app) Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+<>c__DisplayClass3_0.b__0(IApplicationBuilder app) Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder builder) Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
I know there are similar questions asked, but none seems to solve my problem
As such, it cannot resolve services that should only be consumed within service scopes. If you want to resolve a scoped service from the root provider, for example when you are consuming it from a singleton service, you should create a service scope first using the IServiceScopeFactory:
System.InvalidOperationException- Cannot resolve scoped service ” from root provider. The error seems could occur at multiple places depending on your specific implementation while using the services in your code base. As we know one can register the Services as Singleton or Scoped or Transient.
Try resolve IServiceScopeFactory first and then call CreateScope () to get the non root service provider. You will need to store the factory somewhere to be able to dispose it. Thank you for response. You got the point with IServiceScopeFactory :) Hi I'm having the same issue.
While scope service instances are created once per request it is often recommended they are resolved using a scoped instance of Application Builder. Then followed by using a service provider one can resolve the dependencies. You might need to consider below aspects also before resolving the issue. How a lifetime of service instances is registered.
You need a scope to resolve dependencies registered as scoped. To create it you can use the following:
using(var scope = app.ApplicationServices.CreateScope())
{
//Resolve ASP .NET Core Identity with DI help
var userManager = (UserManager<ApplicationUser>)scope.ServiceProvider.GetService(typeof(UserManager<ApplicationUser>));
// do you things here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With