Asp.net 3.0 IServiceProvider cannot inject

P

Paul Carlton

Guest
sigh This line in startup.cs:

services.AddScoped<IServiceProvider, ServiceProvider>();


Breaks my app, can't debug, just stops after saying that the build was successful. No errors. No exceptions. NOTHING. If I comment out this line, all works again and the app runs.

Why is that happening? The reason I want to inject IServiceProvider is because it is required by ActivatorUtilities and I'm using that in the BaseController to make it easy to validate like such:

public MyController(IServiceProvider serviceProvider) : base(serviceProvider) { }

[HttpPost]
public ActionResult Post(MyViewModel viewModel)
{
ValidateFor<MyValidator>(viewModel);

if (!IsValid)
{
... error stuff
}

return Ok("Success!");
}


And in the BaseController

protected IserviceProvider ServiceProvider { get; set; }

public BaseController(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}

public void ValidateFor<TValidator>(object instance) where TValidator : IValidator
{
// injected ServiceProvider here as a protected property of the base controller
var validator = ActivatorUtilities.CreateInstance<TValidator>(ServiceProvider);

var result = validator.Validate(instance);

if (result.IsValid)
{
return;
}

... process errors
}

Continue reading...
 
Top