question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add controllers via the FeatureParts mechanism does not show up in UI.

See original GitHub issue

Hi,

NSWAG does not detect the controllers that I add using the FeatureParts mechanism. I am dynamically loading controllers form assemblies at run time. These dynamically loaded endpoints do not show up in the swagger web page; however the endpoints are accessible normally. I believe this is either a bug or I have missed something.

ConfigureServices in Startup.cs

public void ConfigureServices(IServiceCollection services)
{
	var loc = Assembly.GetExecutingAssembly().Location;
	var addinsDirectory = Path.Combine(Path.GetDirectoryName(loc), "addins");
	var appFeatureProvider = typeof(IApplicationFeatureProvider<ControllerFeature>);
	List<IApplicationFeatureProvider<ControllerFeature>> features = new List<IApplicationFeatureProvider<ControllerFeature>>();
	foreach (var file in Directory.EnumerateFiles(addinsDirectory, "*.dll"))
	{
		try
		{
			var assembly = Assembly.LoadFrom(file);
			IEnumerable<Type> externalTypes = assembly.GetTypes().Where(t => t.IsClass && !t.IsInterface);

			foreach (Type et in externalTypes)
			{
				object instance = Activator.CreateInstance(et);
				if(instance == null)
				{
					continue;
				}

				var castedInstance = instance as IApplicationFeatureProvider<ControllerFeature>;
				if (castedInstance == null)
				{
					continue;
				}
				features.Add(castedInstance);
			}

		}
		catch( Exception ex)
		{
			Debug.Fail(ex.ToString());
		}
	}

	services.AddMvc().ConfigureApplicationPartManager(p => features.ForEach(p.FeatureProviders.Add));

}

My controller and FeatureProvider in another assembly

[Route("api/[controller]")]
public class ValuesV2Controller : Controller
{
	// GET api/values
	[HttpGet]
	public IEnumerable<string> Get()
	{
		return new string[] { "value1", "value2" };
	}

	// GET api/values/5
	[HttpGet("{id}")]
	public string Get(int id)
	{
		return "value";
	}

	// POST api/values
	[HttpPost]
	public void Post([FromBody]string value)
	{
	}

	// PUT api/values/5
	[HttpPut("{id}")]
	public void Put(int id, [FromBody]string value)
	{
	}

	// DELETE api/values/5
	[HttpDelete("{id}")]
	public void Delete(int id)
	{
	}
}

public class GenericControllerFeatureProvider : IApplicationFeatureProvider<ControllerFeature>
{
	public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
	{
		feature.Controllers.Add(typeof(ValuesV2Controller).GetTypeInfo());

	}
}

Thre Configure methods in startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
	if (env.IsDevelopment())
	{
		app.UseDeveloperExceptionPage();
	}
	app.UseSwaggerUi(GetType().Assembly, new SwaggerUiSettings());

	app.UseMvc();
}	

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bitbonkcommented, Jan 24, 2019

I just ran into the same problem even though my Startup looks a bit different:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(
            o => o.Conventions.Add(new GenericControllerRouteConvention()))
        .ConfigureApplicationPartManager(
            m => m.FeatureProviders.Add(new GenericControllerFeatureProvider()))
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    services.AddSwaggerDocument();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    
    app.UseMvc();
    app.UseSwagger(); 
    app.UseSwaggerUi3();
}
0reactions
rwb196884commented, Jan 24, 2023

I have such a controller that is a generic type and my feature provider routes it in for a number of different types. It shows up in the UI but not in the json file that I generate using dotnet tool run swagger tofile.


Read more comments on GitHub >

github_iconTop Results From Across the Web

Add controllers via the FeatureParts mechanism does not ...
I am dynamically loading controllers form assemblies at run time. These dynamically loaded endpoints do not show up in the swagger web page; ......
Read more >
Solved: Adding New Controllers Not Showing in UI
I believe the issue was that inside the controller, the name of the class didn't in with the word "Controller". It ended with...
Read more >
Controller does not appear in swagger-ui.html
It happened to me that one of the controllers was not displayed. The problem was the kotlin class has not declared any package....
Read more >
Accessing Your Model's Data from a New Controller
Screenshot that shows the Solution Explorer window. The right click menu from the Controllers folder. In the Add Scaffold dialog box, click MVC ......
Read more >
Part 2, add a controller to an ASP.NET Core MVC app
In the Add New Scaffolded Item dialog box, select MVC Controller ... Browser window showing an app response of This is my default...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found