Add controllers via the FeatureParts mechanism does not show up in UI.
See original GitHub issueHi,
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:
- Created 6 years ago
- Comments:7 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I just ran into the same problem even though my Startup looks a bit different:
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
.