asp.net将ApiController添加为依赖项注入服务

时间:2018-08-05 03:43:42

标签: c# asp.net asp.net-mvc asp.net-web-api

您好,我退出this guide并将asp.net核心依赖注入集成到MVC 5中。最初,它使用默认控制器(继承自System.Web.Mvc.Controller类)可以正常工作。但是后来我希望注入的依赖项在从System.Web.Http.ApiController继承的控制器中可用。我有95%的把握是问题出在代码的这一部分,所有的控制器都作为服务添加了,因为我遇到的错误与指南中所说的一样,如果没有该部分,我会得到的。

  

缺少方法异常,说明您的构造函数未实现默认的无参数构造函数

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);
  }
}

我查看了

的结果
typeof(Startup).Assembly.GetExportedTypes()
     .Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
     .Where(t => typeof(IController).IsAssignableFrom(t) 
        || t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))

它似乎可以正确找到我的ApiController(ValuesController.cs)

我还更改了我的api控制器,使其继承自Controller类,并且工作正常。

是否有更简单的方法将控制器添加为服务?自从我使用MVC代替Core以来,为此一直很难找到文档。

1 个答案:

答案 0 :(得分:5)

更新为包含ApiControllers

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

您还需要为Web API全局配置设置依赖性解析器。

更新DefaultDependencyResolver,以便MVC和Web API均可使用它。它们通过名称共享一个接口,但是它们属于不同的命名空间。

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. 
    }
}

然后在启动时为MVC和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
}