asyncapi / saunter

Saunter is a code-first AsyncAPI documentation generator for dotnet.
https://www.asyncapi.com/
MIT License
199 stars 56 forks source link

Inject dependencies into Filter classes ? #116

Closed benspeth closed 2 years ago

benspeth commented 3 years ago

Hello,

The need arises in my company to generate documentation for the async contracts used by our apps. I would like to use Saunter but a feature is missing in order to do that easily.

Here's a bit of context, we use a custom built Nuget to register contracts that are published and received through Service Bus (It's open source and available here). This Nuget is using IOptions pattern to handle its configuration and I would like to use those options to dynamically create channel and operation items. In order to do that, I would like to be able to inject said options into DocumentFilter, ChannelItemFilters and OperationFilters classes.

Is it doable ?

tl;dr : I would like to inject services into DocumentFilter, ChannelItemFilters and OperationFilters classes in order to generate documentation based on the internal state of my application.

m-wild commented 3 years ago

Not currently doable without some pretty ugly hacks (build the service provider manually during Startup)...

This should be possible to implement in a nice way, we just need a reference to the IApplicationBuilder.ApplicationServices which we can probably grab during the middleware registration (as this is already an extension on IApplicationBuilder).

I'll do some reading and see what the recommended approach is.

benspeth commented 3 years ago

Hello @tehmantra, any update on this ?

m-wild commented 3 years ago

Sorry, this one fell off my radar! I'll get something out over the weekend :)

m-wild commented 2 years ago

This is available in v0.8.0

services.AddAsyncApiSchemaGeneration(options =>
{
    options.AddOperationFilter<FooFilter>();
}

services.AddTransient<FooFilter>(); // remember to register the type too (not sure how to avoid this step)