public static void Initialize()
        {
            var container = new Container();
            container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

            // Chamada dos módulos do Simple Injector
            InitializeContainer(container);

            // Necessário para registrar o ambiente do Owin que é dependência do Identity
            // Feito fora da camada de IoC para não levar o System.Web para fora
            container.RegisterPerWebRequest(() =>
            {
                if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null && container.IsVerifying())
                {
                    return new OwinContext().Authentication;
                }
                return HttpContext.Current.GetOwinContext().Authentication;

            });

            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

            container.Verify();

            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
        }
예제 #2
0
 public void RegisterServices(Container container)
 {
     container.RegisterPerWebRequest<IUserStore<User>>(() => new UserStore<User>(container.GetInstance<IdentityDbContext<User>>()));
     container.RegisterPerWebRequest(() => container.IsVerifying()
         ? new OwinContext(new Dictionary<string, object>()).Authentication
         : HttpContext.Current.GetOwinContext().Authentication);
 }
        public static void Register(HttpConfiguration config)
        {
            using (var container = new Container())
            {
                container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();

                // Chamada dos módulos do Simple Injector
                BootStrapper.RegisterServices(container);

                // Necessário para registrar o ambiente do Owin que é dependência do Identity
                // Feito fora da camada de IoC para não levar o System.Web para fora
                container.RegisterPerWebRequest(() =>
                {
                    if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null && container.IsVerifying())
                    {
                        return new OwinContext().Authentication;
                    }
                    return HttpContext.Current.GetOwinContext().Authentication;

                });

                // This is an extension method from the integration package.
                container.RegisterWebApiControllers(config);

                container.Verify();

                GlobalConfiguration.Configuration.DependencyResolver =
                    new SimpleInjectorWebApiDependencyResolver(container);
            }
        }
        public static void ConfigureIoC(HttpConfiguration config, Container container)
        {
            container.Options.DefaultScopedLifestyle = new ExecutionContextScopeLifestyle();
            container.RegisterWebApiRequest(() =>
            {
                if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null && container.IsVerifying())
                {
                    return new OwinContext().Authentication;
                }
                return HttpContext.Current.GetOwinContext().Authentication;

            });
            container.RegisterWebApiControllers(config, Assembly.GetExecutingAssembly());
            container.Register<ApplicationDbContext>(Lifestyle.Scoped);
            container.RegisterWebApiRequest<IUserStore<ApplicationUser>>(() => new UserStore<ApplicationUser>(new ApplicationDbContext()));
            container.Register<ApplicationUserManager>(Lifestyle.Scoped);
            container.Register<ApplicationSignInManager>(Lifestyle.Scoped);
            container.Verify();
            config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
        }
        public static void Initialize()
        {
            var container = new Container();
            container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

            InitializeContainer(container);

            container.RegisterPerWebRequest(() =>
            {
                if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null && container.IsVerifying())
                {
                    return new OwinContext().Authentication;
                }

                return HttpContext.Current.GetOwinContext().Authentication;
            });

            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
            container.Verify();

            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
        }
예제 #6
0
        public Container GetInitializedContainer(IAppBuilder app)
        {
            var container = new Container();

            container.RegisterSingle(app);


            //allows objects to be reused when inside web request, or created fresh when used on background threads or outside a request context
            var hybridLifestyle = Lifestyle.CreateHybrid(
                () => HttpContext.Current != null, new WebRequestLifestyle(), Lifestyle.Transient);

            container.RegisterPerWebRequest<TicketDeskContextSecurityProvider>();

            container.Register(() => new TdPushNotificationContext(), hybridLifestyle);

            container.Register(() => HttpContext.Current != null ?
                    new TdDomainContext(container.GetInstance<TicketDeskContextSecurityProvider>()) :
                    new TdDomainContext(),
                hybridLifestyle);

            container.Register(() => new TdIdentityContext(), hybridLifestyle);

            container.Register<IUserStore<TicketDeskUser>>(() =>
                new UserStore<TicketDeskUser>(container.GetInstance<TdIdentityContext>()), 
                hybridLifestyle);

            container.Register<IRoleStore<TicketDeskRole, string>>(() =>
                new RoleStore<TicketDeskRole>(container.GetInstance<TdIdentityContext>()), 
                hybridLifestyle);


            container.RegisterPerWebRequest(() =>
            {
                IOwinContext context;
                try
                {
                    context = HttpContext.Current.GetOwinContext();
                }
                catch (InvalidOperationException)
                {
                    //avoid exception when this is called before the owin environment is fully initialized
                    if (container.IsVerifying())
                    {
                        return new FakeAuthenticationManager();
                    }
                    throw;
                }

                return context.Authentication;
            }
                );

            container.RegisterPerWebRequest<SignInManager<TicketDeskUser, string>, TicketDeskSignInManager>();

            container.RegisterPerWebRequest<TicketDeskRoleManager>();

            container.RegisterInitializer<TicketDeskUserManager>(manager =>
                manager.ConfigureDataProtection(app));


            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

            return container;
        }