asp.net adding ApiController as service for depend

2019-07-09 22:04发布

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.

1条回答
祖国的老花朵
2楼-- · 2019-07-09 22:38

Update to include ApiControllers

services.AddControllersAsServices(typeof(Startup).Assembly.GetExportedTypes()
     .Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
     .Where(t => typeof(IController).IsAssignableFrom(t) 
        || typeof(IHttpController).IsAssignableFrom(t));

You will also need to set the dependency resolver for the Web API global configuration.

Update the DefaultDependencyResolver so that it can be used by both MVC and Web API. They share an interface by name but they belong to different namespaces.

public class DefaultDependencyResolver :
    System.Web.Http.Dependencies.IDependencyResolver,
    System.Web.Mvc.IDependencyResolver {

    private readonly IServiceProvider serviceProvider;

    public DefaultDependencyResolver(IServiceProvider serviceProvider) {
        this.serviceProvider = serviceProvider;
    }    

    public object GetService(Type serviceType) {
        return this.serviceProvider.GetService(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType) {
        return this.serviceProvider.GetServices(serviceType);
    }

    //Web API specific

    public System.Web.Http.Dependencies.IDependencyScope BeginScope() {
        return this;
    }

    public void Dispose() {
        // NO-OP, as the container is shared. 
    }
}

And in start up you set the resolver for both MVC and Web API.

public void Configuration(IAppBuilder app){
    var services = new ServiceCollection();    
    ConfigureServices(services);        
    var resolver = new DefaultDependencyResolver(services.BuildServiceProvider());
    DependencyResolver.SetResolver(resolver);//Set MVC
    GlobalConfiguration.Configuration.DependencyResolver = resolver; //Set for Web API
}
查看更多
登录 后发表回答