-[Lifestyle Mismatch] Applicationsigninmanager (Web Request) depends on Iauthenticationmanager (Transient)

Asked

Viewed 501 times

3

I’m getting an error on startup simpleinjector which I cannot solve at all. The error appears when it tries to run the line:

container.Verify();

Follows error:

-[Lifestyle Mismatch] Applicationsigninmanager (Web Request) depends on Iauthenticationmanager (Transient).

Class Simpleinjectorinitializer:

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?

1 answer

4


I believe the error happens when you register yours owin authentications, using the method Register without passing as parameter his lifestyle, it assumes as standard the 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;

            });

Like this line here:

container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

you are registering your container with a lifestyle WebRequest, which has a longer life than that of its 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 method Register 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);

https://simpleinjector.org/ReferenceLibrary/html/M_SimpleInjector_Container_Register__1_3.htm

Here are a few more examples if you want more information, in addition to the links of the documentation previously posted:

https://stackoverflow.com/questions/40826996/lifestyle-mismatch-web-request-depends-on-transient-in-simple-injector

  • Dae Gabriel, that’s right... the life time.... thank you very much for your help...!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.