/// <summary>
        /// Adds DotVVM to the <see cref="IApplicationBuilder" /> request execution pipeline.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder" /> instance.</param>
        /// <param name="startup">The <see cref="IDotvvmStartup" /> instance.</param>
        /// <param name="applicationRootPath">
        /// The path to application's root directory. It is used to resolve paths to views, etc.
        /// The default value is equal to <see cref="IHostingEnvironment.ContentRootPath" />.
        /// </param>
        /// <param name="useErrorPages">
        /// A value indicating whether to show detailed error page if an exception occurs. It is enabled by default
        /// if <see cref="HostingEnvironmentExtensions.IsDevelopment" /> returns <c>true</c>.
        /// </param>
        /// <param name="modifyConfiguration">An action that allows modifying configuration before it's frozen.</param>
        public static DotvvmConfiguration UseDotVVM(this IApplicationBuilder app, IDotvvmStartup startup, string applicationRootPath, bool?useErrorPages, Action <DotvvmConfiguration> modifyConfiguration = null)
        {
            var env    = app.ApplicationServices.GetRequiredService <IHostingEnvironment>();
            var config = app.ApplicationServices.GetRequiredService <DotvvmConfiguration>();

            config.Debug = env.IsDevelopment();
            config.ApplicationPhysicalPath = applicationRootPath ?? env.ContentRootPath;

            var startupTracer = app.ApplicationServices.GetRequiredService <IStartupTracer>();

            startupTracer.TraceEvent(StartupTracingConstants.DotvvmConfigurationUserConfigureStarted);
            startup.Configure(config, applicationRootPath);
            startupTracer.TraceEvent(StartupTracingConstants.DotvvmConfigurationUserConfigureFinished);

            if (useErrorPages ?? config.Debug)
            {
                app.UseMiddleware <DotvvmErrorPageMiddleware>();
            }

            modifyConfiguration?.Invoke(config);
            config.Freeze();

            startupTracer.TraceEvent(StartupTracingConstants.UseDotvvmStarted);
            app.UseMiddleware <DotvvmMiddleware>(config, new List <IMiddleware> {
                ActivatorUtilities.CreateInstance <DotvvmCsrfTokenMiddleware>(config.ServiceProvider),
                ActivatorUtilities.CreateInstance <DotvvmLocalResourceMiddleware>(app.ApplicationServices),
                DotvvmFileUploadMiddleware.TryCreate(app.ApplicationServices),
                new DotvvmReturnedFileMiddleware(),
                new DotvvmRoutingMiddleware()
            }.Where(t => t != null).ToArray());

            startupTracer.TraceEvent(StartupTracingConstants.UseDotvvmFinished);

            var compilationConfiguration = config.Markup.ViewCompilation;

            compilationConfiguration.HandleViewCompilation(config, startupTracer);

            if (config.ServiceProvider.GetService <IDiagnosticsInformationSender>() is IDiagnosticsInformationSender sender)
            {
                startupTracer.NotifyStartupCompleted(sender);
            }

            return(config);
        }
Example #2
0
        private static DotvvmConfiguration UseDotVVM(this IApplicationBuilder app, string applicationRootPath, bool?useErrorPages, IDotvvmStartup startup, Action <DotvvmConfiguration> modifyConfiguration)
        {
            var env    = app.ApplicationServices.GetRequiredService <IHostingEnvironment>();
            var config = app.ApplicationServices.GetRequiredService <DotvvmConfiguration>();

            config.Debug = env.IsDevelopment();
            config.ApplicationPhysicalPath = applicationRootPath ?? env.ContentRootPath;
            startup.Configure(config, applicationRootPath);

            if (useErrorPages ?? config.Debug)
            {
                app.UseMiddleware <DotvvmErrorPageMiddleware>();
            }

            modifyConfiguration?.Invoke(config);
            config.Freeze();

            app.UseMiddleware <DotvvmMiddleware>(config, new List <IMiddleware> {
                ActivatorUtilities.CreateInstance <DotvvmCsrfTokenMiddleware>(config.ServiceProvider),
                ActivatorUtilities.CreateInstance <DotvvmLocalResourceMiddleware>(app.ApplicationServices),
                DotvvmFileUploadMiddleware.TryCreate(app.ApplicationServices),
                new DotvvmReturnedFileMiddleware(),
                new DotvvmRoutingMiddleware()
            }.Where(t => t != null).ToArray());
            return(config);
        }
