I am using Simple Injector in my ASP.NET WEb API project to dependency injection and OAuth for authentication. For that, I need to resolve an interface inside the GrantResourceOwnerCredentials
method, like this:
using (IBusiness business = Injector.Container.GetInstance<IBusiness>())
{
}
But when it passes in that point, it shows me this error:
The Business is registered as 'Async Scoped' lifestyle, but the instance is requested outside the context of an active (Async Scoped) scope.
And I am configuring my container with this singleton method:
public class Injector
{
private static Container container;
public static Container Container
{
get
{
if (container == null)
{
container = new Container();
container.Options.DefaultLifestyle = Lifestyle.Scoped;
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
}
return container;
}
}
public static TInstance GetInstance<TInstance>() where TInstance : class
{
return Container.GetInstance<TInstance>();
}
}
I am registering the dependencies with this code:
Injector.Container.Register<IBusiness, Business>(Lifestyle.Scoped);