示例#1
0
        /// <summary>
        /// Scans for all implementations of <typeparamref name="TRegistration"/> and registers them, followed by the
        /// types defined by the <paramref name="defaultImplementations"/> parameter.
        /// </summary>
        /// <typeparam name="TRegistration">The <see cref="Type"/> to register as.</typeparam>
        /// <param name="defaultImplementations">The types to register last.</param>
        /// <param name="lifetime">Lifetime of the registration, defaults to singleton</param>
        /// <remarks>
        /// When scanning, it will exclude the assembly that the <see cref="Registrations"/> instance is defined in and it will also ignore
        /// the types specified by <paramref name="defaultImplementations"/>.
        /// </remarks>
        public void RegisterWithUserThenDefault <TRegistration>(IEnumerable <Type> defaultImplementations, Lifetime lifetime = Lifetime.Singleton)
        {
            var implementations = AppDomainAssemblyTypeScanner
                                  .TypesOf <TRegistration>()
                                  .Where(type => type.Assembly != this.GetType().Assembly)
                                  .Where(type => !defaultImplementations.Contains(type))
                                  .ToList();

            this.collectionRegistrations.Add(new CollectionTypeRegistration(typeof(TRegistration), implementations.Union(defaultImplementations), lifetime));
        }
示例#2
0
        /// <summary>
        /// Scans for all implementations of <typeparamref name="TRegistration"/> and registers them.
        /// </summary>
        /// <param name="lifetime">Lifetime of the registration, defaults to singleton</param>
        /// <typeparam name="TRegistration">The <see cref="Type"/> to scan for and register as.</typeparam>
        public void RegisterAll <TRegistration>(Lifetime lifetime = Lifetime.Singleton)
        {
            var implementations = AppDomainAssemblyTypeScanner
                                  .TypesOf <TRegistration>();

            var registration =
                new CollectionTypeRegistration(typeof(TRegistration), implementations, lifetime);

            this.collectionRegistrations.Add(registration);
        }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NancyBootstrapperBase{TContainer}"/> class.
        /// </summary>
        protected NancyBootstrapperBase()
        {
            AppDomainAssemblyTypeScanner.LoadNancyAssemblies();

            this.BeforeRequest = new BeforePipeline();
            this.AfterRequest  = new AfterPipeline();
            this.OnError       = new ErrorPipeline();

            this.conventions = new NancyConventions();
        }
示例#4
0
        private static IRootPathProvider GetRootPathProvider()
        {
            var providerType = AppDomainAssemblyTypeScanner
                               .TypesOf <IRootPathProvider>(ScanMode.ExcludeNancy)
                               .SingleOrDefault();

            if (providerType == null)
            {
                providerType = typeof(DefaultRootPathProvider);
            }

            return(Activator.CreateInstance(providerType) as IRootPathProvider);
        }
示例#5
0
        /// <summary>
        /// Scans for an implementation of <typeparamref name="TRegistration"/> and registers it if found. If no implementation could
        /// be found, it will retrieve an instance of <typeparamref name="TRegistration"/> using the provided <paramref name="defaultImplementationFactory"/>,
        /// which will be used in the registration.
        /// </summary>
        /// <typeparam name="TRegistration">The <see cref="Type"/> to register as.</typeparam>
        /// <param name="defaultImplementationFactory">Factory that provides an instance of <typeparamref name="TRegistration"/>.</param>
        /// <remarks>When scanning, it will exclude the assembly that the <see cref="Registrations"/> instance is defined in</remarks>
        public void RegisterWithDefault <TRegistration>(Func <TRegistration> defaultImplementationFactory)
        {
            var implementation = AppDomainAssemblyTypeScanner
                                 .TypesOf <TRegistration>()
                                 .SingleOrDefault(type => type.Assembly != this.GetType().Assembly);

            if (implementation != null)
            {
                this.typeRegistrations.Add(new TypeRegistration(typeof(TRegistration), implementation));
            }
            else
            {
                this.instanceRegistrations.Add(new InstanceRegistration(typeof(TRegistration), defaultImplementationFactory.Invoke()));
            }
        }
