Пример #1
0
        public static IUkiyoBuilder AddWebApi(this IUkiyoBuilder builder, Action <IMvcCoreBuilder> configureMvc, IJsonSerializer jsonSerializer = null,
                                              string sectionName = SectionName)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            if (!builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            if (jsonSerializer is null)
            {
                var factory = new JsonSerializerFactory(StandardResolver.AllowPrivateCamelCase);
                JsonSerializer.SetDefaultResolver(new UkiyoFormatterResolver());
                jsonSerializer = factory.GetSerializer();
            }

            builder.Services.AddSingleton(jsonSerializer);
            builder.Services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            builder.Services.AddSingleton(new WebApiEndpointDefinitions());
            var options = builder.GetOptions <WebApiOptions>(sectionName);

            builder.Services.AddSingleton(options);
            _bindRequestFromRoute = options.BindRequestFromRoute;

            var mvcCoreBuilder = builder.Services
                                 .AddLogging()
                                 .AddMvcCore();

            mvcCoreBuilder.AddMvcOptions(o =>
            {
                var resolver = CompositeResolver.Create(EnumResolver.Default, StandardResolver.AllowPrivateCamelCase);
                o.OutputFormatters.Clear();
                o.OutputFormatters.Add(new JsonOutputFormatter(resolver));
                o.InputFormatters.Clear();
                o.InputFormatters.Add(new JsonInputFormatter(resolver));
            })
            .AddDataAnnotations()
            .AddApiExplorer()
            .AddAuthorization();

            configureMvc?.Invoke(mvcCoreBuilder);

            builder.Services.Scan(s =>
                                  s.FromAssemblies(AppDomain.CurrentDomain.GetAssemblies())
                                  .AddClasses(c => c.AssignableTo(typeof(IRequestHandler <,>)))
                                  .AsImplementedInterfaces()
                                  .WithTransientLifetime());

            builder.Services.AddTransient <IRequestDispatcher, RequestDispatcher>();

            return(builder);
        }
Пример #2
0
        public static IUkiyoBuilder AddSwaggerDocs(this IUkiyoBuilder builder, string sectionName = SectionName)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            var options = builder.GetOptions <SwaggerOptions>(sectionName);

            return(builder.AddSwaggerDocs(options));
        }
Пример #3
0
        public static IUkiyoBuilder AddDbContext <TDbContext>(this IUkiyoBuilder builder, string configurationSectionName = SectionName) where TDbContext : DbContext
        {
            var options = builder.GetOptions <DatabaseOptions>(configurationSectionName);

            return(builder.AddDbContext <TDbContext>(options));
        }
Пример #4
0
        public static IUkiyoBuilder AddRedis(this IUkiyoBuilder builder, string configurationSectionName = SectionName)
        {
            var options = builder.GetOptions <RedisOptions>(configurationSectionName);

            return(builder.AddRedis(options));
        }