public MediatorEndpointApiDescriptionGroupCollectionProvider(MediatorEndpointCollections mediatorEndpointCollections) { _mediatorEndpointCollections = mediatorEndpointCollections; }
public static IServiceCollection AddMediatREndpoints(this IServiceCollection services, IEnumerable <Type> handlerTypes, bool addHandlersWithoutHttpMethodAttribute = false, Action <MediatorEndpointOptions> configureOptions = null) { var endpoints = new List <MediatorEndpoint>(); foreach (var handlerType in handlerTypes) { var requestHandlerType = handlerType.GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequestHandler <,>)); if (requestHandlerType == null) { throw new InvalidOperationException($"Type ({handlerType.FullName}) is not an IReqeustHandler<,>"); } var requestArguments = requestHandlerType.GetGenericArguments(); var requestType = requestArguments[0]; var responseType = requestArguments[1]; var requestMetadata = new MediatorEndpointMetadata(requestType, responseType); var attributes = handlerType.GetMethod("Handle").GetCustomAttributes(false); var httpAttributes = attributes.OfType <HttpMethodAttribute>().ToArray(); if (httpAttributes.Length == 0 && addHandlersWithoutHttpMethodAttribute) { var httpMethodMetadata = new HttpMethodMetadata(new[] { HttpMethods.Post }); var metadata = new List <object>(attributes); metadata.Add(httpMethodMetadata); metadata.Add(requestMetadata); endpoints.Add(new MediatorEndpoint { Metadata = metadata, HandlerType = requestHandlerType, RequestType = requestMetadata.RequestType, ResponseType = requestMetadata.ResponseType, Uri = requestMetadata.RequestType.Name }); } else { for (var i = 0; i < httpAttributes.Length; i++) { var httpAttribute = httpAttributes[i]; var httpMethodMetadata = new HttpMethodMetadata(httpAttribute.HttpMethods); string template; if (string.IsNullOrEmpty(httpAttribute.Template)) { template = "/" + requestType.Name; } else { template = httpAttribute.Template; } var metadata = new List <object>(attributes); metadata.Add(httpMethodMetadata); metadata.Add(requestMetadata); endpoints.Add(new MediatorEndpoint { Metadata = metadata, HandlerType = requestHandlerType, RequestType = requestMetadata.RequestType, ResponseType = requestMetadata.ResponseType, Uri = template }); } } } var mediatorEndpointCollections = new MediatorEndpointCollections(endpoints); services.AddSingleton(mediatorEndpointCollections); if (configureOptions != null) { services.Configure(configureOptions); } return(services); }