/// <summary> /// Make sure attributes are valid types /// </summary> private static void ValidateRegistration(AppBlocksServiceAttribute attributeInformation) { // No further checks necessary if ServiceType is not set if (attributeInformation.ServiceType == null) { return; } // Make sure that service loggers have the correct service attribute if (attributeInformation.ServiceType.GetType() == typeof(IServiceLogger) && !attributeInformation.GetType().IsAssignableFrom(typeof(AppBlocksLoggerServiceAttribute))) { var message = $"Please register {nameof(IServiceLogger)} using attribute {nameof(AppBlocksLoggerServiceAttribute)}. " + $"Registering using {nameof(AppBlocksServiceAttribute)} is not permitted"; var exception = new InvalidOperationException(message); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError(exception, "Error registering service"); } throw new InvalidOperationException(message); } // Make sure that service validators have the correct service attribute if (attributeInformation.ServiceType.GetType() == typeof(IServiceValidator) && !attributeInformation.GetType().IsAssignableFrom(typeof(AppBlocksValidatorServiceAttribute))) { var message = $"Please register {nameof(IServiceValidator)} using attribute {nameof(AppBlocksValidatorServiceAttribute)}. " + $"Registering using {nameof(AppBlocksValidatorServiceAttribute)} is not permitted"; var exception = new InvalidOperationException(message); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError(exception, "Error registering service"); } throw new InvalidOperationException(message); } // Make sure that workflow writers have the correct service attribute if (attributeInformation.ServiceType.GetType() == typeof(IWorkflowWriter) && !attributeInformation.GetType().IsAssignableFrom(typeof(AppBlocksWorkflowWriterServiceAttribute))) { var message = $"Please register {nameof(IWorkflowWriter)} using attribute {nameof(AppBlocksWorkflowWriterServiceAttribute)}. " + $"Registering using {nameof(AppBlocksWorkflowWriterServiceAttribute)} is not permitted"; var exception = new InvalidOperationException(message); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError(exception, "Error registering service"); } throw new InvalidOperationException(message); } }
/// <summary> /// Check if service is assignable to service type /// </summary> private static Type GetServiceRegistrationType(Type type, AppBlocksServiceAttribute attribute) { //If service type value is set make sure it is assignable. if (attribute.ServiceType != null) { if (!attribute.ServiceType.IsAssignableFrom(type)) { throw new Exception($"Type {type.FullName} cannot be registered as service " + $"{attribute.ServiceType.FullName}. {type.FullName} does not implement this interface"); } return(attribute.ServiceType); } else { return(type.GetInterfaces().Length > 0 ? type.GetInterfaces()[0] : type); } }
/// <summary> /// Adds interceptors to AppBlocks services /// </summary> private static void AddTypeInterceptors( AppBlocksServiceAttribute attribute, IRegistrationBuilder <object, ConcreteReflectionActivatorData, SingleRegistrationStyle> registration) { //bool isEnabledForInterception = false; // Return if no interceptors are set on service if ((attribute.Interceptors?.Count() ?? 0) == 0 && !(attribute.Workflows ?? Array.Empty <string>()).Any(s => !string.IsNullOrWhiteSpace(s))) { return; } // Add interceptor for service registration = registration .EnableInterfaceInterceptors() .InterceptedBy(typeof(AppBlocksServiceInterceptor)); //var interceptorsSet = attribute.Interceptors.ToHashSet<string>(); //if (interceptorsSet.Contains(AppBlocksInterceptorConstants.Logging)) //{ // registration = registration // .EnableInterfaceInterceptors().InterceptedBy(typeof(LoggingInterceptor)); // isEnabledForInterception = true; //} //if (interceptorsSet.Contains(AppBlocksInterceptorConstants.Validation)) //{ // if (isEnabledForInterception) registration = registration.InterceptedBy(typeof(ValidationInterceptor)); // else registration.EnableInterfaceInterceptors().InterceptedBy(typeof(ValidationInterceptor)); // isEnabledForInterception = true; //} ////A class should only be incercepted by workflow interceptor if ////Worflows fields has at least one non whitespace workflow name //if ((attribute.Workflows ?? new string[0]).Any(s => !string.IsNullOrWhiteSpace(s))) //{ // if (isEnabledForInterception) registration = registration.InterceptedBy(typeof(WorkflowInterceptor)); // else registration.EnableInterfaceInterceptors().InterceptedBy(typeof(WorkflowInterceptor)); // isEnabledForInterception = true; //} }