locked
Explore ApiControllers from external assemblies RRS feed

  • Question

  • User2000122216 posted

    Hi. 

    What I want is to develop an application to explore ApiControllers in external assemblies in order to generate documentation (for example, Word or txt document).

    I know about Web Api Help Page but it is not what I need.

    I have an assembly:

    var assembly = Assembly.LoadFrom(name);

    I've tried 2 ways.

    Way 1: CustomAssemblyResolver and ApiExplorer

    public class CustomAssemblyResolver : DefaultAssembliesResolver
    {
    	private readonly Assembly assembly;
    
    	public CustomAssemblyResolver(Assembly assembly)
    	{
    		this.assembly = assembly;
    	}
    
    	public ICollection<Assembly> GetAssemblies()
    	{
    		return new[] { this.assembly };
    	}
    }
    
    private void GenerateAssemblyDescription(Assembly assembly)
    {
    	var httpConfiguration = new HttpConfiguration();
    httpConfiguration.Services.Replace(typeof(IDocumentationProvider), this.context.XmlDocumentationProvider); httpConfiguration.Services.Replace(typeof(IAssembliesResolver), new CustomAssemblyResolver(assembly)); var apiDescriptions = httpConfiguration.Services.GetApiExplorer().ApiDescriptions; // <-- Empty }

    But in this case I can't get apiDescriptions.

    Way 2: Reflexion and HttpActionDescriptor

    private void GenerateAssemblyDescription(Assembly assembly, IEnumerable<string> namespaceNames)
    {
    	// var httpConfiguration = new HttpConfiguration();
    	// httpConfiguration.Services.Replace(typeof(IDocumentationProvider), this.context.XmlDocumentationProvider);
    	// httpConfiguration.Services.Replace(typeof(IAssembliesResolver), new CustomAssemblyResolver(assembly));
    	// var apiDescriptions = httpConfiguration.Services.GetApiExplorer().ApiDescriptions;
    
    	foreach (var namespaceName in namespaceNames.OrderBy(x => x))
    	{
    		var types =
    			(from t in assembly.GetLoadableTypes()
    			 where t.IsClass && t.IsPublic 
    			 /* && typeof(ApiController).IsAssignableFrom(t)*/ 
    			 && t.Namespace == namespaceName
    			 select t).OrderBy(x => x.Name).ToList();
    
    		// Generate the description for each type
    		types.ForEach(this.GenerateDescription);
    	}
    }
    
    private void GenerateDescription(Type type)
    {
    	var httpConfiguration = new HttpConfiguration();
    
    	var apiControllerSelection = new ApiControllerActionSelector();
    	var apiDescriptor = new HttpControllerDescriptor(httpConfiguration, type.Name, type);
    	ILookup<string, HttpActionDescriptor> apiMappings = apiControllerSelection.GetActionMapping(apiDescriptor);
    
    	foreach (var maps in apiMappings)
    	{
    		foreach (HttpActionDescriptor httpActionDescriptor in maps)
    		{
    			this.GenerateDescription(httpActionDescriptor);
    		}
    	}
    }
    
    private void GenerateDescription(HttpActionDescriptor httpActionDescriptor)
    {
    	// RelativePath ???
    	// Route Values ???
    	// Request Body ???
    }

    But how to get RelativePath (/api/controller/action/id?parameters), ad other information that ApiExplorer contains?

    Thanks.

    Thursday, September 10, 2015 3:29 AM

Answers

  • User-84896714 posted

    Hi Vitone,

    // RelativePath ??? // Route Values ??? // Request Body ???

    They are runtime values. We can't get it from external assemblies.

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2015 4:32 AM