| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using TeleFlow.Framework.Application; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Validates Telegram handler metadata against the final DI service provider so missing method-parameter |
| | | 8 | | /// services and custom filters fail before an update reaches the dispatcher hot path. |
| | | 9 | | /// </summary> |
| | | 10 | | internal sealed class TelegramRuntimeDependencyValidator : ITeleFlowRuntimeValidator |
| | | 11 | | { |
| | | 12 | | public void Validate(IServiceProvider services) |
| | | 13 | | { |
| | 114 | 14 | | ArgumentNullException.ThrowIfNull(services); |
| | | 15 | | |
| | 114 | 16 | | var serviceAvailability = services.GetService<IServiceProviderIsService>(); |
| | | 17 | | |
| | 114 | 18 | | if (serviceAvailability is null) |
| | | 19 | | { |
| | 0 | 20 | | throw new TeleFlowConfigurationException( |
| | 0 | 21 | | "TeleFlow runtime validation requires a Microsoft.Extensions.DependencyInjection service provider."); |
| | | 22 | | } |
| | | 23 | | |
| | 114 | 24 | | var errors = new List<string>(); |
| | | 25 | | |
| | 114 | 26 | | ValidateHandlers(services, serviceAvailability, errors); |
| | 114 | 27 | | ValidateErrorHandlers(services, serviceAvailability, errors); |
| | | 28 | | |
| | 114 | 29 | | if (errors.Count == 0) |
| | | 30 | | { |
| | 108 | 31 | | return; |
| | | 32 | | } |
| | | 33 | | |
| | 6 | 34 | | throw new TeleFlowConfigurationException( |
| | 6 | 35 | | "TeleFlow configuration is invalid." + |
| | 6 | 36 | | Environment.NewLine + |
| | 6 | 37 | | Environment.NewLine + |
| | 6 | 38 | | string.Join(Environment.NewLine + Environment.NewLine, errors)); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private static void ValidateHandlers( |
| | | 42 | | IServiceProvider services, |
| | | 43 | | IServiceProviderIsService serviceAvailability, |
| | | 44 | | List<string> errors) |
| | | 45 | | { |
| | 702 | 46 | | foreach (var handler in services.GetServices<TelegramHandlerDescriptor>()) |
| | | 47 | | { |
| | 874 | 48 | | foreach (var parameter in handler.Parameters.Where(static parameter => |
| | 718 | 49 | | parameter.Kind == TelegramHandlerParameterKind.Service)) |
| | | 50 | | { |
| | 200 | 51 | | if (!serviceAvailability.IsService(parameter.ParameterType)) |
| | | 52 | | { |
| | 4 | 53 | | errors.Add( |
| | 4 | 54 | | "Handler dependency was not registered." + |
| | 4 | 55 | | Environment.NewLine + |
| | 4 | 56 | | $"Handler: {TelegramHandlerDescriptorFormatter.GetDisplayName(handler)}" + |
| | 4 | 57 | | Environment.NewLine + |
| | 4 | 58 | | $"Parameter: {parameter.Name ?? "<unnamed>"}" + |
| | 4 | 59 | | Environment.NewLine + |
| | 4 | 60 | | $"Service type: {FormatType(parameter.ParameterType)}" + |
| | 4 | 61 | | Environment.NewLine + |
| | 4 | 62 | | $"Register {FormatType(parameter.ParameterType)} in IServiceCollection before building the TeleF |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | 494 | 66 | | foreach (var filterType in GetCustomFilterTypes(handler)) |
| | | 67 | | { |
| | 10 | 68 | | if (!serviceAvailability.IsService(filterType)) |
| | | 69 | | { |
| | 2 | 70 | | errors.Add( |
| | 2 | 71 | | "Custom filter was not registered." + |
| | 2 | 72 | | Environment.NewLine + |
| | 2 | 73 | | $"Handler: {TelegramHandlerDescriptorFormatter.GetDisplayName(handler)}" + |
| | 2 | 74 | | Environment.NewLine + |
| | 2 | 75 | | $"Filter type: {FormatType(filterType)}" + |
| | 2 | 76 | | Environment.NewLine + |
| | 2 | 77 | | $"Register {FormatType(filterType)} in IServiceCollection before building the TeleFlow applicati |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |
| | 114 | 81 | | } |
| | | 82 | | |
| | | 83 | | private static void ValidateErrorHandlers( |
| | | 84 | | IServiceProvider services, |
| | | 85 | | IServiceProviderIsService serviceAvailability, |
| | | 86 | | List<string> errors) |
| | | 87 | | { |
| | 270 | 88 | | foreach (var handler in services.GetServices<TelegramErrorHandlerDescriptor>()) |
| | | 89 | | { |
| | 80 | 90 | | foreach (var parameter in handler.Parameters.Where(static parameter => |
| | 71 | 91 | | parameter.Kind == TelegramErrorHandlerParameterKind.Service)) |
| | | 92 | | { |
| | 19 | 93 | | if (!serviceAvailability.IsService(parameter.ParameterType)) |
| | | 94 | | { |
| | 1 | 95 | | errors.Add( |
| | 1 | 96 | | "Error handler dependency was not registered." + |
| | 1 | 97 | | Environment.NewLine + |
| | 1 | 98 | | $"Error handler: {FormatErrorHandler(handler)}" + |
| | 1 | 99 | | Environment.NewLine + |
| | 1 | 100 | | $"Parameter: {parameter.Name ?? "<unnamed>"}" + |
| | 1 | 101 | | Environment.NewLine + |
| | 1 | 102 | | $"Service type: {FormatType(parameter.ParameterType)}" + |
| | 1 | 103 | | Environment.NewLine + |
| | 1 | 104 | | $"Register {FormatType(parameter.ParameterType)} in IServiceCollection before building the TeleF |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | } |
| | 114 | 108 | | } |
| | | 109 | | |
| | | 110 | | private static IEnumerable<Type> GetCustomFilterTypes(TelegramHandlerDescriptor handler) |
| | | 111 | | { |
| | 237 | 112 | | return handler.Route.Filters |
| | 94 | 113 | | .Select(static filter => filter.CustomFilterType) |
| | 237 | 114 | | .OfType<Type>() |
| | 237 | 115 | | .Distinct(); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | private static string FormatErrorHandler(TelegramErrorHandlerDescriptor handler) |
| | | 119 | | { |
| | 1 | 120 | | return $"{handler.HandlerType.FullName}.{handler.MethodName}"; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | private static string FormatType(Type type) |
| | | 124 | | { |
| | 14 | 125 | | return type.FullName ?? type.Name; |
| | | 126 | | } |
| | | 127 | | } |