Example #1
0
        internal static StartupMethodsMultitenant <TTenant> LoadMethods <TTenant>(IServiceProvider hostingServiceProvider, Type startupType, string environmentName)
        {
            StartupMethods methods = StartupLoader.LoadMethods(hostingServiceProvider, startupType, environmentName);
            ConfigureMultitenantServicesBuilder <TTenant> servicesMethosPerTenant = FindConfigurePerTenantServicesDelegate <TTenant>(startupType, null, environmentName);

            return(new StartupMethodsMultitenant <TTenant>(methods, servicesMethosPerTenant.Build(methods.StartupInstance)));
        }
Example #2
0
        public SelenuimSampleTests()
        {
            this.tokenSource = new CancellationTokenSource();

            string projectName = typeof(Web.Startup).Assembly.GetName().Name;

            string currentDirectory    = Directory.GetCurrentDirectory();
            string webProjectDirectory = Path.GetFullPath(Path.Combine(currentDirectory, $@"..\..\..\..\{projectName}"));

            IWebHost webHost = WebHost.CreateDefaultBuilder(new string[0])
                               .UseSetting(WebHostDefaults.ApplicationKey, projectName)
                               .UseContentRoot(webProjectDirectory) // This will make appsettings.json to work.
                               .ConfigureServices(services =>
            {
                services.AddSingleton(typeof(IStartup), serviceProvider =>
                {
                    IHostingEnvironment hostingEnvironment = serviceProvider.GetRequiredService <IHostingEnvironment>();
                    StartupMethods startupMethods          = StartupLoader.LoadMethods(
                        serviceProvider,
                        typeof(TestStartup),
                        hostingEnvironment.EnvironmentName);

                    return(new ConventionBasedStartup(startupMethods));
                });
            })
                               .UseEnvironment(EnvironmentName.Development)
                               .UseUrls(TestLocalHostUrl)
                               //// .UseStartup<Web.TestStartUp>() // It's not working
                               .Build();

            webHost.RunAsync(this.tokenSource.Token);
        }
        private static void PrepareApplicationAndRoutes(StartupMethods startupMethods)
        {
            var applicationBuilder = new MockedApplicationBuilder(serviceProvider);

            startupMethods?.ConfigureDelegate?.Invoke(applicationBuilder);

            AdditionalApplicationConfiguration?.Invoke(applicationBuilder);

            var routeBuilder = new RouteBuilder(applicationBuilder)
            {
                DefaultHandler = new RouteHandler(NullHandler)
            };

            for (int i = 0; i < applicationBuilder.Routes.Count; i++)
            {
                var route = applicationBuilder.Routes[i];
                routeBuilder.Routes.Add(route);
            }

            AdditionalRoutes?.Invoke(routeBuilder);

            if (StartupType == null || routeBuilder.Routes.Count == 0)
            {
                routeBuilder.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routeBuilder.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(serviceProvider));
            }

            router = routeBuilder.Build();
        }
Example #4
0
 public HisarConventionBasedStartup(StartupMethods methods, Type startupType, IServiceProvider sp)
     : base(methods)
 {
     _sp                     = sp;
     _startupType            = startupType;
     _configureRoutes        = StartupTypeLoader.GetConfigureRoutesMethod(_startupType);
     ConfigureRoutesDelegate = routes => ConfigureRoutes(routes);
 }
Example #5
0
 public StartupMethodsMultitenant(StartupMethods methods, Action <IServiceCollection, TTenant> configurePerTenantServices)
     : this(
         methods.StartupInstance,
         methods.ConfigureDelegate,
         methods.ConfigureServicesDelegate,
         configurePerTenantServices)
 {
 }
Example #6
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            StartupMethods.IssueFines();
        }
Example #7
0
        public WebHostBuilder UseStartup(Action <IApplicationBuilder> configureApp, Func <IServiceCollection, IServiceProvider> configureServices)
        {
            if (configureApp == null)
            {
                throw new ArgumentNullException(nameof(configureApp));
            }

            _startup = new StartupMethods(configureApp, configureServices);
            return(this);
        }
 private StartupMethods SetStartupMethods(IServiceProvider services)
 {
     if (methods == null && inner != null)
     {
         methods = StartupLoader.LoadMethods(
             services,
             inner,
             services.GetRequiredService <IHostingEnvironment>().EnvironmentName);
     }
     return(methods);
 }
