public static IMvcCoreBuilder AddRestContract(this IMvcCoreBuilder builder) { builder = builder.ConfigureApplicationPartManager(p => p.FeatureProviders.Add(new ContractControllerFeatureProvider())); builder.Services.AddSingleton <IApplicationModelProvider, ContractModelProvider>(); return(builder); }
public static IMvcCoreBuilder AddWebApiProxyServer(this IMvcCoreBuilder mvcBuilder, Action <WebApiProxyOptions> optionAction) { AppDomain.CurrentDomain.UpdateExcutingAssemblies(); var option = new WebApiProxyOptions(); optionAction(option); foreach (var o in option.WebApiProxies) { var interfaceTypes = o.GetType().Assembly.GetTypes() .Where(x => x.IsInterface); foreach (var t in interfaceTypes) { mvcBuilder.AddMvcOptions(opt => { opt.Conventions.Add(new ControllerModelConvention(t)); opt.Conventions.Add(new ActionModelConvention(t)); opt.Conventions.Add(new ParameterModelConvention(t)); }); } mvcBuilder.ConfigureApplicationPartManager(manager => { var featureProvider = new ServiceControllerFeatureProvider(interfaceTypes); manager.FeatureProviders.Remove( manager.FeatureProviders.FirstOrDefault(x => x.GetType() == typeof(ControllerFeatureProvider))); manager.FeatureProviders.Add(featureProvider); }); ; } return(mvcBuilder); }
public static IMvcCoreBuilder AddWebApiProxy(this IMvcCoreBuilder mvcBuilder, Action <WebApiProxyOptions> optionAction) { var option = new WebApiProxyOptions(); optionAction(option); foreach (var o in option.WebApiProxies) { var interfaceTypes = o.GetType().Assembly.GetTypes() .Where(x => x.IsInterface && x.GetMethods() .SelectMany(m => m.GetCustomAttributes(typeof(ApiActionAttribute), true)).Any()); foreach (var t in interfaceTypes) { mvcBuilder.AddMvcOptions(opt => { opt.Conventions.Add(new ControllerModelConvention(t)); opt.Conventions.Add(new ActionModelConvention(t)); opt.Conventions.Add(new ParameterModelConvention(t)); }); } mvcBuilder.ConfigureApplicationPartManager(manager => { var featureProvider = new ServiceControllerFeatureProvider(interfaceTypes); manager.FeatureProviders.Remove( manager.FeatureProviders.FirstOrDefault(x => x.GetType() == typeof(ControllerFeatureProvider))); manager.FeatureProviders.Add(featureProvider); }); ; } return(mvcBuilder); }
public static IMvcCoreBuilder AddMiruNestedControllers(this IMvcCoreBuilder mvcCoreBuilder) { mvcCoreBuilder.ConfigureApplicationPartManager(m => { m.FeatureProviders.Add(new MiruControllerFeatureProvider()); }); return(mvcCoreBuilder); }
public static IMvcCoreBuilder RemoveIdentityUtilsAuthenticationControllerAssemblyPart(this IMvcCoreBuilder builder) { builder.ConfigureApplicationPartManager(x => { x.ApplicationParts.RemoveAuthenticationControllerFromAppParts(); }); return(builder); }
/// <summary> /// 注入api /// </summary> /// <param name="mvcBuilder"></param> /// <returns></returns> public static IMvcCoreBuilder AddNarutoId4DashbordApi(this IMvcCoreBuilder mvcBuilder) { mvcBuilder.Services.AddNarutoIdentityServer4Dashbord(); //注入mvc扩展 mvcBuilder.ConfigureApplicationPartManager(a => { a.ApplicationParts.Add(new AssemblyPart(typeof(DashbordExtension).Assembly)); }); return(mvcBuilder); }
/// <summary> /// 注入配置界面 /// </summary> /// <param name="mvcBuilder"></param> /// <returns></returns> public static IMvcCoreBuilder AddConfigurationManagement(this IMvcCoreBuilder mvcBuilder, OcelotEFOption ocelotEFOption) { mvcBuilder.Services.AddOcelotRepository(ocelotEFOption); mvcBuilder.Services.AddServices(); //注入mvc扩展 mvcBuilder.ConfigureApplicationPartManager(a => { a.ApplicationParts.Add(new AssemblyPart(typeof(ConfigurationExtensions).Assembly)); }); return(mvcBuilder); }
public static IMvcCoreBuilder ExcludeControllers(this IMvcCoreBuilder mvcBuilder, params Type[] excludedControllers) { mvcBuilder.ConfigureApplicationPartManager(manager => { var controllerFeatureProvider = manager.FeatureProviders .Single(p => p.GetType() == typeof(ControllerFeatureProvider)); manager.FeatureProviders[manager.FeatureProviders.IndexOf(controllerFeatureProvider)] = new CustomControllerFeatureProvider(excludedControllers); }); return(mvcBuilder); }
public static void LoadDynamicControllers( this IMvcCoreBuilder mvcBuilder, DynamicModuleProvider dynamicModuleProvider ) { mvcBuilder.ConfigureApplicationPartManager(p => p.FeatureProviders.Add( new DynamicModuleControllerFeatureProvider( dynamicModuleProvider.ModuleContexts.GroupBy(x => x.LibModuleInfo.ModuleGuid).Select(x => x.First()).ToList() ) ) ); }
/// <summary> /// Adds an <see cref="ApplicationPart"/> to the list of <see cref="ApplicationPartManager.ApplicationParts"/> on the /// <see cref="IMvcCoreBuilder.PartManager"/>. /// </summary> /// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param> /// <param name="assembly">The <see cref="Assembly"/> of the <see cref="ApplicationPart"/>.</param> /// <returns>The <see cref="IMvcCoreBuilder"/>.</returns> public static IMvcCoreBuilder AddApplicationPart(this IMvcCoreBuilder builder, Assembly assembly) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } builder.ConfigureApplicationPartManager(manager => manager.ApplicationParts.Add(new AssemblyPart(assembly))); return(builder); }
/// <summary> /// Adds all services required for the <see cref="RenderRazor"/> module. /// </summary> /// <param name="serviceCollection">The service collection to register services in.</param> /// <param name="fileSystem">The file system or <c>null</c> to skip.</param> /// <param name="classCatalog">An existing class catalog or <c>null</c> to scan assemblies during registration.</param> /// <returns>The service collection.</returns> public static IServiceCollection AddRazor(this IServiceCollection serviceCollection, IReadOnlyFileSystem fileSystem, ClassCatalog classCatalog = null) { // Register the file system if we're not expecting one from an engine if (fileSystem != null) { serviceCollection.TryAddSingleton(fileSystem); } // Register some of our own types if not already registered serviceCollection.TryAddSingleton <Microsoft.Extensions.FileProviders.IFileProvider, FileSystemFileProvider>(); serviceCollection.TryAddSingleton <DiagnosticSource, SilentDiagnosticSource>(); serviceCollection.TryAddSingleton(new DiagnosticListener("Razor")); serviceCollection.TryAddSingleton <IWebHostEnvironment, HostEnvironment>(); serviceCollection.TryAddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>(); serviceCollection.TryAddSingleton <StatiqRazorProjectFileSystem>(); serviceCollection.TryAddSingleton <RazorProjectFileSystem, StatiqRazorProjectFileSystem>(); // Register the view location expander if not already registered serviceCollection.Configure <RazorViewEngineOptions>(x => { if (!x.ViewLocationExpanders.OfType <ViewLocationExpander>().Any()) { x.ViewLocationExpanders.Add(new ViewLocationExpander()); } }); // Add the default services _after_ adding our own // (most default registration use .TryAdd...() so they skip already registered types) IMvcCoreBuilder builder = serviceCollection .AddMvcCore() .AddRazorViewEngine() .AddRazorRuntimeCompilation(); // Add all loaded assemblies CompilationReferencesProvider referencesProvider = new CompilationReferencesProvider(); referencesProvider.Assemblies.AddRange((classCatalog ?? new ClassCatalog()).GetAssemblies()); // And a couple needed assemblies that might not be loaded in the AppDomain yet referencesProvider.Assemblies.Add(typeof(IHtmlContent).Assembly); referencesProvider.Assemblies.Add(Assembly.Load(new AssemblyName("Microsoft.CSharp"))); // Add the reference provider as an ApplicationPart builder.ConfigureApplicationPartManager(x => x.ApplicationParts.Add(referencesProvider)); return(serviceCollection); }
public static IMvcCoreBuilder AddDynamicWebApi(this IMvcCoreBuilder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } builder.ConfigureApplicationPartManager(applicationPartManager => { applicationPartManager.FeatureProviders.Add(new ApplicationServiceControllerFeatureProvider()); }); builder.Services.Configure <MvcOptions>(options => { options.Conventions.Add(new ApplicationServiceConvention()); }); return(builder); }
private static void ConfigureInterfaces(this IMvcCoreBuilder mvcBuilder, IEnumerable <Type> serviceTypes) { foreach (var t in serviceTypes) { mvcBuilder.AddMvcOptions(opt => { opt.Conventions.Add(new ControllerModelConvention(t)); opt.Conventions.Add(new ActionModelConvention(t)); opt.Conventions.Add(new ParameterModelConvention(t)); }); } mvcBuilder.ConfigureApplicationPartManager(manager => { var featureProvider = new ServiceControllerFeatureProvider(serviceTypes); manager.FeatureProviders.Remove(manager.FeatureProviders.FirstOrDefault(x => x.GetType() == typeof(ControllerFeatureProvider))); manager.FeatureProviders.Add(featureProvider); }); }
private static IMvcCoreBuilder ConfigureInterfaces(this IMvcCoreBuilder mvcBuilder, WebApiProxyOptions options) { foreach (var t in options.RegisteredServices.Select(x => x.Value)) { mvcBuilder.AddMvcOptions(opt => { opt.Conventions.Add(new ControllerModelConvention(t)); opt.Conventions.Add(new ActionModelConvention(t)); opt.Conventions.Add(new ParameterModelConvention(t)); }); } mvcBuilder.ConfigureApplicationPartManager(manager => { var featureProvider = new ServiceControllerFeatureProvider(options.RegisteredServices.Select(x => x.Value)); manager.FeatureProviders.Remove(manager.FeatureProviders.FirstOrDefault(x => x.GetType() == typeof(ControllerFeatureProvider))); manager.FeatureProviders.Add(featureProvider); }); return(mvcBuilder); }
/// <summary> /// Adds an <see cref="ApplicationPart"/> to the list of <see cref="ApplicationPartManager.ApplicationParts"/> on the /// <see cref="IMvcCoreBuilder.PartManager"/>. /// </summary> /// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param> /// <param name="assembly">The <see cref="Assembly"/> of the <see cref="ApplicationPart"/>.</param> /// <returns>The <see cref="IMvcCoreBuilder"/>.</returns> public static IMvcCoreBuilder AddApplicationPart(this IMvcCoreBuilder builder, Assembly assembly) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } builder.ConfigureApplicationPartManager(manager => { var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly); foreach (var applicationPart in partFactory.GetApplicationParts(assembly)) { manager.ApplicationParts.Add(applicationPart); } }); return(builder); }
public static IMvcCoreBuilder AddSpecificControllers(this IMvcCoreBuilder mvcCoreBuilder, IEnumerable <Type> controllerTypes) { return(mvcCoreBuilder.ConfigureApplicationPartManager( partManager => partManager.AddSpecificControllers(controllerTypes))); }
private static void AddActiveRouteImpl <TController, TFeature, TFeatureOptions>(IMvcCoreBuilder mvcBuilder) where TFeature : class, IDynamicFeature where TFeatureOptions : class { // Add [DynamicController(typeof(TComponentOptions))] if not present if (!typeof(TController).HasAttribute <DynamicControllerAttribute>()) { var attribute = new DynamicControllerAttribute(typeof(TFeatureOptions)); TypeDescriptor.AddAttributes(typeof(TController), attribute); var attributes = TypeDescriptor.GetAttributes(typeof(TController)); if (!attributes.Contains(attribute)) { throw new InvalidOperationException("Could not add attribute dynamically on this runtime."); } } // See: https://github.com/aspnet/Mvc/issues/5992 mvcBuilder.AddApplicationPart(typeof(TController).Assembly); mvcBuilder.ConfigureApplicationPartManager(x => { x.ApplicationParts.Add(new DynamicControllerApplicationPart(new[] { typeof(TController).GetTypeInfo() })); }); var componentDescriptor = ServiceDescriptor.Singleton(r => { var component = Instancing.CreateInstance <TFeature>(); component.GetRouteTemplate = () => { var o = r.GetRequiredService <IOptionsMonitor <TFeatureOptions> >(); return(o.CurrentValue is IFeatureNamespace ns ? ns.RootPath ?? string.Empty : string.Empty); }; return(component); }); mvcBuilder.Services.Replace(componentDescriptor); mvcBuilder.Services.AddTransient <IDynamicFeature>(r => { // cached singleton var component = r.GetService <TFeature>(); // each resolution, we could be discovering a different controller that needs hydration into its type for (var i = 0; i < component.ControllerTypes.Count; i++) { var controllerType = component.ControllerTypes[i]; if (controllerType.IsGenericType && controllerType.Name == typeof(TController).Name) { component.ControllerTypes[i] = typeof(TController); } } return(component); }); mvcBuilder.AddAuthorization(x => { if (x.GetPolicy(Constants.Security.Policies.NoPolicy) == null) { x.AddPolicy(Constants.Security.Policies.NoPolicy, b => { b.RequireAssertion(context => true); }); } }); }
public RazorCompiler(CompilationParameters parameters, IExecutionContext context) { // Create the service collection that MVC needs and add default MVC services ServiceCollection serviceCollection = new ServiceCollection(); DiagnosticListener listener = new DiagnosticListener("Razor"); // Register some of our own types serviceCollection .AddSingleton(parameters.FileSystem) .AddSingleton <Microsoft.Extensions.FileProviders.IFileProvider, FileSystemFileProvider>() .AddSingleton(context.GetRequiredService <ILoggerFactory>()) .AddSingleton <DiagnosticSource, SilentDiagnosticSource>() .AddSingleton <DiagnosticListener>(listener) .AddSingleton <IWebHostEnvironment, HostEnvironment>() .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>() // .AddSingleton<IViewCompilerProvider, StatiqRazorViewCompilerProvider>() .AddSingleton <StatiqRazorProjectFileSystem>() .AddSingleton <RazorProjectFileSystem, StatiqRazorProjectFileSystem>(); // Register the view location expander serviceCollection.Configure <RazorViewEngineOptions>(x => x.ViewLocationExpanders.Add(new ViewLocationExpander())); // Add the default services _after_ adding our own // (most default registration use .TryAdd...() so they skip already registered types) IMvcCoreBuilder builder = serviceCollection .AddMvcCore() .AddRazorViewEngine() .AddRazorRuntimeCompilation(); // Add all loaded assemblies CompilationReferencesProvider referencesProvider = new CompilationReferencesProvider(); referencesProvider.Assemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies()); // And a couple needed assemblies that might not be loaded in the AppDomain yet referencesProvider.Assemblies.Add(typeof(IHtmlContent).Assembly); referencesProvider.Assemblies.Add(Assembly.Load(new AssemblyName("Microsoft.CSharp"))); // Add the reference provider as an ApplicationPart builder.ConfigureApplicationPartManager(x => x.ApplicationParts.Add(referencesProvider)); // Build the service provider and local scope IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider(); _serviceScopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>(); // Calculate the base page type Type basePageType = parameters.BasePageType ?? typeof(StatiqRazorPage <>); string baseClassName = basePageType.FullName; int tickIndex = baseClassName.IndexOf('`'); if (tickIndex > 0) { baseClassName = baseClassName.Substring(0, tickIndex); } string baseType = basePageType.IsGenericTypeDefinition ? $"{baseClassName}<TModel>" : baseClassName; // We need to register a new document classifier phase because builder.SetBaseType() (which uses builder.ConfigureClass()) // use the DefaultRazorDocumentClassifierPhase which stops applying document classifier passes after DocumentIntermediateNode.DocumentKind is set // (which gets set by the Razor document classifier passes registered in RazorExtensions.Register()) // Also need to add it just after the DocumentClassifierPhase, otherwise it'll miss the C# lowering phase RazorProjectEngine razorProjectEngine = serviceProvider.GetRequiredService <RazorProjectEngine>(); List <IRazorEnginePhase> phases = razorProjectEngine.Engine.Phases.ToList(); phases.Insert( phases.IndexOf(phases.OfType <IRazorDocumentClassifierPhase>().Last()) + 1, new StatiqDocumentPhase(baseType, parameters.Namespaces) { Engine = razorProjectEngine.Engine }); FieldInfo phasesField = razorProjectEngine.Engine.GetType().GetField("<Phases>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic); phasesField.SetValue(razorProjectEngine.Engine, phases.ToArray()); }
/// <summary> /// Only allow selected controllers /// </summary> /// <param name="mvcCoreBuilder">The builder that configures mvc core</param> /// <param name="controllerTypes">The controller types that are allowed. </param> public static IMvcCoreBuilder UseSpecificControllers( this IMvcCoreBuilder mvcCoreBuilder, params Type[] controllerTypes) => mvcCoreBuilder .ConfigureApplicationPartManager(partManager => partManager.UseSpecificControllers(controllerTypes));
public static IMvcCoreBuilder IgnoreAccessModifiersOfControllers(this IMvcCoreBuilder mvcCoreBuilder) { return(mvcCoreBuilder.ConfigureApplicationPartManager( partManager => partManager.FeatureProviders.Add(new CustomControllerFeatureProvider()))); }