Exemple #1
0
        // Set up application services
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
            .WithControllersAsServices(
                new[]
            {
                typeof(TimeScheduleController).GetTypeInfo().Assembly
            });

            services.AddTransient <QueryValueService>();

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

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

            return(builder.Build()
                   .Resolve <IServiceProvider>());
#else
            return(services.BuildServiceProvider());
#endif
        }
Exemple #2
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 #3
0
        protected override IServiceProvider CreateContainer(IServiceProvider fallbackProvider)
        {
            var builder = new ContainerBuilder();

            AutofacRegistration.Populate(
                builder,
                TestServices.DefaultServices(),
                fallbackProvider);

            IContainer container = builder.Build();

            return(container.Resolve <IServiceProvider>());
        }
Exemple #4
0
        // Set up application services
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddTransient <HelloWorldBuilder>();

            var builder = new ContainerBuilder();

            AutofacRegistration.Populate(builder,
                                         services);

            var container = builder.Build();

            return(container.Resolve <IServiceProvider>());
        }
Exemple #5
0
        public void Configure(IBuilder app)
        {
            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterType <CallOne>().As <ICall>().SingleInstance();
            containerBuilder.RegisterType <CallTwo>().As <ICall>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <CallThree>().As <ICall>().InstancePerDependency();

            AutofacRegistration.Populate(containerBuilder, Enumerable.Empty <IServiceDescriptor>(), app.ApplicationServices);
            var container = containerBuilder.Build();

            app.UseServices(container.Resolve <IServiceProvider>());

            app.UseMiddleware(typeof(MyMiddleware));
            app.UseMiddleware(typeof(MyMiddleware));

            app.Run(async context =>
                    await context.Response.WriteAsync("---------- Done\r\n"));
        }
Exemple #6
0
        public void Configure(IApplicationBuilder app)
        {
            app.UseServices(services => {
                services.AddMvc();
                services.AddTransient<HelloWorldBuilder>();

                var builder = new ContainerBuilder();
                AutofacRegistration.Populate(builder, 
                                             services, 
                                             fallbackServiceProvider: app.ApplicationServices);

                var container = builder.Build();

                return container.Resolve<IServiceProvider>();
            });

            app.UseMvc(routes =>
            {
                // This default route is for running the project directly.
                routes.MapRoute("default", "{controller=DI}/{action=Index}");
            });
        }
Exemple #7
0
        public void Configure(IBuilder app)
        {
            app.UseFileServer();
#if NET45
            var configuration = new Configuration()
                                .AddJsonFile(@"App_Data\config.json")
                                .AddEnvironmentVariables();

            string diSystem;

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

                var services = new ServiceCollection();

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

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

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

                builder.RegisterModule <MonitoringModule>();

                IContainer container = builder.Build();

                app.UseServices(container.Resolve <IServiceProvider>());
            }
            else
#endif
            {
                app.UseServices(services =>
                {
                    services.AddMvc();
                    services.AddSingleton <PassThroughAttribute>();
                    services.AddSingleton <UserNameService>();
                    services.AddTransient <ITestService, TestService>();
                });
            }

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

                routes.MapRoute(
                    "controllerActionRoute",
                    "{controller}/{action}",
                    new { controller = "Home", action = "Index" });

                routes.MapRoute(
                    "controllerRoute",
                    "{controller}",
                    new { controller = "Home" });
            });
        }
Exemple #8
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" });
            });
        }