Skip to content

Instantly share code, notes, and snippets.

@dandohotaru
Last active February 3, 2022 17:05
Show Gist options
  • Save dandohotaru/e96b8f20d81b6bc4b94b3a4d757f2d33 to your computer and use it in GitHub Desktop.
Save dandohotaru/e96b8f20d81b6bc4b94b3a4d757f2d33 to your computer and use it in GitHub Desktop.
services.descriptors
{
"results": [
{
"lifetime": "transient",
"contract": "MediatR.ISender"
},
{
"lifetime": "transient",
"contract": "MediatR.IPublisher"
},
{
"lifetime": "transient",
"contract": "MediatR.IMediator",
"implementation": "MediatR.Mediator"
},
{
"lifetime": "transient",
"contract": "MediatR.ServiceFactory"
},
{
"lifetime": "transient",
"contract": "MediatR.IPipelineBehavior`2",
"implementation": "MediatR.Pipeline.RequestPreProcessorBehavior`2"
},
{
"lifetime": "transient",
"contract": "MediatR.IPipelineBehavior`2",
"implementation": "MediatR.Pipeline.RequestPostProcessorBehavior`2"
},
{
"lifetime": "transient",
"contract": "MediatR.IPipelineBehavior`2",
"implementation": "MediatR.Pipeline.RequestExceptionActionProcessorBehavior`2"
},
{
"lifetime": "transient",
"contract": "MediatR.IPipelineBehavior`2",
"implementation": "MediatR.Pipeline.RequestExceptionProcessorBehavior`2"
},
]
}
[Route("api/diagnostics")]
[ApiController]
public class DiagnosticsController : ControllerBase
{
public DiagnosticsController(IOptions<DescriptorOptions> descriptors, ILogger<DiagnosticsController> logger)
{
Descriptors = descriptors ?? throw new ArgumentNullException(nameof(descriptors));
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
protected ILogger Logger { get; set; }
protected IOptions<DescriptorOptions> Descriptors { get; set; }
[Authorize]
[HttpGet("services")]
public async Task<IActionResult> Services(string text)
{
var results =
(
from service in Descriptors.Value.Services
let contract = service.ServiceType.FullName
let implementation = service.ImplementationType?.FullName
where text == null
|| contract != null && contract.ToLower().Contains(text.ToLower())
|| implementation != null && implementation.ToLower().Contains(text.ToLower())
select new
{
Lifetime = service.Lifetime,
Contract = contract,
Implementation = implementation,
}
).ToList();
var response = await Task.FromResult(new
{
Meta = new
{
Counter = results.Count
},
Results = results,
});
return Ok(response);
}
}
public static class Startup
{
public static IServiceCollection AttachDescriptors(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<DescriptorOptions>(options =>
{
options.Services = services;
});
return services;
}
public static IApplicationBuilder UseDescriptors(this IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsLocal() || env.IsDevelopment() || env.IsTest())
{
app.UseMiddleware<DescriptorMiddleware>();
}
return app;
}
}
public class DescriptorOptions
{
public IEnumerable<ServiceDescriptor> Services { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment