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?