コード例 #1
0
        public void AddMvcTwice_DoesNotAddApplicationFeatureProvidersTwice()
        {
            // Arrange
            var services  = new ServiceCollection();
            var providers = new IApplicationFeatureProvider[]
            {
                new ControllerFeatureProvider(),
                new ViewComponentFeatureProvider()
            };

            // Act
            services.AddMvc();
            services.AddMvc();

            // Assert
            var descriptor = Assert.Single(services, d => d.ServiceType == typeof(ApplicationPartManager));

            Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime);
            Assert.NotNull(descriptor.ImplementationInstance);
            var manager = Assert.IsType <ApplicationPartManager>(descriptor.ImplementationInstance);

            Assert.Collection(manager.FeatureProviders,
                              feature => Assert.IsType <ControllerFeatureProvider>(feature),
                              feature => Assert.IsType <ViewComponentFeatureProvider>(feature),
                              feature => Assert.IsType <TagHelperFeatureProvider>(feature),
                              feature => Assert.IsType <RazorCompiledItemFeatureProvider>(feature));
        }
コード例 #2
0
        private static void ConfigureApplicationPartManager(
            ApplicationOptions appOptions,
            ApplicationPartManager manager)
        {
            // Remove default ControllerFeatureProvider
            IApplicationFeatureProvider item = manager.FeatureProviders
                                               .FirstOrDefault(c => c.GetType()
                                                               .Equals(typeof(ControllerFeatureProvider)));

            if (item != null)
            {
                manager.FeatureProviders.Remove(item);
            }

            // Register new IApplicationFeatureProvider with a blacklist
            // depending on current configuration
            manager.FeatureProviders.Add(
                new BlackListedControllerFeatureProvider(new List <TypeInfo>()

                                                         .AddIf <Api.Invitations.InvitationsGetController>(
                                                             !appOptions.EnableInvitationGetEndpoint)

                                                         .AddIf <Api.Invitations.InvitationsPutController>(
                                                             !appOptions.EnableInvitationCreateEndpoint)

                                                         .AddIf <Api.Invitations.InvitationsDeleteController>(
                                                             !appOptions.EnableInvitationDeleteEndpoint)

                                                         .AddIf <Actions.Recover.RecoverController>(
                                                             !appOptions.EnableAccountRecovery)

                                                         .AddIf <Actions.Register.RegisterController>(
                                                             !appOptions.EnableAccountRegistration)
                                                         ));
        }
コード例 #3
0
        public void AddMvcTwice_DoesNotAddDuplicateFrameworkParts()
        {
            // Arrange
            var mvcRazorAssembly      = typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly;
            var mvcTagHelpersAssembly = typeof(InputTagHelper).GetTypeInfo().Assembly;
            var services  = new ServiceCollection();
            var providers = new IApplicationFeatureProvider[]
            {
                new ControllerFeatureProvider(),
                new ViewComponentFeatureProvider()
            };

            // Act
            services.AddMvc();
            services.AddMvc();

            // Assert
            var descriptor = Assert.Single(services, d => d.ServiceType == typeof(ApplicationPartManager));

            Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime);
            Assert.NotNull(descriptor.ImplementationInstance);
            var manager = Assert.IsType <ApplicationPartManager>(descriptor.ImplementationInstance);

            Assert.Equal(2, manager.ApplicationParts.Count);
            Assert.Single(manager.ApplicationParts.OfType <AssemblyPart>(), p => p.Assembly == mvcRazorAssembly);
            Assert.Single(manager.ApplicationParts.OfType <AssemblyPart>(), p => p.Assembly == mvcTagHelpersAssembly);
        }
コード例 #4
0
 /// <summary>
 /// Adds a feature provider.
 /// </summary>
 /// <param name="featureProvider">The feature provider.</param>
 public void AddFeatureProvider(IApplicationFeatureProvider featureProvider)
 {
     if (!this.featureProviders.Contains(featureProvider))
     {
         this.featureProviders.Add(featureProvider);
     }
 }
コード例 #5
0
 /// <inheritdoc />
 public IApplicationPartManager AddFeatureProvider(IApplicationFeatureProvider featureProvider)
 {
     if (!this.featureProviders.Contains(featureProvider))
     {
         this.featureProviders.Add(featureProvider);
     }
     return(this);
 }
        /// <summary>构造函数</summary>
        public ApplicationFeatureService()
        {
            this.configuration = AppsConfigurationView.Instance.Configuration;

            // 创建对象构建器(Spring.NET)
            string springObjectFile = this.configuration.Keys["SpringObjectFile"].Value;

            SpringObjectBuilder objectBuilder = SpringObjectBuilder.Create(AppsConfiguration.ApplicationName, springObjectFile);

            // 创建数据提供器
            this.provider = objectBuilder.GetObject <IApplicationFeatureProvider>(typeof(IApplicationFeatureProvider));
        }