Example #9
0
 public WebHostBuilder UseStartup([NotNull] Action <IApplicationBuilder> configureApp, Action <IServiceCollection> configureServices)
 {
     _startup = new StartupMethods(configureApp,
                                   services =>
     {
         if (configureServices != null)
         {
             configureServices(services);
         }
         return(services.BuildServiceProvider());
     });
     return(this);
 }
Example #10
0
        public static TestApplicationContext BuildApplication(Action <IConfigurationBuilder> customConfiguration, Action <IServiceCollection> serviceConfiguration, Action <IApplicationBuilder, IHostingEnvironment, ILoggerFactory> serviceCustomConfiguration, string environmentName = "Production")
        {
            StartupMethods      startup      = null;
            IApplicationBuilder bootingupApp = null;
            var bootstrapDiagnosticMessages  = new List <string>();


            var testApp = new TestApplicationContext
            {
                LoggerFactory          = new StubLoggerFactory(),
                ApplicationEnvironment = CreateApplicationEnvironment(),
                HostingEnvironment     = new HostingEnvironment {
                    EnvironmentName = environmentName
                }
            };

            testApp.Configuration = BuildConfiguration(testApp.HostingEnvironment, testApp.ApplicationEnvironment, customConfiguration);


            Func <IServiceCollection, IServiceProvider> configureServices = services =>
            {
                services.AddInstance <ILoggerFactory>(testApp.LoggerFactory);
                services.AddInstance <IApplicationEnvironment>(testApp.ApplicationEnvironment);

                var loader = new StartupLoader(services.BuildServiceProvider(), testApp.HostingEnvironment);
                startup = loader.LoadMethods(typeof(Startup), bootstrapDiagnosticMessages);
                startup.ConfigureServicesDelegate(services);
                serviceConfiguration(services);

                testApp.ApplicationServices = services.BuildServiceProvider();
                return(testApp.ApplicationServices);
            };
            Action <IApplicationBuilder> configure = app =>
            {
                bootingupApp = app;
                startup.ConfigureDelegate(app);
                serviceCustomConfiguration(app, testApp.HostingEnvironment, testApp.ApplicationServices.GetService <ILoggerFactory>());
            };


            var webHostBuilder = TestServer
                                 .CreateBuilder(testApp.Configuration)
                                 .UseEnvironment(environmentName)
                                 .UseStartup(configure, configureServices);

            testApp.Server         = new TestServer(webHostBuilder);
            testApp.RequestHandler = bootingupApp.Build();

            return(testApp);
        }
        private static void PrepareServices(IServiceCollection serviceCollection, StartupMethods startupMethods)
        {
            if (startupMethods?.ConfigureServicesDelegate != null)
            {
                startupMethods.ConfigureServicesDelegate(serviceCollection);
            }
            else
            {
                var defaultRegistrationPlugin = DefaultRegistrationPlugins
                                                .OrderByDescending(p => p.Priority)
                                                .FirstOrDefault();

                if (defaultRegistrationPlugin != null)
                {
                    defaultRegistrationPlugin.DefaultServiceRegistrationDelegate(serviceCollection);
                }
                else
                {
                    serviceCollection
                    .AddMvcCore()
                    .AddFormatterMappings()
                    .AddJsonFormatters();
                }
            }

            AdditionalServices?.Invoke(serviceCollection);

            // custom MVC options
            serviceCollection.Configure <MvcOptions>(options =>
            {
                // add controller conventions to save all valid controller types
                options.Conventions.Add(new ValidControllersCache());

                // string input formatter helps with HTTP request processing
                var inputFormatters = options.InputFormatters.OfType <TextInputFormatter>();
                if (!inputFormatters.Any(f => f.SupportedMediaTypes.Contains(ContentType.TextPlain)))
                {
                    options.InputFormatters.Add(new StringInputFormatter());
                }
            });

            TryReplaceKnownServices(serviceCollection);
            PrepareRouteServices(serviceCollection);

            serviceProvider = serviceCollection.BuildServiceProvider();

            // this call prepares all application conventions and fills the controller action descriptor cache
            serviceProvider.GetService <IControllerActionDescriptorCache>();
        }
        private static void PrepareServices(IServiceCollection serviceCollection, StartupMethods startupMethods)
        {
            if (startupMethods?.ConfigureServicesDelegate != null)
            {
                startupMethods.ConfigureServicesDelegate(serviceCollection);
            }
            else
            {
                var defaultRegistrationPlugin = DefaultRegistrationPlugins
                                                .OrderByDescending(p => p.Priority)
                                                .FirstOrDefault();

                if (defaultRegistrationPlugin != null)
                {
                    defaultRegistrationPlugin.DefaultServiceRegistrationDelegate(serviceCollection);
                }
                else
                {
                    serviceCollection.AddMvcCore();
                }
            }

            AdditionalServices?.Invoke(serviceCollection);

            TryReplaceKnownServices(serviceCollection);
            PrepareRoutingServices(serviceCollection);

#if NET451
            var baseStartupType = StartupType;
            while (baseStartupType != null && baseStartupType?.BaseType != typeof(object))
            {
                baseStartupType = baseStartupType.BaseType;
            }

            var applicationPartManager = (ApplicationPartManager)serviceCollection
                                         .FirstOrDefault(t => t.ServiceType == typeof(ApplicationPartManager))
                                         ?.ImplementationInstance;

            if (applicationPartManager != null && baseStartupType != null)
            {
                applicationPartManager.ApplicationParts.Add(new AssemblyPart(baseStartupType.GetTypeInfo().Assembly));
            }
#endif

            serviceProvider = serviceCollection.BuildServiceProvider();

            InitializationPlugins.ForEach(plugin => plugin.InitializationDelegate(serviceProvider));
        }
