static IEnumerable <Type> FindTypesInNamespace(Type type, Func <Type, bool> typeFilter) { bool Filter(Type candidate) { return(typeFilter(candidate) && candidate.Namespace != null && candidate.Namespace.StartsWith(type.Namespace, StringComparison.OrdinalIgnoreCase)); } return(AssemblyTypeCache.FindTypes(type.Assembly, TypeClassification.Concrete, Filter).GetAwaiter().GetResult()); }
/// <summary> /// Adds all futures that match the given filter in the specified assemblies /// </summary> /// <param name="configurator"></param> /// <param name="filter"></param> /// <param name="assemblies">The assemblies to scan for futures</param> public static void AddFutures(this IRegistrationConfigurator configurator, Func <Type, bool> filter, params Assembly[] assemblies) { if (assemblies.Length == 0) { assemblies = AppDomain.CurrentDomain.GetAssemblies(); } var types = AssemblyTypeCache.FindTypes(assemblies, TypeMetadataCache.IsFutureOrDefinition).GetAwaiter().GetResult(); AddFutures(configurator, filter, types.FindTypes(TypeClassification.Concrete | TypeClassification.Closed).ToArray()); }
public static void RegisterSagaStateMachines(this IContainerRegistrar registrar, params Assembly[] assemblies) { if (assemblies.Length == 0) { assemblies = AppDomain.CurrentDomain.GetAssemblies(); } var types = AssemblyTypeCache.FindTypes(assemblies, x => x.HasInterface(typeof(SagaStateMachine <>))).GetAwaiter().GetResult(); registrar.RegisterSagaStateMachines(types.FindTypes(TypeClassification.Concrete | TypeClassification.Closed).ToArray()); }
/// <summary> /// Adds SagaStateMachines to the registry, using the factory method, and updates the registrar prior to registering so that the default /// saga registrar isn't notified. /// </summary> /// <param name="configurator"></param> /// <param name="registrar"></param> /// <param name="assemblies">The assemblies to scan for state machines</param> public static void AddSagaStateMachines(this IRegistrationConfigurator configurator, ISagaStateMachineRegistrar registrar, params Assembly[] assemblies) { if (assemblies.Length == 0) { assemblies = AppDomain.CurrentDomain.GetAssemblies(); } var types = AssemblyTypeCache.FindTypes(assemblies, IsSagaStateMachineOrDefinition).GetAwaiter().GetResult(); configurator.AddSagaStateMachines(registrar, types.FindTypes(TypeClassification.Concrete).ToArray()); }
/// <summary> /// Adds all consumers in the specified assemblies /// </summary> /// <param name="configurator"></param> /// <param name="assemblies">The assemblies to scan for consumers</param> public static void AddConsumers(this IRegistrationConfigurator configurator, params Assembly[] assemblies) { if (assemblies.Length == 0) { assemblies = AppDomain.CurrentDomain.GetAssemblies(); } var types = AssemblyTypeCache.FindTypes(assemblies, IsConsumerOrDefinition).GetAwaiter().GetResult(); AddConsumers(configurator, types.FindTypes(TypeClassification.Concrete | TypeClassification.Closed).ToArray()); }
static IList <Type> FindTypes <T>(IKernel kernel, Func <Type, bool> filter) { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); TypeSet types = AssemblyTypeCache.FindTypes(assemblies, typeof(T).IsAssignableFrom).Result; return(types.AllTypes() .SelectMany(kernel.GetBindings) .Select(x => x.Service) .Distinct() .Where(filter) .ToList()); }
/// <summary> /// Adds SagaStateMachines to the registry, using the factory method, and updates the registrar prior to registering so that the default /// saga registrar isn't notified. /// </summary> /// <param name="configurator"></param> /// <param name="assemblies">The assemblies to scan for state machines</param> public static void AddSagaStateMachines(this IRegistrationConfigurator configurator, params Assembly[] assemblies) { if (assemblies.Length == 0) { assemblies = AppDomain.CurrentDomain.GetAssemblies(); } var types = AssemblyTypeCache.FindTypes(assemblies, type => TypeMetadataCache.IsSagaStateMachineOrDefinition(type) && !TypeMetadataCache.IsFutureOrDefinition(type)) .GetAwaiter().GetResult(); configurator.AddSagaStateMachines(types.FindTypes(TypeClassification.Concrete | TypeClassification.Closed).ToArray()); }
static IServiceInstanceTransportConfigurator <TEndpointConfigurator> GetServiceInstanceTransportConfigurator() { var assembly = typeof(TEndpointConfigurator).Assembly; var transportConfiguratorType = AssemblyTypeCache .FindTypes(assembly, TypeClassification.Concrete, x => x.HasInterface <IServiceInstanceTransportConfigurator <TEndpointConfigurator> >()) .GetAwaiter().GetResult().FirstOrDefault(); if (transportConfiguratorType == null) { throw new ArgumentException($"Type not found: {TypeMetadataCache<IServiceInstanceTransportConfigurator<TEndpointConfigurator>>.ShortName}"); } return((IServiceInstanceTransportConfigurator <TEndpointConfigurator>)Activator.CreateInstance(transportConfiguratorType)); }
TypeConverterCache() { _typeConverters = new ConcurrentDictionary <Type, object>(); _converters = new List <object>(); var assembly = typeof(BooleanTypeConverter).Assembly; var ns = typeof(BooleanTypeConverter).Namespace; var converterTypes = AssemblyTypeCache.FindTypes(assembly, TypeClassification.Concrete | TypeClassification.Closed, x => x.Namespace.StartsWith(ns) && x.HasInterface(typeof(ITypeConverter <,>))).GetAwaiter().GetResult().ToArray(); foreach (var converterType in converterTypes) { AddSupportedTypes(converterType); } }
static IEnumerable <Type> FindTypesInNamespace(Type type, Func <Type, bool> typeFilter) { if (type.Namespace == null) { throw new ArgumentException("The type must have a valid namespace", nameof(type)); } var dottedNamespace = type.Namespace + "."; bool Filter(Type candidate) { return(typeFilter(candidate) && candidate.Namespace != null && (candidate.Namespace.StartsWith(dottedNamespace, StringComparison.OrdinalIgnoreCase) || candidate.Namespace.Equals(type.Namespace, StringComparison.OrdinalIgnoreCase))); } return(AssemblyTypeCache.FindTypes(type.Assembly, TypeClassification.Concrete | TypeClassification.Closed, Filter).GetAwaiter().GetResult()); }
public static async Task MapEndpointConventions(this IBusFactoryConfigurator cfg, params Assembly[] assemblies) { var types = await AssemblyTypeCache.FindTypes(assemblies, t => { var info = t.GetTypeInfo(); return(!info.IsAbstract && !info.IsInterface && typeof(ICommand).IsAssignableFrom(t)); }); var allTypes = types.AllTypes(); var mapMethod = typeof(EndpointConvention) .GetMethod("Map", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(Uri) }, null); foreach (var type in allTypes) { var generic = mapMethod.MakeGenericMethod(type); generic.Invoke(null, new object[] { new Uri($"queue:{type.Name}") }); } }
public Task <TypeSet> ScanForTypes() { return(AssemblyTypeCache.FindTypes(_assemblies, _filter.Matches)); }
public static IServiceCollection ConfigureBus(this IServiceCollection services, IConfiguration configuration, string apiResourceName, Assembly assembly, Action <IServiceCollectionConfigurator <IExternalBus> > postAction = null) { var config = new MassTransitConfig(); configuration.Bind("MassTransitConfig", config); // get referenced assemblies var assemblies = new List <Assembly> { assembly }; assemblies.AddRange(assembly.GetProjectReferencedAssemblies()); // find consumers var types = AssemblyTypeCache.FindTypes(assemblies, TypeMetadataCache.IsConsumerOrDefinition).GetAwaiter() .GetResult(); var consumers = types.FindTypes(TypeClassification.Concrete | TypeClassification.Closed).ToArray(); var internals = new List <Type>(); var externals = new List <Type>(); foreach (var type in consumers) { if (type.HasInterface <IExternalConsumer>()) { externals.Add(type); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"{type.FullName} registered into External Bus"); } else { internals.Add(type); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine($"{type.FullName} registered into Mediator"); } } services.AddMediator(x => { x.AddConsumers(internals.ToArray()); }); //services.AddMassTransit(x => //{ // x.AddConsumers(internals.ToArray()); // x.AddMediator((provider, cfg) => { cfg.UseFluentValidation(); }); //}); services.AddMassTransit <IExternalBus>(x => { x.AddConsumers(externals.ToArray()); x.AddBus(provider => { switch (config.BusType) { case BusType.AzureServiceBus: case BusType.ActiveMQ: case BusType.AmazonSQS: throw new NotImplementedException(); default: var busControl = Bus.Factory.CreateUsingRabbitMq(cfg => { cfg.Host(new Uri(config.RabbitMQ.Address), h => { h.Username(config.RabbitMQ.Username); h.Password(config.RabbitMQ.Password); }); cfg.ReceiveEndpoint(apiResourceName, ep => { ep.ConfigureConsumers(provider); }); cfg.ConnectConsumerConfigurationObserver( new FluentValidationConsumerConfigurationObserver(provider)); //cfg.UseFluentValidation(); }); return(busControl); } }); postAction?.Invoke(x); }); services.AddMassTransitHostedService(); return(services); }