Hello I went off this guide to integrate asp.net core Dependency Injection into MVC 5. Originally it worked fine using a default controller (Inherited from System.Web.Mvc.Controller class). But then I wanted the injected dependency to be available in an controller that inherited from System.Web.Http.ApiController. I'm 95% sure the problem is coming from this part of the code where all the controllers are added as services since I'm getting the same error as the guide says I will get without it that portion.
a missing method exception saying that your constructor doesn’t implement the default parameterless constructor
Startup.cs
public static class ServiceProviderExtensions
{
public static IServiceCollection AddControllersAsServices(this IServiceCollection services,
IEnumerable<Type> controllerTypes)
{
foreach (var type in controllerTypes)
{
services.AddTransient(type);
}
return services;
}
}
public partial class Startup
{
public void ConfigureServices(IServiceCollection services){
PackageScraperService pScraper = new PackageScraperService();
services.addSington<IPackageScraper>(pScraper);
// Problem Code
services.AddControllersAsServices(typeof(Startup).Assembly.GetExportedTypes()
.Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
.Where(t => typeof(IController).IsAssignableFrom(t)
|| t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)));
}
public void Configuration(IAppBuilder app){
var services = new ServiceCollection();
ConfigureServices(services)
var resolver = new DefaultDependencyResolver(services.BuildServiceProvider());
DependencyResolver.SetResolver(resolver);
}
}
I have looked at the results of
typeof(Startup).Assembly.GetExportedTypes()
.Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
.Where(t => typeof(IController).IsAssignableFrom(t)
|| t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
and it appears to correctly find my ApiController (ValuesController.cs)
I also changed my api controller to inherit from the Controller class and it worked fine.
Is there a simpler way to add a controller as a service? I've had a incredibly hard time finding documentation for this since I'm using MVC instead of Core.
IDependencyResolverexists for web api as well but in a different namespace. you then set the resolver for the GlobalConfiguration