private ITypesResolver GetResolver(params Type[] types) { var resolverOptions = new TypesResolverOptions() { Assembly = Assembly.GetExecutingAssembly().GetName().Name, Types = types.Select(t => t.Name).ToArray() }; return(new TypesResolver(resolverOptions)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { Configuration = new ConfigurationBuilder() .AddEnvironmentVariables() .AddJsonFile("./appsettings.json") .Build(); var connectionString = Configuration["SQL_CONN_STR"] ?? "Server=192.168.1.191;Initial Catalog=nlayersappdb-tests;User Id=sa;Password=mrullerp!0"; TypesResolver resolver = getResolverFromConfig(nameof(TypesResolverOptions)); services.AddScoped <ITypesResolver>(s => resolver); services.AddScoped <IContext, TDbContext>(s => s.GetRequiredService <TDbContext>()); services.AddDbContext <TDbContext>( optionsAction: (s, o) => { o.UseSqlServer( connectionString: connectionString, sqlServerOptionsAction: b => { b.MigrationsAssembly("NLayersApp.SampleProject"); b.EnableRetryOnFailure(3); } ); //var builder = new ModelBuilder(new ConventionSet()); // foreach (var type in resolver.RegisteredTypes) // { // builder.Entity(type); // } // builder.FinalizeModel(); // o.UseModel(builder.Model); o.UseOpenIddict(); }, contextLifetime: ServiceLifetime.Scoped ); services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()));; services.AddMediatRHandlers(resolver); services.AddDynamicRolesAuthorizationServices <TDbContext>(); services.AddControllers(c => c.AddDynamicRolesAuthorizationFilter <TDbContext>()) .UseDynamicControllers(resolver) .AddControllersAsServices(); services.ConfigureAuthenticationAndAuthorisation <IdentityUser, IdentityRole, string, TDbContext>(); TypesResolver getResolverFromConfig(string configSection) { services.AddOptions <TypesResolverOptions>(configSection); TypesResolverOptions resolverOptions = new TypesResolverOptions(); Configuration.GetSection(nameof(TypesResolverOptions)).Bind(resolverOptions); foreach (var definition in resolverOptions.TypesDefinitions) { definition.Assembly = appendToExecutionPath(definition.Assembly); } var resolver = new TypesResolver(resolverOptions); return(resolver); } string appendToExecutionPath(string filename) { var assembly_location = Assembly.GetEntryAssembly().Location; var latstIndexOf_slash = Assembly.GetEntryAssembly().Location.LastIndexOf('/'); return($"{assembly_location.Remove(latstIndexOf_slash + 1)}{filename}"); } }