예제 #1
0
        /// <summary>
        /// Initializes the AppHost.
        /// Calls the <see cref="Configure"/> method.
        /// Should be called before start.
        /// </summary>
        public virtual ServiceStackHost Init()
        {
            if (Instance != null)
            {
                throw new InvalidDataException($"ServiceStackHost.Instance has already been set ({Instance.GetType().Name})");
            }

            Service.GlobalResolver = Instance = this;

            //RegisterLicenseKey(AppSettings.GetNullableString("servicestack:license"));

            var scanAssemblies = new List <Assembly>(ServiceAssemblies);

            scanAssemblies.AddIfNotExists(GetType().Assembly);
            //var scanTypes = scanAssemblies.SelectMany(x => x.GetTypes())
            //    .Where(x => (x.HasInterface(typeof(IPreConfigureAppHost))
            //                 || x.HasInterface(typeof(IConfigureAppHost))
            //                 || x.HasInterface(typeof(IAfterInitAppHost))))
            //    .ToArray();

            //var startupConfigs = scanTypes.Where(x => !x.HasInterface(typeof(IPlugin)))
            //    .Select(x => x.CreateInstance()).WithPriority();
            //var configInstances = startupConfigs.PriorityOrdered();
            //var preStartupConfigs = startupConfigs.PriorityBelowZero();
            //var postStartupConfigs = startupConfigs.PriorityZeroOrAbove();

            //void RunPreConfigure(object instance)
            //{
            //    try
            //    {
            //        if (instance is IPreConfigureAppHost preConfigureAppHost)
            //            preConfigureAppHost.PreConfigure(this);
            //    }
            //    catch (Exception ex)
            //    {
            //        OnStartupException(ex);
            //    }
            //}
            //var priorityPlugins = Plugins.WithPriority().PriorityOrdered().Map(x => (IPlugin)x);
            //priorityPlugins.ForEach(RunPreConfigure);
            //configInstances.ForEach(RunPreConfigure);

            if (ServiceController == null)
            {
                ServiceController = CreateServiceController(ServiceAssemblies.ToArray());
            }

            Config = HostConfig.ResetInstance();
            OnConfigLoad();

            //AbstractVirtualFileBase.ScanSkipPaths = Config.ScanSkipPaths;
            //ResourceVirtualDirectory.EmbeddedResourceTreatAsFiles = Config.EmbeddedResourceTreatAsFiles;

            OnBeforeInit();
            ServiceController.Init();

            //void RunConfigure(object instance)
            //{
            //    try
            //    {
            //        //if (instance is IConfigureAppHost configureAppHost)
            //            //configureAppHost.Configure(this);
            //    }
            //    catch (Exception ex)
            //    {
            //        OnStartupException(ex);
            //    }
            //}
            //preStartupConfigs.ForEach(RunConfigure);
            //BeforeConfigure.Each(fn => fn(this));

            Configure(Container);

            //AfterConfigure.Each(fn => fn(this));

            //postStartupConfigs.ForEach(RunConfigure);

            if (Config.StrictMode == null && Config.DebugMode)
            {
                Config.StrictMode = true;
            }

            //if (!Config.DebugMode)
            //    Plugins.RemoveAll(x => x is RequestInfoFeature);

            //Some plugins need to initialize before other plugins are registered.
            Plugins.ToList().ForEach(RunPreInitPlugin);
            //configInstances.ForEach(RunPreInitPlugin);

            List <IVirtualPathProvider> pathProviders = null;

            if (VirtualFileSources == null)
            {
                pathProviders = GetVirtualFileSources().Where(x => x != null).ToList();

                //VirtualFileSources = pathProviders.Count > 1
                //    ? new MultiVirtualFiles(pathProviders.ToArray())
                //    : pathProviders.First();
            }

            //if (VirtualFiles == null)
            //    VirtualFiles = pathProviders?.FirstOrDefault(x => x is FileSystemVirtualFiles) as IVirtualFiles
            //        ?? GetVirtualFileSources().FirstOrDefault(x => x is FileSystemVirtualFiles) as IVirtualFiles;

            OnAfterInit();

            //configInstances.ForEach(RunPostInitPlugin);

            PopulateArrayFilters();

            LogInitComplete();

            HttpHandlerFactory.Init();

            foreach (var callback in AfterInitCallbacks)
            {
                callback(this);
            }

            Plugins.ForEach(RunAfterInitAppHost);
            //configInstances.ForEach(RunAfterInitAppHost);

            ReadyAt = DateTime.UtcNow;

            return(this);
        }