Ejemplo n.º 1
0
        /// <summary>Initialize the container and register it as MVC3 Dependency Resolver.</summary>
        public static void RegisterDependencies()
        {
            var container = new Container(
                new ContainerOptions
                {
                    AllowOverridingRegistrations = true,
                });

            InitializeContainer(container);

            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

            container.RegisterMvcAttributeFilterProvider();
            container.RegisterHttpFilterProvider();

            // Using Entity Framework? Please read this: http://simpleinjector.codeplex.com/discussions/363935
            container.Verify();

            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorHttpDependencyResolver(container);

            InitializeData(container);
        }
Ejemplo n.º 2
0
        /// <summary>Initialize the container and register it as MVC3 Dependency Resolver.</summary>
        public static void RegisterDependencies()
        {
            // compose the root
            var container = new Container(
                new ContainerOptions
                {
                    AllowOverridingRegistrations = true,
                });

            var rootCompositionSettings = new RootCompositionSettings
            {
                Flags = RootCompositionFlags.Web |
                        RootCompositionFlags.Work,
                FluentValidationAssemblies = new[]
                {
                    Assembly.GetAssembly(typeof(IHandleCommands<>)),
                    Assembly.GetExecutingAssembly(),
                }
            };
            container.ComposeRoot(rootCompositionSettings);

            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
            container.RegisterMvcAttributeFilterProvider();
            container.RegisterHttpFilterProvider();

            container.Verify();

            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorHttpDependencyResolver(container);

            FluentValidationModelValidatorProvider.Configure(
                provider =>
                {
                    provider.ValidatorFactory = new FluentValidationValidatorFactory(container);
                    provider.AddImplicitRequiredValidator = false;
                }
            );

            // seed data
            if (rootCompositionSettings.Flags.HasFlag(RootCompositionFlags.Debug))
            {
                var seeder = container.GetInstance<ISeedData>();
                if (seeder != null) seeder.Seed();
            }

            // fire up poor man's worker role
            if (rootCompositionSettings.Flags.HasFlag(RootCompositionFlags.Work) &&
                rootCompositionSettings.Flags.HasFlag(RootCompositionFlags.Debug) &&
                !rootCompositionSettings.Flags.HasFlag(RootCompositionFlags.Azure))
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var cancellationToken = cancellationTokenSource.Token;
                Task.Factory.StartNew(() =>
                {
                    var workerRole = DependencyResolver.Current.GetService<WebDevelopmentWorkerRole>();
                    workerRole.OnStart();
                    workerRole.Run(cancellationToken);
                }, cancellationToken);

                AppDomain.CurrentDomain.DomainUnload += (sender, args) => cancellationTokenSource.Cancel();
            }
        }