コード例 #7
0
        public static void ReplaceControllerFeatureProvider(
            this IList <IApplicationFeatureProvider> featureProviders,
            ControllerFeatureProvider featureProvider)
        {
            // Remove default ControllerFeatureProvider
            IApplicationFeatureProvider item = featureProviders
                                               .FirstOrDefault(c => c.GetType()
                                                               .Equals(typeof(ControllerFeatureProvider)));

            if (item != null)
            {
                featureProviders.Remove(item);
            }

            featureProviders.Add(featureProvider);
        }
コード例 #8
0
 public IApplicationPartManager AddFeatureProvider(IApplicationFeatureProvider featureProvider)
 {
     this.manager.AddFeatureProvider(featureProvider);
     return(this);
 }
コード例 #9
0
        public static void AddPluginsMvc(
            this IServiceCollection services,
            IViewLocationExpander viewLocationExpander = null)
        {
            ServiceProvider serviceProvider = services.BuildServiceProvider();

            ILogger logger = serviceProvider
                             .GetService <ILoggerFactory>()
                             .CreateLogger(typeof(IServiceCollectionExtensions));

            // Dont use uglycase urls !
            services.AddRouting((options) =>
            {
                options.LowercaseUrls = true;
            });

            IMvcBuilder mvcBuilder = services
                                     .AddMvc()
                                     .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                                     .AddViewLocalization()
                                     .AddDataAnnotationsLocalization()
                                     .ConfigureApplicationPartManager(manager =>
            {
                IApplicationFeatureProvider toRemove = manager
                                                       .FeatureProviders
                                                       .First(f => f is MetadataReferenceFeatureProvider);

                manager.FeatureProviders
                .Remove(toRemove);

                manager.FeatureProviders
                .Add(new ReferencesMetadataReferenceFeatureProvider());
            });

            IEnumerable <Assembly> assemblies = PluginAssembyLoader.Assemblies;

            foreach (Assembly assembly in assemblies)
            {
                logger.LogDebug(
                    "Adding mvc application part: \"{0}\"",
                    assembly.FullName
                    );

                mvcBuilder.AddApplicationPart(assembly);
            }

            mvcBuilder.AddRazorOptions(razor =>
            {
                if (viewLocationExpander != null)
                {
                    logger.LogDebug(
                        "Replacing default view location expander with: \"{0}\"",
                        viewLocationExpander.GetType().FullName
                        );

                    razor.ViewLocationExpanders.Clear();
                    razor.ViewLocationExpanders.Add(viewLocationExpander);
                }
            });

            IEnumerable <IAddMvcAction> actions = PluginAssembyLoader
                                                  .GetServices <IAddMvcAction>();

            foreach (IAddMvcAction action in actions)
            {
                logger.LogDebug(
                    "Executing add mvc action \"{0}\"",
                    action.GetType().FullName
                    );

                action.Execute(mvcBuilder);
            }
        }
コード例 #10
0
        public void AddMvc_AddsAssemblyPartsForFrameworkTagHelpers()
        {
            // Arrange
            var mvcRazorAssembly = typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly;
            var mvcTagHelpersAssembly = typeof(InputTagHelper).GetTypeInfo().Assembly;
            var services = new ServiceCollection();
            var providers = new IApplicationFeatureProvider[]
            {
                new ControllerFeatureProvider(),
                new ViewComponentFeatureProvider()
            };

            // Act
            services.AddMvc();

            // Assert
            var descriptor = Assert.Single(services, d => d.ServiceType == typeof(ApplicationPartManager));
            Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime);
            Assert.NotNull(descriptor.ImplementationInstance);
            var manager = Assert.IsType<ApplicationPartManager>(descriptor.ImplementationInstance);

            Assert.Equal(2, manager.ApplicationParts.Count);
            Assert.Single(manager.ApplicationParts.OfType<AssemblyPart>(), p => p.Assembly == mvcRazorAssembly);
            Assert.Single(manager.ApplicationParts.OfType<AssemblyPart>(), p => p.Assembly == mvcTagHelpersAssembly);
        }
