I want to register an existing instance of a class using life style PerWebRequest on JwtTokenValidated event handler. I am using ASP.NET Core OWIN. I am using AspNetBoilerplate.
You cannot register an existing instance using a specific lifestyle:
⚠ Registering instance ignores lifestyle: When you register an existing instance, even if you specify a lifestyle it will be ignored. Also registering instance will set the implementation type for you, so if you try to do it manually, an exception will be thrown.
You can register a type with the equivalent of PerWebRequest
lifestyle:
IocManager.IocContainer.Register(Component.For<TestService>()
.LifestyleCustom<MsScopedLifestyleManager>());
Then inject (or resolve) TestService
in your event handler:
public MyEventHandler(TestService testService)
{
// ...
}
Alternative: Castle Windsor lifestyle in dotnet core web application
ASP.NET Core has it's own 'scoped' lifecycle, which is 'per request'. See it's documentation: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection
Usage example:
services.AddScoped<ICharacterRepository, CharacterRepository>();
You should do it inside
ConfigureServices
method inStartup
class.