Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var config = new KavaDocsConfiguration();

            Configuration.Bind("KavaDocs", config);
            services.AddSingleton(config);

            KavaDocsConfiguration.Current = config;

            services.AddDbContext <KavaDocsContext>(builder =>
            {
                var connStr = config.ConnectionString;
                if (string.IsNullOrEmpty(connStr))
                {
                    connStr = "server=.;database=KavaDocs; integrated security=true;MultipleActiveResultSets=true";
                }
                //connStr = "";

                builder.UseSqlServer(connStr, opt =>
                {
                    opt.EnableRetryOnFailure();
                    opt.CommandTimeout(15);
                });
            });

            services.AddTransient <UserBusiness>();
            services.AddTransient <RepositoryBusiness>();
            services.AddTransient <OrganizationBusiness>();

            Task.Run(() =>
            {
                // preload data on separate thread if possible
                var ctx = KavaDocsContext.GetKavaDocsContext(config.ConnectionString);
                DatabaseCreator.EnsureKavaDocsData(ctx);
                ctx.Users.Any(p => p.Id == Guid.NewGuid());
            });

            // set up and configure Authentication - make sure to call .UseAuthentication()
            services
            .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)

            .AddCookie(o =>
            {
                o.LoginPath         = "/account/signin";
                o.LogoutPath        = "/account/signout";
                o.SlidingExpiration = true;
                o.ExpireTimeSpan    = new TimeSpan(2, 0, 0, 0);
            });

            services.AddMvc()
            .AddJsonOptions(opt =>
            {
                opt.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
                opt.SerializerSettings.Converters.Add(new StringEnumConverter()
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                });
            })
            .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1);
        }
Пример #2
0
        public static KavaDocsContext GetContext()
        {
            var options = new DbContextOptionsBuilder <KavaDocsContext>()
                          .UseSqlServer(ConnectionString)
                          .Options;

            var ctx = new KavaDocsContext(options);

            DatabaseCreator.EnsureKavaDocsData(ctx);
            return(ctx);
        }