| 123456789101112131415161718192021222324252627282930313233 |
- using System.Reflection;
- namespace Web.Api.Extensions;
- public static class EndpointExtensions
- {
- public static IServiceCollection AddEndpoints(this IServiceCollection services, Assembly assembly)
- {
- var endpointTypes = assembly.DefinedTypes.Where(type => type is {
- IsAbstract: false,
- IsInterface: false
- } && type.IsAssignableTo(typeof(IEndpoint))).Select(type => ServiceDescriptor.Transient(typeof(IEndpoint), type)).ToArray();
- foreach (var descriptor in endpointTypes)
- {
- services.Add(descriptor);
- }
- return services;
- }
- public static IApplicationBuilder MapEndpoints(this WebApplication app)
- {
- var endpoints = app.Services.GetRequiredService<IEnumerable<IEndpoint>>();
- foreach (var endpoint in endpoints)
- {
- endpoint.MapEndpoint(app);
- }
- return app;
- }
- }
|