public void PopulateFeature(IEnumerable <ApplicationPart> parts, ControllerFeature feature) { var shaConfig = _iocManager.Resolve <IShaApplicationModuleConfiguration>(); foreach (var registration in shaConfig.DynamicApplicationServiceRegistrations) { var entityTypes = registration.Assembly.GetExportedTypes().Where(t => t.IsEntityType()).ToList(); var existingControllerNames = feature.Controllers.Select(c => MvcHelper.GetControllerName(c)).ToList(); foreach (var entityType in entityTypes) { var entityAttribute = entityType.GetAttribute <EntityAttribute>(); if (entityAttribute != null && !entityAttribute.GenerateApplicationService) { continue; } var appServiceType = GetAppServiceType(entityType); if (appServiceType != null) { var controllerName = MvcHelper.GetControllerName(appServiceType); if (!existingControllerNames.Contains(controllerName)) { feature.Controllers.Add(appServiceType.GetTypeInfo()); if (!_iocManager.IsRegistered(appServiceType)) { _iocManager.Register(appServiceType, lifeStyle: DependencyLifeStyle.Transient); } } } } } }
public void Apply(ControllerModel controller) { if (controller.ControllerType.IsGenericType && controller.ControllerType.GetGenericTypeDefinition() == typeof(DynamicCrudAppService <, ,>)) { var entityType = controller.ControllerType.GenericTypeArguments[0]; var registrations = _shaConfig.Value.DynamicApplicationServiceRegistrations.Where(reg => reg.Assembly == entityType.Assembly).ToList(); if (registrations.Count > 1) { throw new NotSupportedException($"Multiple registration of dynamic entities are not supported. Assembly name: '{entityType.Assembly.FullName}', modules: {registrations.Select(r => r.ModuleName).Delimited(",")}"); } var registration = registrations.FirstOrDefault(); if (registration == null) { return; } var moduleName = registration.ModuleName ?? AbpControllerAssemblySetting.DefaultServiceModuleName; controller.ControllerName = MvcHelper.GetControllerName(controller.ControllerType); foreach (var action in controller.Actions) { var routeTemplate = $"api/services/{moduleName}/{controller.ControllerName}/{action.ActionName}"; foreach (var selector in action.Selectors) { if (!selector.ActionConstraints.OfType <HttpMethodActionConstraint>().Any()) { var httpMethod = ProxyScriptingHelper.GetConventionalVerbForMethodName(action.ActionName); selector.ActionConstraints.Add(new HttpMethodActionConstraint(new[] { httpMethod })); } if (selector.AttributeRouteModel == null) { selector.AttributeRouteModel = new AttributeRouteModel(new RouteAttribute(routeTemplate)); } } } } }
/// <summary> /// Add separate Swagger documents for each Application Service and Controller /// Url format: `/swagger/service:{ApplicationService or controller name}/swagger.json` /// </summary> /// <param name="options"></param> public static void AddDocumentsPerService(this SwaggerGenOptions options) { var types = GetRegisteredControllerTypes(); var assemblyFinder = StaticContext.IocManager.Resolve <IAssemblyFinder>(); var assemblies = assemblyFinder.GetAllAssemblies(); // filter assemblies to include only the ones which are defined in modules types = types.Where(t => assemblies.Contains(t.Assembly)).ToList(); //var controllers = typeFinder.Find(c => !c.IsAbstract && typeof(ControllerBase).IsAssignableFrom(c)); // 1. add controllers var controllers = types.Where(t => typeof(ControllerBase).IsAssignableFrom(t)).ToList(); foreach (var controller in controllers) { var serviceName = MvcHelper.GetControllerName(controller); options.SwaggerDoc($"service:{serviceName}", new OpenApiInfo() { Title = $"{serviceName} (ControllerBase)", Version = "v1" }); } // 2. add application services var appServices = types.Where(t => typeof(IApplicationService).IsAssignableFrom(t)).ToList(); foreach (var service in appServices) { var serviceName = MvcHelper.GetControllerName(service); options.SwaggerDoc($"service:{serviceName}", new OpenApiInfo() { Title = $"API {serviceName} (IApplicationService)", Version = "v1" }); } options.DocInclusionPredicate((docName, description) => ApiExplorerGroupPerControllerConvention.GroupInclusionPredicate(docName, description)); }