Ejemplo n.º 1
0
        protected override object OnExecute(CommandContext context)
        {
            if (_serviceFactory == null)
            {
                throw new CommandException(ResourceUtility.GetString("Text.CannotObtainCommandTarget", "ServiceProviderFactory"));
            }

            string providerName;

            if (context.Expression.Options.TryGetValue("provider", out providerName))
            {
                if (string.IsNullOrWhiteSpace(providerName) || providerName == "~" || providerName == ".")
                {
                    _serviceProvider = _serviceFactory.Default;
                }
                else
                {
                    var provider = _serviceFactory.GetProvider(providerName);

                    if (provider == null)
                    {
                        throw new CommandException(ResourceUtility.GetString("Text.ServicesCommand.NotFoundProvider", providerName));
                    }

                    _serviceProvider = provider;
                }

                //显示执行成功的信息
                context.Output.WriteLine(ResourceUtility.GetString("Text.CommandExecuteSucceed"));
            }

            var items = _serviceFactory as IEnumerable <KeyValuePair <string, Zongsoft.Services.IServiceProvider> >;

            if (items != null)
            {
                int index = 1;

                foreach (var item in items)
                {
                    context.Output.Write(CommandOutletColor.DarkMagenta, "[{0}] ", index++);

                    if (string.IsNullOrWhiteSpace(item.Key))
                    {
                        context.Output.Write("<Default>");
                    }
                    else
                    {
                        context.Output.Write(item.Key);
                    }

                    if (object.ReferenceEquals(item.Value, this.ServiceProvider))
                    {
                        context.Output.WriteLine(CommandOutletColor.Green, " (Actived)");
                    }
                    else
                    {
                        context.Output.WriteLine();
                    }
                }
            }

            return(_serviceProvider);
        }