예제 #1
0
        public void Group()
        {
            lock (_groupingLock)
            {
                if (_hasGrouped)
                {
                    return;
                }

                _calls.Where(x => TypeExtensions.IsConcrete(x.MessageType))
                .GroupBy(x => x.MessageType)
                .Select(group => new HandlerChain(@group))
                .Each(chain =>
                {
                    _chains = _chains.AddOrUpdate(chain.MessageType, chain);
                });

                _calls.Where(x => !TypeExtensions.IsConcrete(x.MessageType))
                .Each(call =>
                {
                    Chains
                    .Where(c => call.CouldHandleOtherMessageType(c.MessageType))
                    .Each(c => { c.AddAbstractedHandler(call); });
                });

                _hasGrouped = true;
            }
        }
예제 #2
0
        private async Task startAspNetCoreServer()
        {
            if (!Registry.HttpRoutes.Enabled || Registry.BootstrappedWithinAspNetCore)
            {
                return;
            }


            HostingEventSource.Log.HostStart();

            _logger = Get <ILoggerFactory>().CreateLogger("Jasper");

            _applicationLifetime = TypeExtensions.As <ApplicationLifetime>(Container.GetInstance <IApplicationLifetime>());

            var httpContextFactory = Container.QuickBuild <HttpContextFactory>();

            var hostingApp = new HostingApplication(
                RequestDelegate,
                _logger,
                Container.GetInstance <DiagnosticListener>(), // See if this can be passed in directly
                httpContextFactory);

            await _server.StartAsync(hostingApp, Settings.Cancellation);

            // Fire IApplicationLifetime.Started
            _applicationLifetime?.NotifyStarted();
        }
예제 #3
0
        public static bool IsCandidate(MethodInfo method)
        {
            if (method.DeclaringType == typeof(object))
            {
                return(false);
            }

            var parameterCount = method.GetParameters() == null ? 0 : method.GetParameters().Length;

            if (parameterCount > 1)
            {
                return(false);
            }

            if (method.GetParameters().Any(x => TypeExtensions.IsSimple(x.ParameterType)))
            {
                return(false);
            }

            var hasOutput = method.ReturnType != typeof(void);

            if (hasOutput && !method.ReturnType.GetTypeInfo().IsClass)
            {
                return(false);
            }


            if (hasOutput)
            {
                return(true);
            }

            return(parameterCount == 1);
        }
            public static IStartup Build(IServiceProvider provider, ServiceDescriptor descriptor)
            {
                if (descriptor.ImplementationInstance != null)
                {
                    return(TypeExtensions.As <IStartup>(descriptor.ImplementationInstance));
                }

                if (descriptor.ImplementationType != null)
                {
                    return(TypeExtensions.As <IStartup>(provider.GetService(descriptor.ServiceType)));
                }

                return(TypeExtensions.As <IStartup>(descriptor.ImplementationFactory(provider)));
            }
예제 #5
0
        public void AddForwarders(Forwarders forwarders)
        {
            foreach (var pair in forwarders.Relationships)
            {
                var source      = pair.Key;
                var destination = pair.Value;

                if (_chains.TryFind(destination, out var inner))
                {
                    var handler =
                        TypeExtensions.CloseAndBuildAs <MessageHandler>(typeof(ForwardingHandler <,>), this, new Type[] { source, destination });

                    _chains   = _chains.AddOrUpdate(source, handler.Chain);
                    _handlers = _handlers.AddOrUpdate(source, handler);
                }
            }
        }
예제 #6
0
        private static void applyExtensions(JasperRegistry registry)
        {
            var assemblies = FindExtensionAssemblies();

            if (!assemblies.Any())
            {
                return;
            }

            var extensions = assemblies
                             .Select(x => x.GetAttribute <JasperModuleAttribute>().ExtensionType)
                             .Where(x => x != null)
                             .Select(x => TypeExtensions.As <IJasperExtension>(Activator.CreateInstance(x)))
                             .ToArray();

            registry.ApplyExtensions(extensions);
        }
예제 #7
0
        private static async Task <JasperRuntime> bootstrap(JasperRegistry registry)
        {
            var timer = new PerfTimer();

            timer.Start("Bootstrapping");

            timer.Record("Finding and Applying Extensions", () =>
            {
                applyExtensions(registry);
            });

            var buildingServices = Task.Factory.StartNew(() =>
            {
                return(timer.Record("Combining Services and Building Settings", registry.CompileConfigurationAndServicesForIdiomaticBootstrapping));
            });



            var handlerCompilation = registry.Messaging.CompileHandlers(registry, timer);


            var runtime = new JasperRuntime(registry, timer);

            var services = await buildingServices;

            services.AddSingleton(runtime);


            var container = await Lamar.Container.BuildAsync(services, timer);

            container.DisposalLock = DisposalLock.Ignore;
            runtime.Container      = container;


            var routeDiscovery = registry.HttpRoutes.Enabled
                ? registry.HttpRoutes.FindRoutes(runtime, registry, timer)
                : Task.CompletedTask;

            runtime.buildAspNetCoreServer(services);

            await routeDiscovery;

            await Task.WhenAll(runtime.startAspNetCoreServer(), handlerCompilation, runtime.startHostedServices());


            // Run environment checks
            timer.Record("Environment Checks", () =>
            {
                var recorder = EnvironmentChecker.ExecuteAll(runtime);
                if (runtime.Settings.ThrowOnValidationErrors)
                {
                    recorder.AssertAllSuccessful();
                }
            });

            _lifetime = TypeExtensions.As <ApplicationLifetime>(container.GetInstance <IApplicationLifetime>());
            _lifetime.NotifyStarted();

            timer.Stop();

            return(runtime);
        }