---
title: "How to resolve scoped service from root provider in ASP.NET Core 6"  
description: "How to resolve scoped service from root provider in ASP.NET Core 6"  
author: "Utpal Vishwas"  
published: 2023-11-07  
updated: 2023-11-08  
canonical: https://www.mindstick.com/forum/160455/how-to-resolve-scoped-service-from-root-provider-in-asp-dot-net-core-6  
category: ".net core"  
tags: ["asp.net core", ".net core", ".net core 6"]  
reading_time: 2 minutes  

---

# How to resolve scoped service from root provider in ASP.NET Core 6

How to [resolve](https://www.mindstick.com/forum/159427/windows-app-dll-not-found-resolve-library-issue) scoped [service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities) from [root](https://answers.mindstick.com/qa/49855/tell-me-what-is-root-port-in-spanning-tree-protocol) [provider](https://answers.mindstick.com/qa/92701/who-is-the-best-telecom-solution-provider) in [ASP.NET Core](https://www.mindstick.com/articles/12946/get-started-with-asp-dot-net-core-mvc-and-visual-studio) 6

## Replies

### Reply by Aryan Kumar

In [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) Core 6, you can resolve a scoped service from the root service provider by creating a scope and using it to request the scoped service. This is commonly done when you need to access a scoped service within a singleton or transient service. Here's how you can do it:

**Inject IServiceProvider**: First, inject the **IServiceProvider** into the constructor of the class where you need to resolve the scoped service:

```plaintext
using Microsoft.Extensions.DependencyInjection;

public class MySingletonService
{
    private readonly IServiceProvider _serviceProvider;

    public MySingletonService(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public void DoSomething()
    {
        using (var scope = _serviceProvider.CreateScope())
        {
            // Request the scoped service within the scope
            var scopedService = scope.ServiceProvider.GetRequiredService<IScopedService>();

            // Use the scoped service
            scopedService.PerformScopedTask();
        }
    }
}
```

In this example, we've injected the **IServiceProvider** into a singleton service, **MySingletonService**.

**Create a Scope**: Inside the method where you need to use the scoped service, create a scope using **CreateScope()** method of the service provider. This method returns an **IServiceScope** that allows you to request services within that scope.

**Request the Scoped Service**: Request the scoped service using **GetRequiredService<IScopedService>()** within the scope.

**Use the Scoped Service**: You can now use the scoped service as needed within the scope. Any services and dependencies required by the scoped service will be correctly injected.

This approach allows you to resolve a scoped service from the root service provider while ensuring that the scoped service is disposed of properly when the scope is disposed.

Remember to replace **IScopedService** with the actual interface and **PerformScopedTask** with the method or functionality provided by your scoped service. This pattern is particularly useful when you need to work with scoped services in the context of singleton or transient services.


---

Original Source: https://www.mindstick.com/forum/160455/how-to-resolve-scoped-service-from-root-provider-in-asp-dot-net-core-6

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
