- [Lifestyle Mismatch] ApplicationSignInManager (Web Request) depends on IAuthenticationManager (Transient)

2

I'm getting an error initializing simpleinjector that I can not solve at all. The error appears when it tries to execute the line:

container.Verify();

Following error:

  

- [Lifestyle Mismatch] ApplicationSignInManager (Web Request) depends on IAuthenticationManager (Transient).

SimpleInjectorInitializer class:

using CCValemixEF.Infra.IoC;
using CCValemixEF.UI.Web.App_Start;
using Microsoft.Owin;
using SimpleInjector;
using SimpleInjector.Integration.Web;
using SimpleInjector.Integration.Web.Mvc;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using WebActivatorEx;

[assembly: PostApplicationStartMethod(typeof(SimpleInjectorInitializer), "Initialize")]

namespace CCValemixEF.UI.Web.App_Start
{
    public static class SimpleInjectorInitializer
    {
        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.Register(() =>
            {
                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(); << Aqui acontece o erro.

            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
        }

        private static void InitializeContainer(Container container)
        {
            BootStrapper.RegisterServices(container);
        }
    }
}

BootStrapper class:

using CCValemixEF.Application.AppInterface; using CCValemixEF.Application.AppServices; using CCValemixEF.Domain.Interfaces.Contracts; using CCValemixEF.Domain.Interfaces.Services; using CCValemixEF.Domain.Services; using CCValemixEF.Infra.Data.Context; using CCValemixEF.Infra.Data.Identity.Configuration; using CCValemixEF.Infra.Data.Identity.Model; using CCValemixEF.Infra.Data.Repository; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Owin; using Microsoft.Owin.Security; using SimpleInjector; using SimpleInjector.Advanced; using System.Collections.Generic; using System.Net; using System.Web;

namespace CCValemixEF.Infra.IoC {
    public class BootStrapper
    {
        public static void RegisterServices(Container container)
        {
            #region Defaults
            container.Register<ApplicationDbContext>(Lifestyle.Scoped);

            container.Register<IUserStore<ApplicationUser>>(() => new UserStore<ApplicationUser>(new ApplicationDbContext()), Lifestyle.Scoped);
            container.Register<IRoleStore<IdentityRole, string>>(() => new RoleStore<IdentityRole>(), Lifestyle.Scoped);

            container.Register<ApplicationRoleManager>(Lifestyle.Scoped);
            container.Register<ApplicationUserManager>(Lifestyle.Scoped);
            container.Register<ApplicationSignInManager>(Lifestyle.Scoped);
            #endregion

            #region Aplicação
            container.Register(typeof(IAppServiceBase<>), typeof(AppServiceBase<>));
            container.Register<IFilialAppService, FilialAppService>(Lifestyle.Scoped);
            #endregion

            #region Dominio
            container.Register(typeof(IServiceBase<>), typeof(ServiceBase<>));
            container.Register<IFilialService, FilialService>(Lifestyle.Scoped);
            #endregion

            #region Repositories
            container.Register(typeof(IRepositoryBase<>), typeof(RepositoryBasse<>));
            container.Register<IFilialRepository, FilialRepository>(Lifestyle.Scoped);
            #endregion
        }
    } }

Could someone tell me where I'm going wrong?

    
asked by anonymous 18.01.2017 / 01:40

1 answer

3

I believe the error happens when you register your owin authentications , using the Register method without passing your lifestyle parameter, it defaults to Transient , in this line:

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

            });

As in this line here:

container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

You are registering your container with a WebRequest lifestyle, which has a life greater than that of your Owin Authentications, this error is occurring, according to the documentation Diagnostic Warning - Lifestyle Mismatches . This link gives you more information about the problem and the list of lifestyles sorted by their lifetime.

I recommend that in the Register method put a longer lifestyle, for example, LifeStyle.Scoped :

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

            }, LifeStyle.Scoped);

link

Here are some more examples if you'd like more information, in addition to the documentation links posted earlier:

link

    
18.01.2017 / 11:49