Ejemplo n.º 1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            IContainer applicationContainer = null;

            services.AddAutoMapper();
            services.AddMvc(options =>
            {
                options.Filters.Add(typeof(ApiExceptionAttribute));
            })
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            });

            services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority            = _configuration["Auth:Client:Url"];
                options.RequireHttpsMetadata = !_hostingEnvironment.IsDevelopment();
                options.ApiName = "api";
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "FWTL.Api", Version = "v1"
                });
                c.MapType <Guid>(() => new Schema()
                {
                    Type = "string", Format = "text", Description = "GUID"
                });

                c.AddSecurityDefinition("Bearer", new ApiKeyScheme()
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Bearer {token}\"",
                    Name        = "Authorization",
                    In          = "header",
                    Type        = "apiKey"
                });

                c.OperationFilter <AuthorizeOperationFilter>();
                c.DescribeAllEnumsAsStrings();
            });

            services.ConfigureSwaggerGen(options =>
            {
                options.CustomSchemaIds(type => type.FullName);
                options.DescribeAllEnumsAsStrings();
            });

            services.AddDbContext <JobDatabaseContext>();

            applicationContainer = IocConfig.RegisterDependencies(services, _hostingEnvironment, _configuration);

            var cache = applicationContainer.Resolve <IServer>();

            cache.FlushDatabase();

            return(new AutofacServiceProvider(applicationContainer));
        }
Ejemplo n.º 2
0
Archivo: Startup.cs Proyecto: FWTL/API
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();

            services.AddControllers(configuration =>
            {
                configuration.Filters.Add(new ApiExceptionFilterFactory(_hostingEnvironment.EnvironmentName));
            });

            //.AddJsonOptions(o =>
            //{
            //    o.JsonSerializerOptions.Con;
            //    o.JsonSerializerOptions.IgnoreNullValues = true;
            //});

            var defaultSettings = new JsonSerializerSettings()
                                  .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

            JsonConvert.DefaultSettings = () => defaultSettings;

            const string format =
                "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {NewLine}{Message:lj}{NewLine}{Exception}";

            services.AddSingleton <ILogger>(b => new LoggerConfiguration()
                                            .MinimumLevel.Information()
                                            .WriteTo.Console(outputTemplate: format)
                                            .WriteTo.Seq(_configuration["Seq:Url"])
                                            .CreateLogger());

            services.AddAutoMapper(
                config =>
            {
                config.AddProfile(new RequestToCommandProfile(typeof(RegisterUser)));
                config.AddProfile(new RequestToQueryProfile(typeof(RegisterUser)));
            }, typeof(RequestToCommandProfile).Assembly);

            services.AddDbContext <ApiDatabaseContext>();

            services.AddIdentity <User, Role>(options =>
            {
                options.Password.RequiredLength         = 8;
                options.Password.RequireLowercase       = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireDigit           = false;
            })
            .AddEntityFrameworkStores <ApiDatabaseContext>()
            .AddDefaultTokenProviders();

            services.AddAuthentication()
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority            = "http://localhost:5000";
                options.ApiName              = "api";
                options.RequireHttpsMetadata = false;
            });

            IocConfig.RegisterDependencies(services, _hostingEnvironment);

            services.AddMassTransit(x =>
            {
                var commands = typeof(RegisterUser).Assembly.GetTypes().Where(t => typeof(ICommand).IsAssignableFrom(t))
                               .ToList();

                x.AddConsumers(typeof(CommandConsumer <RegisterUser.Command>));
                //x.AddConsumers(typeof(QueryConsumer<GetMe.Query, GetMe.Result>));

                x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    cfg.ConfigureJsonSerializer(config =>
                    {
                        config.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
                        return(config);
                    });

                    cfg.ConfigureJsonDeserializer(config =>
                    {
                        config.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
                        return(config);
                    });

                    var host = cfg.Host(_configuration["RabbitMq:Url"], h =>
                    {
                        h.Username(_configuration["RabbitMq:Username"]);
                        h.Password(_configuration["RabbitMq:Password"]);
                    });

                    cfg.ReceiveEndpoint("commands", ec =>
                    {
                        ec.ConfigureConsumer(context, typeof(CommandConsumer <RegisterUser.Command>));
                    });

                    //cfg.ReceiveEndpoint("queries", ec =>
                    //{
                    //    ec.ConfigureConsumer(context, typeof(QueryConsumer<GetMe.Query, GetMe.Result>));
                    //});
                }));
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo()
                {
                    Title = "FWTL.Api", Version = "v1"
                });
                c.CustomSchemaIds(x =>
                {
                    int plusIndex      = x.FullName.IndexOf("+");
                    int lastIndexOfDot = x.FullName.LastIndexOf(".");
                    int length         = 0;

                    if (plusIndex != -1)
                    {
                        length = plusIndex - lastIndexOfDot - 1;
                    }
                    else
                    {
                        length = x.FullName.Length - lastIndexOfDot - 1;
                    }

                    return(x.FullName.Substring(lastIndexOfDot + 1, length));
                });
            });
        }