Exemple #1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddCaching();
            services.AddSession();

            services.AddMvc();
            services.AddSingleton <PassThroughAttribute>();
            services.AddSingleton <UserNameService>();
            services.AddTransient <ITestService, TestService>();

            services.ConfigureMvc(options =>
            {
                options.Filters.Add(typeof(PassThroughAttribute), order: 17);
                options.AddXmlDataContractSerializerFormatter();
                options.Filters.Add(new FormatFilterAttribute());
            });

#if DNX451
            // Fully-qualify configuration path to avoid issues in functional tests. Just "config.json" would be fine
            // but Configuration uses CallContextServiceLocator.Locator.ServiceProvider to get IApplicationEnvironment.
            // Functional tests update that service but not in the static provider.
            var applicationEnvironment = services.BuildServiceProvider().GetRequiredService <IApplicationEnvironment>();
            var configurationPath      = Path.Combine(applicationEnvironment.ApplicationBasePath, "config.json");

            // Set up configuration sources.
            var configuration = new Configuration()
                                .AddJsonFile(configurationPath)
                                .AddEnvironmentVariables();
            string diSystem;
            if (configuration.TryGet("DependencyInjection", out diSystem) &&
                diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
            {
                _autoFac = true;
                services.ConfigureRazorViewEngine(options =>
                {
                    var expander = new LanguageViewLocationExpander(
                        context => context.HttpContext.Request.Query["language"]);
                    options.ViewLocationExpanders.Insert(0, expander);
                });

                // Create the autofac container
                var builder = new ContainerBuilder();

                // Create the container and use the default application services as a fallback
                AutofacRegistration.Populate(
                    builder,
                    services);

                builder.RegisterModule <MonitoringModule>();

                var container = builder.Build();

                return(container.Resolve <IServiceProvider>());
            }
            else
#endif
            {
                return(services.BuildServiceProvider());
            }
        }
Exemple #2
0
        public void Configure(IApplicationBuilder app)
        {
            app.UseFileServer();
#if ASPNET50
            // We use Path.Combine here so that it works on platforms other than Windows as well.
            var configuration = new Configuration()
                                .AddJsonFile(Path.Combine("App_Data", "config.json"))
                                .AddEnvironmentVariables();
            string diSystem;

            if (configuration.TryGet("DependencyInjection", out diSystem) &&
                diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
            {
                app.UseMiddleware <MonitoringMiddlware>();

                app.UseServices(services =>
                {
                    services.AddMvc();
                    services.AddSingleton <PassThroughAttribute>();
                    services.AddSingleton <UserNameService>();
                    services.AddTransient <ITestService, TestService>();

                    // Setup services with a test AssemblyProvider so that only the
                    // sample's assemblies are loaded. This prevents loading controllers from other assemblies
                    // when the sample is used in the Functional Tests.
                    services.AddTransient <IAssemblyProvider, TestAssemblyProvider <Startup> >();
                    services.Configure <MvcOptions>(options =>
                    {
                        options.Filters.Add(typeof(PassThroughAttribute), order: 17);
                    });
                    services.Configure <RazorViewEngineOptions>(options =>
                    {
                        var expander = new LanguageViewLocationExpander(
                            context => context.HttpContext.Request.Query["language"]);
                        options.ViewLocationExpanders.Insert(0, expander);
                    });

                    // Create the autofac container
                    ContainerBuilder builder = new ContainerBuilder();

                    // Create the container and use the default application services as a fallback
                    AutofacRegistration.Populate(
                        builder,
                        services);

                    builder.RegisterModule <MonitoringModule>();

                    IContainer container = builder.Build();

                    return(container.Resolve <IServiceProvider>());
                });
            }
            else
#endif
            {
                app.UseServices(services =>
                {
                    services.AddMvc();
                    services.AddSingleton <PassThroughAttribute>();
                    services.AddSingleton <UserNameService>();
                    services.AddTransient <ITestService, TestService>();
                    // Setup services with a test AssemblyProvider so that only the
                    // sample's assemblies are loaded. This prevents loading controllers from other assemblies
                    // when the sample is used in the Functional Tests.
                    services.AddTransient <IAssemblyProvider, TestAssemblyProvider <Startup> >();

                    services.Configure <MvcOptions>(options =>
                    {
                        options.Filters.Add(typeof(PassThroughAttribute), order: 17);
                    });
                });
            }

            app.UseMvc(routes =>
            {
                routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");

                routes.MapRoute(
                    "controllerActionRoute",
                    "{controller}/{action}",
                    new { controller = "Home", action = "Index" },
                    constraints: null,
                    dataTokens: new { NameSpace = "default" });

                routes.MapRoute(
                    "controllerRoute",
                    "{controller}",
                    new { controller = "Home" });
            });
        }