MassTransit.Host.AssemblyScanner.GetAssemblyRegistrations C# (CSharp) Method

GetAssemblyRegistrations() public method

public GetAssemblyRegistrations ( ) : IEnumerable
return IEnumerable
        public IEnumerable<AssemblyRegistration> GetAssemblyRegistrations()
        {
            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += CurrentDomain_ReflectionOnlyAssemblyResolve;

            try
            {
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

                if (_log.IsDebugEnabled)
                    _log.Debug($"Scanning assembly directory: {baseDirectory}");

                List<string> assemblies = Directory.EnumerateFiles(baseDirectory, "*.dll", SearchOption.AllDirectories)
                    .Select(x => new {Path = x, File = Path.GetFileName(x)})
                    .Where(x => !x.File.StartsWith("MassTransit.", StringComparison.OrdinalIgnoreCase))
                    .Where(x => !x.File.StartsWith("Topshelf.", StringComparison.OrdinalIgnoreCase))
                    .Where(x => !x.File.StartsWith("NewId.", StringComparison.OrdinalIgnoreCase))
                    .Where(x => !x.File.StartsWith("Newtonsoft.", StringComparison.OrdinalIgnoreCase))
                    .Where(x => !x.File.StartsWith("log4net.", StringComparison.OrdinalIgnoreCase))
                    .Where(x => !x.File.StartsWith("NLog.", StringComparison.OrdinalIgnoreCase))
                    .Where(x => !x.File.StartsWith("Autofac.", StringComparison.OrdinalIgnoreCase))
                    .Where(x => !x.File.StartsWith("RabbitMQ.", StringComparison.OrdinalIgnoreCase))
                    .Where(x => !x.File.StartsWith("Microsoft.", StringComparison.OrdinalIgnoreCase))
                    .Where(x => !x.File.StartsWith("System.", StringComparison.OrdinalIgnoreCase))
                    .Select(x => x.Path)
                    .ToList();

                var registrations = assemblies
                    .Select(ReflectionOnlyLoadAssembly)
                    .Where(x => x != null)
                    .SelectMany(TrySelectAllTypes)
                    .Where(x => IsSupportedType(x.Item2))
                    .GroupBy(x => x.Item1)
                    .Select(x => new { Assembly = Assembly.Load(x.Key.GetName()), FoundTypes = x })
                    .Select(x => new AssemblyRegistration(x.Assembly, x.FoundTypes.Select(y => x.Assembly.GetType(y.Item2.FullName)).ToArray()))
                    .ToList();

                return registrations;
            }
            finally
            {
                AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= CurrentDomain_ReflectionOnlyAssemblyResolve;
            }
        }

Usage Example

Example #1
0
        public bool Start(HostControl hostControl)
        {
            _log.InfoFormat("Starting RapidTransit Host");

            var started = new List <ServiceControl>();

            try
            {
                var scanner = new AssemblyScanner();

                List <AssemblyRegistration> registrations = scanner.GetAssemblyRegistrations().ToList();

                _log.Info($"Found {registrations.Count} assembly registrations");
                foreach (var registration in registrations)
                {
                    _log.Info($"Assembly: {registration.Assembly.GetName().Name}");
                    foreach (var type in registration.Types)
                    {
                        _log.Info($"  Type: {type.GetTypeName()}");
                    }
                }

                var busFactoryType = scanner.GetHostBusFactoryType();
                if (busFactoryType == null)
                {
                    throw new ConfigurationException("A valid transport assembly was not found.");
                }

                _bootstrapperScope = CreateBootstrapperScope(registrations, busFactoryType);

                var bootstrappers = _bootstrapperScope.Resolve <IEnumerable <IServiceBootstrapper> >();

                List <ServiceControl> services = bootstrappers.Select(x => x.CreateService()).ToList();

                foreach (ServiceControl serviceControl in services)
                {
                    hostControl.RequestAdditionalTime(TimeSpan.FromMinutes(1));

                    StartService(hostControl, serviceControl);

                    started.Add(serviceControl);
                }

                _services = started;

                return(true);
            }
            catch (Exception ex)
            {
                _log.Error("Service failed to start", ex);

                Parallel.ForEach(started, service =>
                {
                    hostControl.RequestAdditionalTime(TimeSpan.FromMinutes(1));
                    StopService(hostControl, service);
                });

                throw;
            }
        }
All Usage Examples Of MassTransit.Host.AssemblyScanner::GetAssemblyRegistrations