question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Cannot resolve scoped service" in Development environment

See original GitHub issue

Hi!

Environment: Windows 10 Visual studio 2019 Version 16.10.3 Asp.NET core 3.1

I have simple asp.net core app which launched with Kestrel. I am experimenting with HttpClientFactory and have caught an interesting behaviour. I have 2 services. First:

public interface IService1
{
	void Inc();
	string ToString();
}

public class Service1: IService1
{
	public int _count = 0;

	public Service1(HttpClient client, IService2 s2)
	{
		
	}

	public void Inc()
	{
		_count++;
	}

	public override string ToString()
	{
		return $"{nameof(Service1)}, count {_count}";
	}
}

Second:

public interface IService2
{
	void Inc();
	string ToString();
}

public class Service2 : IService2
{
	public int _count = 0;

	public Service2()
	{

	}

	public void Inc()
	{
		_count++;
	}

	public override string ToString()
	{
		return $"{nameof(Service2)}, count {_count}";
	}
}

I have registered them inside ConfigureServices:

services.AddScoped<IService2, Service2>();
services.AddHttpClient<IService1, Service1>();

And in Configure(…) method I am tryin call my Service1:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
	var s1 = app.ApplicationServices.GetRequiredService<IService1>();
	s1.Inc();

	s1 = app.ApplicationServices.GetRequiredService<IService1>();
	s1.Inc();

	Console.WriteLine(s1);
	...
}

If I start this code in Visual Studio (doesn’t matter in Debug or Release mode), exception is throwing: System.InvalidOperationException: ‘Cannot resolve scoped service ‘WebApplication1.IService2’ from root provider.’

If I start application with double click on exe file, application successfully starts. Difference was in “ASPNETCORE_ENVIRONMENT” variable. As I removed it from Visual Studio debug settings, the app was started successfully in Visual Studio as well.

Is this behaviour a bug or a feature?

Thank in advance!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
davidfowlcommented, Jul 26, 2021

@Vake93 is correct, it’s the performance impact.

2reactions
Vake93commented, Jul 26, 2021

Not 100% sure about this, but I think there is a performance impact when you validate the scopes when dependencies are resolved. So that is probably why it is skipped by default in the production environment. Also, since you would have tested the application in development before pushing it to production, you would catch the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot resolve scoped service from root provider .Net Core 2
What's happening there, is that the scope nesting isn't being validated; as in, it isn't checking, during runtime, if you have improper nesting ......
Read more >
Cannot resolve from root provider because it requires ...
The answer is Scope Validation: When the app runs in the Development environment and calls CreateDefaultBuilder to build the host, the default  ......
Read more >
Cannot resolve scoped service from root provider ASP.NET ...
Issue resolution for error -Cannot resolve scoped service from root provider ASP.NET Core. Understand the lifetime of service instances is registered, ...
Read more >
TestServer cannot resolve scoped service - Matt's work blog
According to this GitHub comment, this behaviour is by design, as scoped services are only supposed to be resolved within a scope.
Read more >
Cannot resolve scoped service 'Microsoft.Identity.Web. ...
When I try to instantiate a class within middleware which has one of the parameters/dependecy as ITokenAcquisition, I get the following ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found