From 188b5a7095a0000898fb7cdcfd0086d277cb71d7 Mon Sep 17 00:00:00 2001 From: Aigamo <51428094+ycanardeau@users.noreply.github.com> Date: Tue, 22 Dec 2020 00:33:58 +1000 Subject: [PATCH] Update Startup --- VocaDbWeb.Core/Startup.cs | 125 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/VocaDbWeb.Core/Startup.cs b/VocaDbWeb.Core/Startup.cs index cf0651006e..3256179f03 100644 --- a/VocaDbWeb.Core/Startup.cs +++ b/VocaDbWeb.Core/Startup.cs @@ -1,11 +1,40 @@ #nullable disable +using System.Linq; +using System.Runtime.Caching; using Autofac; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using VocaDb.Model.Database.Queries; +using VocaDb.Model.Database.Repositories; +using VocaDb.Model.Database.Repositories.NHibernate; +using VocaDb.Model.DataContracts.Api; +using VocaDb.Model.DataContracts.Users; +using VocaDb.Model.Domain; +using VocaDb.Model.Domain.Globalization; +using VocaDb.Model.Domain.Images; +using VocaDb.Model.Domain.Security; +using VocaDb.Model.Domain.Web; +using VocaDb.Model.Service; +using VocaDb.Model.Service.BrandableStrings; +using VocaDb.Model.Service.ExtSites; +using VocaDb.Model.Service.Helpers; +using VocaDb.Model.Service.Security; +using VocaDb.Model.Service.Security.StopForumSpam; +using VocaDb.Model.Service.Translations; +using VocaDb.Model.Service.VideoServices; +using VocaDb.Model.Utils; +using VocaDb.Model.Utils.Config; +using VocaDb.Web.Code; +using VocaDb.Web.Code.Markdown; +using VocaDb.Web.Code.Security; +using VocaDb.Web.Helpers; namespace VocaDb.Web { @@ -24,8 +53,104 @@ public void ConfigureServices(IServiceCollection services) services.AddControllersWithViews(); } + private static string[] LoadBlockedIPs(IComponentContext componentContext) => componentContext.Resolve().HandleQuery(q => q.Query().Select(i => i.Address).ToArray()); + public void ConfigureContainer(ContainerBuilder builder) { + // Register services. + //builder.RegisterControllers(typeof(MvcApplication).Assembly); + //builder.RegisterType(); + //builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); + //builder.RegisterModule(new AutofacWebTypesModule()); + + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.Register(x => new UrlHelperFactory().GetUrlHelper(x.Resolve().ActionContext)); + + // TODO: re-enable cache + builder.Register(x => DatabaseConfiguration.BuildSessionFactory(useSysCache: false)).SingleInstance(); + + // Other dependencies (for repositories mostly) + builder.RegisterType().As(); + builder.RegisterType().AsSelf().As(); + builder.Register(x => new EntryAnchorFactory(AppConfig.HostAddress)).As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf(); + builder.Register(x => new IPRuleManager(LoadBlockedIPs(x))).AsSelf().SingleInstance(); + builder.Register(_ => MemoryCache.Default).As().ExternallyOwned(); // Disable dispose + builder.RegisterType().AsSelf(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + + // Legacy services + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + + // Repositories + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); + + // Enable DI for action filters + //builder.Register(c => new RestrictBlockedIPAttribute(c.Resolve())) + // .AsActionFilterFor().InstancePerRequest(); + + //builder.RegisterFilterProvider(); + + // Build container. + //var container = builder.Build(); + + // Set ASP.NET MVC dependency resolver. + //DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); // For ASP.NET MVC + //GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); // For Web API + //AutofacHostFactory.Container = container; // For WCF } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.