/// <summary>
        /// Tries to load commands.
        /// </summary>
        /// <param name="commands">The commands.</param>
        /// <returns></returns>
        public override bool TryLoadCommands(out IEnumerable <ICommand> commands)
        {
            commands = null;

            var commandAssemblies = new List <Assembly>();

            if (m_AppServer.GetType().Assembly != this.GetType().Assembly)
            {
                commandAssemblies.Add(m_AppServer.GetType().Assembly);
            }

            string commandAssembly = m_AppServer.Config.Options.GetValue("commandAssembly");

            if (!string.IsNullOrEmpty(commandAssembly))
            {
                try
                {
                    var definedAssemblies = AssemblyUtil.GetAssembliesFromString(commandAssembly);

                    if (definedAssemblies.Any())
                    {
                        commandAssemblies.AddRange(definedAssemblies);
                    }
                }
                catch (Exception e)
                {
                    OnError(new Exception("Failed to load defined command assemblies!", e));
                    return(false);
                }
            }

            if (!commandAssemblies.Any())
            {
                OnError("You should configure the commandAssembly value!");
                return(false);
            }

            var outputCommands = new List <ICommand>();

            foreach (var assembly in commandAssemblies)
            {
                try
                {
                    outputCommands.AddRange(assembly.GetImplementedObjectsByInterface <ICommand>(m_CommandType));
                }
                catch (Exception exc)
                {
                    OnError(new Exception(string.Format("Failed to get commands from the assembly {0}!", assembly.FullName), exc));
                    return(false);
                }
            }

            commands = outputCommands;

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the commands for specific server.
        /// </summary>
        /// <typeparam name="TAppSession">The type of the app session.</typeparam>
        /// <typeparam name="TRequestInfo">The type of the request info.</typeparam>
        /// <param name="appServer">The app server.</param>
        /// <param name="commandRegister">The command register.</param>
        /// <param name="commandUpdater">The command updater.</param>
        /// <returns></returns>
        public bool LoadCommands <TAppSession, TRequestInfo>(IAppServer appServer, Func <ICommand <TAppSession, TRequestInfo>, bool> commandRegister, Action <IEnumerable <CommandUpdateInfo <ICommand <TAppSession, TRequestInfo> > > > commandUpdater)
            where TAppSession : IAppSession, IAppSession <TAppSession, TRequestInfo>, new()
            where TRequestInfo : IRequestInfo
        {
            var commandAssemblies = new List <Assembly> {
                appServer.GetType().Assembly
            };

            string commandAssembly = appServer.Config.Options.GetValue("commandAssembly");

            if (!string.IsNullOrEmpty(commandAssembly))
            {
                try
                {
                    var definedAssemblies = AssemblyUtil.GetAssembliesFromString(commandAssembly);

                    if (definedAssemblies.Any())
                    {
                        commandAssemblies.AddRange(definedAssemblies);
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("Failed to load defined command assemblies!", e);
                }
            }

            foreach (var assembly in commandAssemblies)
            {
                foreach (var c in assembly.GetImplementedObjectsByInterface <ICommand <TAppSession, TRequestInfo> >())
                {
                    if (!commandRegister(c))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        /// <summary>
        /// Tries to load commands.
        /// </summary>
        /// <param name="commands">The commands.</param>
        /// <returns></returns>
        public override bool TryLoadCommands(out IEnumerable <TCommand> commands)
        {
            commands = null;

            var commandAssemblies = new List <Assembly>();

            if (m_AppServer.GetType().Assembly != this.GetType().Assembly)
            {
                commandAssemblies.Add(m_AppServer.GetType().Assembly);
            }

            string commandAssembly = m_AppServer.Config.Options.GetValue("commandAssembly");

            if (!string.IsNullOrEmpty(commandAssembly))
            {
                OnError("The configuration attribute 'commandAssembly' is not in used, please try to use the child node 'commandAssemblies' instead!");
                return(false);
            }


            if (m_AppServer.Config.CommandAssemblies != null && m_AppServer.Config.CommandAssemblies.Any())
            {
                try
                {
                    var definedAssemblies = AssemblyUtil.GetAssembliesFromStrings(m_AppServer.Config.CommandAssemblies.Select(a => a.Assembly).ToArray());

                    if (definedAssemblies.Any())
                    {
                        commandAssemblies.AddRange(definedAssemblies);
                    }
                }
                catch (Exception e)
                {
                    OnError(new Exception("Failed to load defined command assemblies!", e));
                    return(false);
                }
            }

            if (!commandAssemblies.Any())
            {
                commandAssemblies.Add(Assembly.GetEntryAssembly());
            }

            var outputCommands = new List <TCommand>();

            foreach (var assembly in commandAssemblies)
            {
                try
                {
                    outputCommands.AddRange(assembly.GetImplementedObjectsByInterface <TCommand>());
                }
                catch (Exception exc)
                {
                    OnError(new Exception(string.Format("Failed to get commands from the assembly {0}!", assembly.FullName), exc));
                    return(false);
                }
            }

            commands = outputCommands;

            return(true);
        }
        /// <summary>
        /// Tries to load commands.
        /// </summary>
        /// <param name="commands">The commands.</param>
        /// <param name="serviceProvider">A container for service objects.</param>
        /// <returns></returns>
        public override bool TryLoadCommands(out IEnumerable <TCommand> commands, IServiceProvider serviceProvider)
        {
            commands = null;

            var commandAssemblies = new List <Assembly>();

            if (m_AppServer.GetType().Assembly != this.GetType().Assembly)
            {
                commandAssemblies.Add(m_AppServer.GetType().Assembly);
            }

#if !NETSTANDARD2_0
            string commandAssembly = m_AppServer.Config.Options.GetValue("commandAssembly");
#else
            string commandAssembly = string.Empty;
#endif

            if (!string.IsNullOrEmpty(commandAssembly))
            {
                OnError("The configuration attribute 'commandAssembly' is not in used, please try to use the child node 'commandAssemblies' instead!");
                return(false);
            }


            if (m_AppServer.Config.CommandAssemblies != null && m_AppServer.Config.CommandAssemblies.Any())
            {
                try
                {
                    var definedAssemblies = AssemblyUtil.GetAssembliesFromStrings(m_AppServer.Config.CommandAssemblies.Select(a => a.Assembly).ToArray());

                    if (definedAssemblies.Any())
                    {
                        commandAssemblies.AddRange(definedAssemblies);
                    }
                }
                catch (Exception e)
                {
                    OnError(new Exception("Failed to load defined command assemblies!", e));
                    return(false);
                }
            }

            if (!commandAssemblies.Any())
            {
                commandAssemblies.Add(Assembly.GetEntryAssembly());
            }

            var outputCommands = new List <TCommand>();

            foreach (var assembly in commandAssemblies)
            {
                try
                {
                    var typesToActivate = assembly.GetImplementedTypesByInterface <TCommand>();
                    outputCommands.AddRange(
                        typesToActivate.Select(typeToActivate => (TCommand)ActivatorUtilities.CreateInstance(serviceProvider, typeToActivate)));
                }
                catch (Exception exc)
                {
                    OnError(new Exception(string.Format("Failed to get commands from the assembly {0}!", assembly.FullName), exc));
                    return(false);
                }
            }

            commands = outputCommands;

            return(true);
        }