internal protected void InitializeAssemblies()
 {
     InternalAssemblies.Add(AddInternalAssemblies1);
 }
示例#2
0
        public void Initialise()
        {
            if (InternalConfiguration == null)
            {
                throw new InvalidOperationException("Configuration cannot be null");
            }

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

            var instanceRegistrations = new[] { new InstanceRegistration(typeof(INancyModuleCatalog), this) }.ToList();

            var typeRegistrations = InternalConfiguration.GetTypeRegistations()
                                    .Concat(GetAdditionalTypes())
                                    .Concat(InternalConfiguration.GetCollectionTypeRegistrations()
                                            .Concat(GetApplicationCollections())
                                            .SelectMany(ctr => ctr.ImplementationTypes
                                                        .Select(it => new TypeRegistration(ctr.RegistrationType, it, ctr.Lifetime))))
                                    .ToList();

            ConfigureConventions(Conventions);
            var conventionValidationResult = Conventions.Validate();

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

            instanceRegistrations.AddRange(Conventions.GetInstanceRegistrations().Concat(GetAdditionalInstances()));

            RegisterRegistrationTasks(typeRegistrations, instanceRegistrations, GetRegistrationTasks(typeRegistrations, instanceRegistrations));

            var conventions      = GetInternalCompositionConventions(typeRegistrations);
            var instanceProvider = GetInternalInstanceExportDescriptorProvider(instanceRegistrations);
            var assemblies       = InternalAssemblies.ToList();

            ApplicationContainer = CreateApplicationContainer(conventions, assemblies, instanceProvider);

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

            ApplicationStartup(ApplicationContainer, ApplicationPipelines);

            RequestStartupTaskTypeCache = RequestStartupTasks.ToArray();

            if (FavIcon != null)
            {
                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);
                });
            }

            GetDiagnostics().Initialize(ApplicationPipelines);

            _initialised = true;
        }