Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Scope available on non-versioned controllers #596

Open
twenzel opened this issue Apr 20, 2021 · 4 comments
Open

No Scope available on non-versioned controllers #596

twenzel opened this issue Apr 20, 2021 · 4 comments

Comments

@twenzel
Copy link
Contributor

twenzel commented Apr 20, 2021

Given:
ASP.NET Core 5 API App using Windsor

When using Microsoft API versioning (Startup.cs line 29) the controller is only working when the request is using the versioned url "/api/v1/weatherforecast". When using the non-versioned url "/weatherforecast" services resolved inplace creates a InvalidOperationException "No Scope available"

This was working with ASP.NET Core 2.2.

AspCoreVersionedApis.zip

@ltines
Copy link
Contributor

ltines commented Jul 23, 2021

HI @twenzel
preferred way to resolve components in web code is to use IServiceProvider not container directly. Especially with scoped services. ASP.NET runtime contols the lifetime of the scope.

For your code:

public WeatherForecastController(ILogger<WeatherForecastController> logger, IUserService userService, IServiceProvider serviceProvider)
{
	_logger = logger;
	_userService = userService ?? throw new ArgumentNullException(nameof(userService));
	_serviceProvider = serviceProvider;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
	var service = _serviceProvider.GetRequiredService<IScopedService>();
	var service1 = _serviceProvider.GetRequiredService<ITransientService>();

	var rng = new Random();
	return Enumerable.Range(1, 5).Select(index => new WeatherForecast
	{
		Date = DateTime.Now.AddDays(index),
		TemperatureC = rng.Next(-20, 55),
		Summary = Summaries[rng.Next(Summaries.Length)]
	})
	.ToArray();
}

@StefanoVuerich
Copy link

StefanoVuerich commented Mar 4, 2022

Hi Guys,

@ltines

I'm experiencing the same issue as @twenzel . I have a POST request, then my code calls Container.BeginScope().
The scope seems to be ok, as I don't receive any exception.
When I try to resolve a scoped service I get error: System.InvalidOperationException: 'No scope available'.

If, in the non api versioned controller, I wrap my code in using(var scope = HttpContext.RequestServices.CreateScope()), then everything works fine.
But I want the scope to be created automatically for each non api versioned controller.

Do you have any suggestion?

Best regards

@ltines
Copy link
Contributor

ltines commented Mar 4, 2022

@StefanoVuerich ,

i am wondering why do you need to manually call Container.BeginScope()? The pipeline should start scope via CreateScope automatically

@StefanoVuerich
Copy link

@ltines ,

our old implementation was calling Container.Beginscope() to be able to resolve scoped services.

What is the piece of the pipeline you are referring when you say 'The pipeline should start scope via CreateScope automatically' ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants