Пример #1
0
        public System.Type[] GetSubclasses(System.Type t)
        {
            var entryAssembly        = Assembly.GetEntryAssembly();
            var referencedAssemblies = entryAssembly.GetReferencedAssemblies();
            var allAssemblies        = new AssemblyName[referencedAssemblies.Length + 1];

            allAssemblies[0] = entryAssembly.GetName();
            for (int i = 0; i < referencedAssemblies.Length; i++)
            {
                allAssemblies[i + 1] = referencedAssemblies[i];
            }
            var subTypes = allAssemblies
                           .Select(Assembly.Load)
                           .SelectMany(x => x.DefinedTypes)
                           .Where(type => t.GetTypeInfo().IsAssignableFrom(type.AsType()));

            List <System.Type> result = new List <System.Type>();

            foreach (System.Type subType in subTypes)
            {
                if (subType.IsAbstract)
                {
                    continue;
                }
                result.Add(subType);
            }
            return(result.ToArray());
        }
Пример #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            _logger.LogTrace("Startup::ConfigureServices");

            try
            {
                if (_appSettings.IsValid())
                {
                    _logger.LogDebug("Startup::ConfigureServices::valid AppSettings");

                    services.Configure <AppSettings>(_appsettingsConfigurationSection);
                    _logger.LogDebug("Startup::ConfigureServices::AppSettings loaded for DI");

                    services.AddCors(opts =>
                    {
                        opts.AddPolicy(name: MyAllowSpecificOrigins,
                                       builder => builder.WithOrigins("http://localhost:3000")
                                       .AllowAnyHeader()
                                       .AllowAnyMethod());
                    });

                    services.AddControllers(
                        opt =>
                    {
                        //Custom filters, if needed
                        //opt.Filters.Add(typeof(CustomFilterAttribute));
                        opt.Filters.Add(new ProducesAttribute("application/json"));
                    }
                        ).SetCompatibilityVersion(CompatibilityVersion.Latest);

                    //API versioning
                    services.AddApiVersioning(
                        o =>
                    {
                        //o.Conventions.Controller<UserController>().HasApiVersion(1, 0);
                        o.ReportApiVersions = true;
                        o.AssumeDefaultVersionWhenUnspecified = true;
                        o.DefaultApiVersion = new ApiVersion(1, 0);
                        o.ApiVersionReader  = new UrlSegmentApiVersionReader();
                    }
                        );

                    // note: the specified format code will format the version as "'v'major[.minor][-status]"
                    services.AddVersionedApiExplorer(
                        options =>
                    {
                        options.GroupNameFormat = "'v'VVV";

                        // note: this option is only necessary when versioning by url segment. the SubstitutionFormat
                        // can also be used to control the format of the API version in route templates
                        options.SubstituteApiVersionInUrl = true;
                    });


                    //SWAGGER
                    if (_appSettings.Swagger.Enabled)
                    {
                        services.AddTransient <IConfigureOptions <SwaggerGenOptions>, ConfigureSwaggerOptions>();

                        services.AddSwaggerGen(options =>
                        {
                            options.OperationFilter <SwaggerDefaultValues>();

                            //1-Get all the assemblies of the project to add the related XML Comments
                            Assembly currentAssembly                 = Assembly.GetExecutingAssembly();
                            AssemblyName[] referencedAssemblies      = currentAssembly.GetReferencedAssemblies();
                            IEnumerable <AssemblyName> allAssemblies = null;

                            if (referencedAssemblies != null && referencedAssemblies.Any())
                            {
                                allAssemblies = referencedAssemblies.Union(new AssemblyName[] { currentAssembly.GetName() });
                            }
                            else
                            {
                                allAssemblies = new AssemblyName[] { currentAssembly.GetName() }
                            };

                            IEnumerable <string> xmlDocs = allAssemblies
                                                           .Select(a => Path.Combine(Path.GetDirectoryName(currentAssembly.Location), $"{a.Name}.xml"))
                                                           .Where(f => File.Exists(f));

                            //2-Add the path to the XML comments for the assemblies having enabled the doc generation
                            if (xmlDocs != null && xmlDocs.Any())
                            {
                                foreach (var item in xmlDocs)
                                {
                                    options.IncludeXmlComments(item);
                                }
                            }
                        });
                    }

                    //Mappings
                    services.ConfigureMappings();

                    //Business settings
                    services.ConfigureBusinessServices(Configuration);

                    _logger.LogDebug("Startup::ConfigureServices::ApiVersioning, Swagger and DI settings");
                }
                else
                {
                    _logger.LogDebug("Startup::ConfigureServices::invalid AppSettings");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }
        }