コード例 #11
0
        public void AddMvcTwice_DoesNotAddApplicationFeatureProvidersTwice()
        {
            // Arrange
            var services = new ServiceCollection();
            var providers = new IApplicationFeatureProvider[]
            {
                new ControllerFeatureProvider(),
                new ViewComponentFeatureProvider()
            };

            // Act
            services.AddMvc();
            services.AddMvc();

            // Assert
            var descriptor = Assert.Single(services, d => d.ServiceType == typeof(ApplicationPartManager));
            Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime);
            Assert.NotNull(descriptor.ImplementationInstance);
            var manager = Assert.IsType<ApplicationPartManager>(descriptor.ImplementationInstance);

            Assert.Collection(manager.FeatureProviders,
                feature => Assert.IsType<ControllerFeatureProvider>(feature),
                feature => Assert.IsType<ViewComponentFeatureProvider>(feature),
                feature => Assert.IsType<TagHelperFeatureProvider>(feature),
                feature => Assert.IsType<MetadataReferenceFeatureProvider>(feature));
        }
コード例 #12
0
        /// <summary>
        /// Use this method to setup the Repository APIs to support RapidCMS WebAssenbly on a separate server.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static IServiceCollection AddRapidCMSApi(this IServiceCollection services, Action <IApiConfig>?config = null)
        {
            if (_routeConvention != null || _controllerFeatureProvider != null)
            {
                throw new InvalidOperationException("Cannot call AddRapidCMSApi twice.");
            }

            var rootConfig = GetRootConfig(config);

            services.AddSingleton <IApiConfig>(rootConfig);

            services.AddHttpContextAccessor();

            if (rootConfig.AllowAnonymousUsage)
            {
                services.AddSingleton <IAuthorizationHandler, AllowAllAuthorizationHandler>();
                services.AddSingleton <AuthenticationStateProvider, AnonymousAuthenticationStateProvider>();
            }
            else
            {
                services.AddSingleton <AuthenticationStateProvider, HttpContextAuthenticationStateProvider>();
            }

            services.AddSingleton <ISetupResolver <IEntityVariantSetup>, GlobalEntityVariantSetupResolver>();

            services.AddTransient <IDataViewResolver, ApiDataViewResolver>();
            services.AddTransient <IRepositoryResolver, RepositoryResolver>();
            services.AddSingleton <IRepositoryTypeResolver, ApiRepositoryTypeResolver>();

            services.AddTransient <IPresentationDispatcher, GetEntityDispatcher>();
            services.AddTransient <IPresentationDispatcher, GetEntitiesDispatcher>();
            services.AddTransient <IPresentationService, PresentationService>();

            services.AddTransient <IInteractionDispatcher, RelateEntityDispatcher>();
            services.AddTransient <IInteractionDispatcher, ReorderEntityDispatcher>();
            services.AddTransient <IInteractionDispatcher, PersistEntityDispatcher>();
            services.AddTransient <IInteractionDispatcher, DeleteEntityDispatcher>();
            services.AddTransient <IInteractionService, InteractionService>();

            services.AddTransient <IAuthService, ApiAuthService>();
            services.AddSingleton <IExceptionService, ExceptionService>();
            // TODO: message service
            services.AddTransient <IParentService, ParentService>();

            services.AddTransient <IEditContextFactory, ApiEditContextWrapperFactory>();

            var controllersToAdd = rootConfig.Repositories.ToDictionary(
                repository =>
            {
                if (repository.DatabaseType == default)
                {
                    return(typeof(ApiRepositoryController <,>)
                           .MakeGenericType(repository.EntityType, repository.RepositoryType)
                           .GetTypeInfo());
                }
                else
                {
                    return(typeof(MappedApiRepositoryController <, ,>)
                           .MakeGenericType(repository.EntityType, repository.DatabaseType, repository.RepositoryType)
                           .GetTypeInfo());
                }
            },
                kv => kv.Alias);

            if (rootConfig.FileUploadHandlers.Any())
            {
                foreach (var handler in rootConfig.FileUploadHandlers)
                {
                    var type  = typeof(ApiFileUploadController <>).MakeGenericType(handler).GetTypeInfo();
                    var alias = AliasHelper.GetFileUploaderAlias(type);

                    controllersToAdd.Add(type, alias);
                }
            }

            _controllerFeatureProvider = new CollectionControllerFeatureProvider(controllersToAdd.Keys);
            _routeConvention           = new CollectionControllerRouteConvention(controllersToAdd);

            return(services);
        }