ApiExplorer for WebAPI controllers in external assembly

橙三吉。 提交于 2019-12-14 03:20:27

问题


My WebApi controllers are located in an assembly (self-hosted OWIN application or ASP MVC application). Is it possible to use ApiExplorer form another application (that loads an assembly with WebApi controllers dynamically) to generate Web API documentation?


回答1:


ApiExplorer uses the GlobalConfiguration to determine the available ApiControllers. When you specify an external assembly you typically do this by replacing the IAssemblyResolver that WebApi is using. This can be done in Application_Start like this:

  protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        UnityConfig.RegisterComponents();

        GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new AssemblyResolver());

        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
    }

Here's the implementation of AssemblyResolver:

public class AssemblyResolver : System.Web.Http.Dispatcher.DefaultAssembliesResolver
    {
        public override ICollection<Assembly> GetAssemblies()
        {
            ICollection<Assembly> baseAssemblies = base.GetAssemblies();
            List<Assembly> assemblies = new List<Assembly>(baseAssemblies);

            var externalAssembly = typeof(MyApp.External).Assembly;
            assemblies.Add(externalAssembly);

            return baseAssemblies;
        }
    }

One might think that this is enough for ApiExplorer to pick it up but it is not. When ApiExplorer starts the GlobalConfiguration is passed to the HelpController. If you look carefully at the instance of GlobalConfiguration you will see that the changes you affected in your Application_Start are not there. So, to get ApiExplorer to pick up your external class you can just update the IAssemblyResolver like this:

public HelpController(HttpConfiguration config)
    {
        GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new AssemblyResolver());

        Configuration = config;
    }

There is probably a cleaner way to do this so you don't break DRY - but have not found it yet. Will update this post when I find it.



来源:https://stackoverflow.com/questions/32462455/apiexplorer-for-webapi-controllers-in-external-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!