示例#6
0
        /// <summary>
        /// Scans for all implementations of <typeparamref name="TRegistration"/>. If no implementations could be found, then it
        /// will register the types specified by <paramref name="defaultImplementations"/>.
        /// </summary>
        /// <typeparam name="TRegistration">The <see cref="Type"/> to register as.</typeparam>
        /// <param name="defaultImplementations">The types to register if non could be located while scanning.</param>
        /// <remarks>
        /// When scanning, it will exclude the assembly that the <see cref="ApplicationRegistrations"/> instance is defined in and it will also ignore
        /// the types specified by <paramref name="defaultImplementations"/>.
        /// </remarks>
        public void RegisterWithDefault <TRegistration>(IEnumerable <Type> defaultImplementations)
        {
            var implementations = AppDomainAssemblyTypeScanner
                                  .TypesOf <TRegistration>()
                                  .Where(type => type.Assembly != this.GetType().Assembly)
                                  .Where(type => !defaultImplementations.Contains(type))
                                  .ToList();

            if (!implementations.Any())
            {
                implementations = defaultImplementations.ToList();
            }

            this.collectionRegistrations.Add(new CollectionTypeRegistration(typeof(TRegistration), implementations));
        }
        private static IRootPathProvider GetRootPathProvider()
        {
            var providerTypes = AppDomainAssemblyTypeScanner
                                .TypesOf <IRootPathProvider>(ScanMode.ExcludeNancy)
                                .ToArray();

            if (providerTypes.Length > 1)
            {
                throw new MultipleRootPathProvidersLocatedException(providerTypes);
            }

            var providerType =
                providerTypes.SingleOrDefault() ?? typeof(DefaultRootPathProvider);

            return(Activator.CreateInstance(providerType) as IRootPathProvider);
        }
示例#8
0
        private static Type GetBootstrapperType()
        {
            var customBootstrappers = AppDomainAssemblyTypeScanner
                                      .TypesOf <INancyBootstrapper>(ScanMode.ExcludeNancy)
                                      .ToList();

            if (!customBootstrappers.Any())
            {
                return(typeof(DefaultNancyBootstrapper));
            }

            if (customBootstrappers.Count == 1)
            {
                return(customBootstrappers.Single());
            }

            var errorMessage = GetMultipleBootstrappersMessage(customBootstrappers);

            throw new BootstrapperException(errorMessage);
        }
        /// <summary>
        /// Initialise the bootstrapper. Must be called prior to GetEngine.
        /// </summary>
        public void Initialise()
        {
            if (this.InternalConfiguration == null)
            {
                throw new InvalidOperationException("Configuration cannot be null");
            }

            if (!this.InternalConfiguration.IsValid)
            {
                throw new InvalidOperationException("Configuration is invalid");
            }

            this.ApplicationContainer = this.GetApplicationContainer();

            this.RegisterBootstrapperTypes(this.ApplicationContainer);

            this.ConfigureApplicationContainer(this.ApplicationContainer);

            // We need to call this to fix an issue with assemblies that are referenced by DI not being loaded
            AppDomainAssemblyTypeScanner.UpdateTypes();

            var typeRegistrations = this.InternalConfiguration
                                    .GetTypeRegistrations()
                                    .Concat(this.GetAdditionalTypes());

            var collectionTypeRegistrations = this.InternalConfiguration
                                              .GetCollectionTypeRegistrations()
                                              .Concat(this.GetApplicationCollections());

            // TODO - should this be after initialiseinternal?
            this.ConfigureConventions(this.Conventions);
            var conventionValidationResult = this.Conventions.Validate();

            if (!conventionValidationResult.Item1)
            {
                throw new InvalidOperationException(string.Format("Conventions are invalid:\n\n{0}", conventionValidationResult.Item2));
            }

            var instanceRegistrations = this.Conventions.GetInstanceRegistrations()
                                        .Concat(this.GetAdditionalInstances());

            this.RegisterTypes(this.ApplicationContainer, typeRegistrations);
            this.RegisterCollectionTypes(this.ApplicationContainer, collectionTypeRegistrations);
            this.RegisterInstances(this.ApplicationContainer, instanceRegistrations);
            this.RegisterRegistrationTasks(this.GetRegistrationTasks());

            var environment = this.GetEnvironmentConfigurator().ConfigureEnvironment(this.Configure);

            this.RegisterNancyEnvironment(this.ApplicationContainer, environment);

            this.RegisterModules(this.ApplicationContainer, this.Modules);

            foreach (var applicationStartupTask in this.GetApplicationStartupTasks().ToList())
            {
                applicationStartupTask.Initialize(this.ApplicationPipelines);
            }

            this.ApplicationStartup(this.ApplicationContainer, this.ApplicationPipelines);

            this.RequestStartupTaskTypeCache = this.RequestStartupTasks.ToArray();

            if (this.FavIcon != null)
            {
                this.ApplicationPipelines.BeforeRequest.AddItemToStartOfPipeline(ctx =>
                {
                    if (ctx.Request == null || String.IsNullOrEmpty(ctx.Request.Path))
                    {
                        return(null);
                    }

                    if (String.Equals(ctx.Request.Path, "/favicon.ico", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var response = new Response
                        {
                            ContentType = "image/vnd.microsoft.icon",
                            StatusCode  = HttpStatusCode.OK,
                            Contents    = s => s.Write(this.FavIcon, 0, this.FavIcon.Length)
                        };

                        response.Headers["Cache-Control"] = "public, max-age=604800, must-revalidate";

                        return(response);
                    }

                    return(null);
                });
            }

            this.GetDiagnostics().Initialize(this.ApplicationPipelines);

            this.initialised = true;
        }