Example #3
0
        private static DotvvmConfiguration UseDotVVM(this IAppBuilder app, string applicationRootPath, bool useErrorPages, bool debug, IDotvvmServiceConfigurator configurator, IDotvvmStartup startup, Func <IServiceCollection, IServiceProvider> serviceProviderFactoryMethod = null, Action <DotvvmConfiguration> modifyConfiguration = null)
        {
            var startupTracer = new DiagnosticsStartupTracer();

            startupTracer.TraceEvent(StartupTracingConstants.AddDotvvmStarted);

            var config = DotvvmConfiguration.CreateDefault(s => {
                s.TryAddSingleton <IDataProtectionProvider>(p => new DefaultDataProtectionProvider(app));
                s.TryAddSingleton <IDotvvmCacheAdapter, DefaultDotvvmCacheAdapter>();
                s.TryAddSingleton <IViewModelProtector, DefaultViewModelProtector>();
                s.TryAddSingleton <ICsrfProtector, DefaultCsrfProtector>();
                s.TryAddSingleton <IEnvironmentNameProvider, DotvvmEnvironmentNameProvider>();
                s.TryAddSingleton <ICookieManager, ChunkingCookieManager>();
                s.TryAddSingleton <IRequestCancellationTokenProvider, RequestCancellationTokenProvider>();
                s.TryAddScoped <DotvvmRequestContextStorage>(_ => new DotvvmRequestContextStorage());
                s.TryAddScoped <IDotvvmRequestContext>(services => services.GetRequiredService <DotvvmRequestContextStorage>().Context);
                s.AddSingleton <IDotvvmViewCompilationService, DotvvmViewCompilationService>();
                s.TryAddSingleton <IStartupTracer>(startupTracer);

                startupTracer.TraceEvent(StartupTracingConstants.DotvvmConfigurationUserServicesRegistrationStarted);
                configurator?.ConfigureServices(new DotvvmServiceCollection(s));
                startupTracer.TraceEvent(StartupTracingConstants.DotvvmConfigurationUserServicesRegistrationFinished);
            }, serviceProviderFactoryMethod);

            config.Debug = debug;
            config.ApplicationPhysicalPath = applicationRootPath;

            startupTracer.TraceEvent(StartupTracingConstants.DotvvmConfigurationUserConfigureStarted);
            startup.Configure(config, applicationRootPath);
            startupTracer.TraceEvent(StartupTracingConstants.DotvvmConfigurationUserConfigureFinished);

            if (useErrorPages)
            {
                app.Use <DotvvmErrorPageMiddleware>();
            }

            modifyConfiguration?.Invoke(config);
            config.Freeze();

            startupTracer.TraceEvent(StartupTracingConstants.UseDotvvmStarted);

            app.Use <DotvvmMiddleware>(config, new List <IMiddleware> {
                ActivatorUtilities.CreateInstance <DotvvmCsrfTokenMiddleware>(config.ServiceProvider),
                ActivatorUtilities.CreateInstance <DotvvmLocalResourceMiddleware>(config.ServiceProvider),
                DotvvmFileUploadMiddleware.TryCreate(config.ServiceProvider),
                new DotvvmReturnedFileMiddleware(),
                new DotvvmRoutingMiddleware()
            }.Where(t => t != null).ToArray());

            startupTracer.TraceEvent(StartupTracingConstants.UseDotvvmFinished);

            var compilationConfiguration = config.Markup.ViewCompilation;

            compilationConfiguration.HandleViewCompilation(config, startupTracer);

            if (config.ServiceProvider.GetService <IDiagnosticsInformationSender>() is IDiagnosticsInformationSender sender)
            {
                startupTracer.NotifyStartupCompleted(sender);
            }

            return(config);
        }
Example #4
0
        private static DotvvmConfiguration UseDotVVM(this IAppBuilder app, string applicationRootPath, bool useErrorPages, bool debug, IDotvvmServiceConfigurator configurator, IDotvvmStartup startup, Func <IServiceCollection, IServiceProvider> serviceProviderFactoryMethod = null)
        {
            var config = DotvvmConfiguration.CreateDefault(s => {
                s.TryAddSingleton <IDataProtectionProvider>(p => new DefaultDataProtectionProvider(app));
                s.TryAddSingleton <IViewModelProtector, DefaultViewModelProtector>();
                s.TryAddSingleton <ICsrfProtector, DefaultCsrfProtector>();
                s.TryAddSingleton <IEnvironmentNameProvider, DotvvmEnvironmentNameProvider>();
                s.TryAddSingleton <ICookieManager, ChunkingCookieManager>();
                s.TryAddScoped <DotvvmRequestContextStorage>(_ => new DotvvmRequestContextStorage());
                s.TryAddScoped <IDotvvmRequestContext>(services => services.GetRequiredService <DotvvmRequestContextStorage>().Context);
                configurator?.ConfigureServices(new DotvvmServiceCollection(s));
            }, serviceProviderFactoryMethod);

            config.Debug = debug;
            config.ApplicationPhysicalPath = applicationRootPath;

            startup.Configure(config, applicationRootPath);


            if (useErrorPages)
            {
                app.Use <DotvvmErrorPageMiddleware>();
            }

            app.Use <DotvvmMiddleware>(config, new List <IMiddleware> {
                ActivatorUtilities.CreateInstance <DotvvmLocalResourceMiddleware>(config.ServiceProvider),
                DotvvmFileUploadMiddleware.TryCreate(config.ServiceProvider),
                new DotvvmReturnedFileMiddleware(),
                new DotvvmRoutingMiddleware()
            }.Where(t => t != null).ToArray());
            config.Freeze();
            return(config);
        }
Example #5
0
 /// <summary>
 /// Adds DotVVM to the <see cref="IAppBuilder" /> request execution pipeline.
 /// </summary>
 /// <param name="app">The <see cref="IAppBuilder" /> instance.</param>
 /// <param name="startup">The <see cref="IDotvvmStartup" /> instance.</param>
 /// <param name="applicationRootPath">The path to application's root directory. It is used to resolve paths to views, etc.</param>
 /// <param name="useErrorPages">
 /// A value indicating whether to show detailed error page if an exception occurs. Disable this
 /// in production.
 /// </param>
 /// <param name="debug">A value indicating whether the application should run in debug mode.</param>
 /// <param name="serviceProviderFactoryMethod">Register factory method to create your own instance of IServiceProvider.</param>
 /// <param name="modifyConfiguration">An action that allows modifying configuration before it's frozen.</param>
 public static DotvvmConfiguration UseDotVVM(this IAppBuilder app, IDotvvmStartup startup, string applicationRootPath, bool useErrorPages = true, bool debug = true, Func <IServiceCollection, IServiceProvider> serviceProviderFactoryMethod = null, Action <DotvvmConfiguration> modifyConfiguration = null)
 {
     return(app.UseDotVVM(applicationRootPath, useErrorPages, debug, startup as IDotvvmServiceConfigurator, startup, serviceProviderFactoryMethod, modifyConfiguration));
 }