예제 #1
0
 public void ConfigureContainer(ServiceRegistry serviceRegistry)
 {
     serviceRegistry.AddControllers();
     serviceRegistry.AddSwaggerDocument();
     serviceRegistry.IncludeRegistry <CTeleportServiceRegistry>();
     serviceRegistry.AddAuthorization();
 }
예제 #2
0
        public void ConfigureContainer(ServiceRegistry services)
        {
            // Supports ASP.Net Core DI abstractions
            services.AddAuthorization();
            services.AddControllers()
            .AddControllersAsServices();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "LamarExample", Version = "v1"
                });
            });

            // Also exposes Lamar specific registrations
            // and functionality
            services.Scan(s =>
            {
                // Automatically register services that follow default conventions, e.g.
                // PurchasingService/IPurchasingService
                // ConcreteService (concrete types can always be resolved)
                // Typically, you will have a lot of Service/IService pairs in your app
                s.AssemblyContainingType(typeof(Startup));
                s.WithDefaultConventions();
                // Register all of the implementations of IGamingService
                // CrosswordService
                // SudokuService
                s.AddAllTypesOf <IGamingService>();
                // Register all non-generic implementations of IValidatior<T> (UserModelValidator)
                s.ConnectImplementationsToTypesClosing(typeof(IValidator <>));
            });


            // When a ILeaderboard<T> is requested, use Leaderboard<T>
            // Equivalent to:
            // services.AddTransient(typeof(ILeaderboard<>), typeof(Leaderboard<>));
            services.For(typeof(ILeaderboard <>)).Use(typeof(Leaderboard <>));
            // When an IUnitOfWork<T> is requested, run the lambda
            // Also, has a "scoped" lifetime, instead of the default "transient" lifetime
            // Equivalent to:
            //services.AddScoped<IUnitOfWork>(_ => new UnitOfWork(3));
            services.For <IUnitOfWork>().Use(_ => new UnitOfWork(3)).Scoped();
            // For a given T, when an IValidator<T> is requested,
            // but there are no non-generic implementations of IValidator<T>
            // Use DefaultValidator<T> instead
            // No equivalent using the built-in container
            services.For(typeof(IValidator <>)).Add(typeof(DefaultValidator <>));
        }
        public static void Register(ServiceRegistry services, IConfiguration configuration)
        {
            services.Configure <ConfigModel>(configuration);
            services.AddHttpContextAccessor();

            services.AddAuthorization(options =>
            {
                options.AddPolicy("RequireAdminRole", policy => policy.RequireRole("Admin"));
                options.AddPolicy("RequireStandardRole", policy => policy.RequireRole("Standard"));
                options.AddPolicy("RequireDealerRole", policy => policy.RequireRole("Dealer"));
                options.AddPolicy("RequireCustomerRole", policy => policy.RequireRole("Customer"));
                options.AddPolicy("RequireDashboardUser", policy => policy.RequireClaim(ClaimType.CurrentUserRole, "Admin", "Dealer", "Standard"));
            });

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                var signingKey    = Convert.FromBase64String(configuration["Jwt:SigningSecret"]);
                var validIssuer   = configuration["Jwt:ValidIssuer"];
                var validAudience = configuration["Jwt:ValidAudience"];
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = validIssuer,
                    ValidateAudience         = true,
                    ValidAudience            = validAudience,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(signingKey),
                    ClockSkew = TimeSpan.Zero,       //dotnet gives 5-10 more minuts if ClockSkew is not set
                };
            });

            services.AddDefaultAWSOptions(configuration.GetAWSOptions());
            services.AddAWSService <IAmazonSimpleEmailService>();

            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new AutoMapperProfiles());
            });

            IMapper mapper = mappingConfig.CreateMapper();

            services.AddSingleton(mapper);

            services.AddSwaggerDocumentation();
        }