Example #13
0
 public DelegatedStatelessWebServiceStartup(
     IServiceProvider provider,
     IHostingEnvironment env,
     Action <IServiceCollection> configureServices)
 {
     _configureServices = configureServices;
     if (typeof(TStartup).IsAssignableTo <IStartup>())
     {
         _startupImplementation = (IStartup)ActivatorUtilities.CreateInstance <TStartup>(provider);
     }
     else
     {
         StartupMethods methods = StartupLoader.LoadMethods(provider, typeof(TStartup), env.EnvironmentName);
         _startupImplementation = new ConventionBasedStartup(methods);
     }
 }
Example #14
0
        public WebHostBuilder UseStartup(Action <IApplicationBuilder> configureApp, Action <IServiceCollection> configureServices)
        {
            if (configureApp == null)
            {
                throw new ArgumentNullException(nameof(configureApp));
            }

            _startup = new StartupMethods(configureApp,
                                          services =>
            {
                if (configureServices != null)
                {
                    configureServices(services);
                }
                return(services.BuildServiceProvider());
            });
            return(this);
        }
Example #15
0
        private static void Initialize()
        {
            var serviceCollection = GetInitialServiceCollection();

            StartupMethods startupMethods = null;

            if (StartupType != null)
            {
                startupMethods = serviceCollection
                                 .BuildServiceProvider()
                                 .GetRequiredService <IStartupLoader>()
                                 .LoadMethods(StartupType, null);
            }

            PrepareServices(serviceCollection, startupMethods);
            PrepareApplicationAndRoutes(startupMethods);

            initialiazed = true;
        }
        private static StartupMethods PrepareStartup(IServiceCollection serviceCollection)
        {
            StartupMethods startupMethods = null;

            if (StartupType != null)
            {
                startupMethods = StartupLoader.LoadMethods(
                    serviceCollection.BuildServiceProvider(),
                    StartupType,
                    Environment.EnvironmentName);

                if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(StartupType.GetTypeInfo()))
                {
                    serviceCollection.AddSingleton(typeof(IStartup), StartupType);
                }
                else
                {
                    serviceCollection.AddSingleton(typeof(IStartup), sp => new ConventionBasedStartup(startupMethods));
                }
            }

            return(startupMethods);
        }
Example #17
0
 public ConventionBasedStartup(StartupMethods methods)
 {
     _methods = methods;
 }
Example #18
0
 public WebHostBuilder UseStartup([NotNull] Action <IApplicationBuilder> configureApp, Func <IServiceCollection, IServiceProvider> configureServices)
 {
     _startup = new StartupMethods(configureApp, configureServices);
     return(this);
 }
Example #19
0
 /// <summary>
 /// Specify the assembly containing the startup type to be used by the console application
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public ConsoleApplicationBuilder UseStartup <T>()
 {
     //TODO setup environment names
     StartupMethods = StartupLoader.LoadMethods(ServiceProvider, typeof(T), "");
     return(this);
 }
Example #20
0
 public TestHostStartupWrapper(TestHost <TStartup> testHost, StartupMethods startupMethods)
 {
     _testHost       = testHost;
     _startupMethods = startupMethods;
 }