コード例 #1
0
ファイル: AppHost.cs プロジェクト: BillHertzing/Ace
        /// <summary>
        /// Application specific configuration
        /// This method should initialize any IoC resources utilized by your web service classes.
        /// </summary>
        public override void Configure(Container container)
        {
            // inject the concrete logging implementation
            Log.Debug($"Entering AppHost.Configure method, container is {container.ToString()}");

            // AppSettings is a first class object on the Container, so it will be auto-wired
            // In any other assembly, AppSettings is read-only, so it must be populated in this assembly
            // Location of the configuration files will depend on running as LifeCycle Production/QA/Dev as well as Debug and Release settings

            //It would be nice if ServiceStack implemented the User Secrets pattern that ASP Core provides
            // The Environment variable may define the location of a text file to add to the AppSettings
            string indirectSettingsTextFilepath;

            try {
                indirectSettingsTextFilepath = Environment.GetEnvironmentVariable(agentEnvironmentIndirectSettingsTextFileNameKey);
            }
            catch (SecurityException e) {
                Log.Error($"{cannotReadEnvironmentVariablesSecurityExceptionMessage}: {e.Message}");
                throw new SecurityException(cannotReadEnvironmentVariablesSecurityExceptionMessage, e);
            }

            // ToDo: If the Environment variable does define a location for another text fle, ensure it can be read
            if (indirectSettingsTextFilepath != null)
            {
                try { }
                catch { }
            }
            // Create the AppSettingsBuilder, and add command line arguments to it
            var multiAppSettingsBuilder = new MultiAppSettingsBuilder();

            // Highest priority is any command line variable values, so add command line arguments to the AppSettingsBuilder
            // ToDo: .Add??
            // Next in priority are all environment values, so append AddEnvironmentalVariables to the AppSettingsBuilder
            multiAppSettingsBuilder.AddEnvironmentalVariables();
            // Next in priority are contents of any indirect files mentioned in the environment variables (e.g. User Secrets )
            if (indirectSettingsTextFilepath != null)
            {
                multiAppSettingsBuilder.AddTextFile(indirectSettingsTextFilepath);
            }
            // Next in priority are any configuration settings in a text file named on the command line.
            // ToDo: if a configuration settings text file is specified on the command line, ensure we have permission to read it
            // ToDo: append AddTextFile for configuration settings in a text file specified on the command line to the AppSettingsBuilder
            // Next in priority are any configuration settings in a text file named as a constant string in the app.
            // Location of the text file is relative to the current working directory at the point in time when this method executes.
            // If this file does not exist, it is not considered an error, but if it does exist, not having read permission is an error
            // ToDo: if a configuration settings text file is specified as a constant string in the app, ensure we have permission to read it
            // ToDo: if it exists, append AddTextFile for configuration settings in a text file specified as a constant string in the app to the AppSettingsBuilder
            multiAppSettingsBuilder.AddTextFile(agentSettingsTextFileNameString);
            // Next in priority are any configuration settings in the application config file (AKA AceAgent.exe.config at runtime)
            // Location of the application config file is relative to the current working directory at the point in time when this method executes.
            // If this file does not exist, it is not considered an error, but if it does exist, not having read permission is an error
            // ToDo: if a application config file  is specified as a constant string in the app, ensure we have permission to read it
            // ToDo: if it exists, append AddTextFile for configuration settings in a text file specified as a constant string in the app to the AppSettingsBuilder
            multiAppSettingsBuilder.AddAppSettings()
            // Builtin (compiled in) configuration settings have the lowest priority
            // Add these to the AppSettingsBuilder
            .AddDictionarySettings(DefaultConfiguration.Configuration());
            //Build the AppSettings
            AppSettings = multiAppSettingsBuilder.Build();

            // Create the BaseServices data structure and register it in the container
            //  The AppHost (here, ServiceStack running as a Windows service) has some configuration that is common
            //  to both Frameworks (.Net and .Net Core), which will be setup in a common assembly, so this instance of
            //  the appHost is being passed to the BaseServicesData constructor.
            //  this also registers a BaseServicesData instance in the container
            // ToDo: implement Singleton pattern for BaseServicesData in the DI Container
            var baseServicesData = new BaseServicesData(this);

            container.Register <BaseServicesData>(c => baseServicesData);

            // ToDo: Get the list of plugins to install from the configuration settings, currently hard coded
            // Create the list of PlugIns to load
            var plugInList = new List <IPlugin>()
            {
                //new RealEstateServicesPlugin(),
                //new MinerServicesPlugin(),
                new DiskAnalysisServicesPlugin(),
                new GUIServicesPlugin()
            };

            // Load each plugin here.
            foreach (var pl in plugInList)
            {
                Plugins.Add(pl);
            }

            // ToDo: See Issue #8
            // ToDo place a static, deep-copy of the current application'instance of the configuration settings as the first object in the application's configuration settings history list.

            // start all the timers
            Log.Debug("In AppHost.Configure: starting all timers");
            var timers = Container.Resolve <Dictionary <string, System.Timers.Timer> >();
            var longRunningTasksCheckTimer = timers[Ace.Agent.BaseServices.BaseServicesData.LongRunningTasksCheckTimerName];

            longRunningTasksCheckTimer.Start();

            /*
             * // ToDo: create a NotifyIcon equivalent for a Windows Service or Linux Daemon. Notifiy Icon itself will not work, as that is for WinForms only
             * Log.Debug("in AppHost.Configure: Create a NotifyIcon for AceCommander");
             * Application.EnableVisualStyles();
             * Application.SetCompatibleTextRenderingDefault(false);
             * NotifyIcon notifyIcon1 = new NotifyIcon();
             * ContextMenu contextMenu1 = new ContextMenu();
             * MenuItem menuItem1 = new MenuItem();
             * contextMenu1.MenuItems
             *  .AddRange(new MenuItem[] { menuItem1 });
             * menuItem1.Index=0;
             * menuItem1.Text="E&xit";
             * menuItem1.Click+=new EventHandler(menuItem1_Click);
             * notifyIcon1.Icon=new Icon("atap.ico");
             * notifyIcon1.Text="AceCommander";
             * notifyIcon1.ContextMenu=contextMenu1;
             * notifyIcon1.Visible=true;
             * // Log.Debug("Calling a Web Forms Application instance's static Run method");
             * // Application.Run();
             * // notifyIcon1.Visible = false;
             * Log.Debug("NotifyIcon for AceCommander created");
             */
            Log.Debug("Leaving AppHost.Configure");
        }